1

I want to get the PropertyValue of CustomAtrribute from all Class as String

Here is my Custom Attribute inherited from ExportAttribute

[JIMSExport("StockGroup","Stock")]

This attribute is attached on many class but with different parameters. First Parameter indicates ContractName and Second one say which Module it belongs to.

Now I want to get a dictionary

Dictionary<string, List<string>> moduleDict

with all ModuleName (2nd Parameter) and ContractName (1st Parameter), There can be classes with same module Name, so I need a List of Contract name with that Module Name

I am able to get all the JIMSExport attribute using Reflection but failed to generate the dictionary

var exported = GetTypesWith<JIMSExportAttribute>(false).Select(x => x.GetCustomAttributes(true).First());

Is there any better way of this doing using Caliburn Micro

Kishore Kumar
  • 12,675
  • 27
  • 97
  • 154

3 Answers3

2

maybe you are looking for something like this:

namespace AttributePlayground
{
    class Program
    {
        static void Main(string[] args)
        {
            var moduleDict = makeModuleDict();
        }

        public static Dictionary<string, List<string>> makeModuleDict()
        {
            var attribs = AppDomain.CurrentDomain
                .GetAssemblies()
                .SelectMany(
                    x => x.GetTypes()
                        .SelectMany(
                            y => y.GetCustomAttributes(typeof(JIMSExport), false)
                        )
                )
                .OfType<JIMSExport>();

            return attribs
                .GroupBy(x => x.Module)
                .ToDictionary(
                    x => x.Key,
                    x => new List<String>(
                        x.Select(y => y.ContractName)
                        )
                    );

        }

    }

    [JIMSExport("Module1", "Contract1")]
    public class TestClass1
    {

    }

    [JIMSExport("Module1", "Contract2")]
    public class TestClass2
    {

    }

    [JIMSExport("Module2", "Contract3")]
    public class TestClass3
    {

    }

    [JIMSExport("Module2", "Contract4")]
    public class TestClass4
    {

    }

    [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
    sealed class JIMSExport : Attribute
    {
        public readonly string ContractName;
        public readonly string Module;
        public JIMSExport(string Module,string ContractName)
        {
            this.Module = Module;
            this.ContractName = ContractName;
        }

    }
}
DarkSquirrel42
  • 10,167
  • 3
  • 20
  • 31
0

need to use SelectMany and GroupBy

// allTypesWithReqdAttributes should contain list of types with JIMSExportAttribute
IEnumerable<object> attributeList = allTypesWithReqdAttribute.SelectMany(x => x.GetCustomAttributes(true));
var myAttributeList = attributeList.OfType<JIMSExportAttribute>();
// assumes JIMSExportAttribute has ModuleName and ContractName properties
var groups = myAttributeList.GroupBy(x => x.ModuleName, x => x.ContractName);
var result = groups.ToDictionary(x => x.Key, x => new List<string>(x));
foreach (var group in groups)
{
    Console.WriteLine("module name: " + group.Key);
    foreach (var item in group)
    {
        Console.WriteLine("contract name: " + item);
    }
}
Sushil
  • 5,265
  • 2
  • 17
  • 15
0

Firstly let me say that I am not familiar with Caliburn Micro. So there is a small chance that normal MEF usage might not apply.

What you want can be achieved with MEF's Export Metadata. Combine your custom ExportAttribute (JIMSExport) with export metadata and modify you imports to include metadata. Study the "Using a Custom Export Attribute" section in previous link.

Note that you should not fall back to using reflection on the loaded assemblies. This could lead to errors since reflection will find all types with the specified attribute while what you actually want is the types that can be properly exported. You need the types that can be used by the composition container.

Panos Rontogiannis
  • 4,154
  • 1
  • 24
  • 29