获取CustomAttributeValue属性值作为字符串数组

时间:2013-04-07 10:33:42

标签: c# .net wpf mef caliburn.micro

我想从所有Class中获取CustomAtrribute的PropertyValue作为String

这是我从ExportAttribute继承的自定义属性

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

此属性附加在许多类上,但具有不同的参数。 第一个参数表示ContractName,第二个参数表示它属于哪个模块。

现在我想要一本字典

Dictionary<string, List<string>> moduleDict

使用所有ModuleName(第二个参数)和ContractName(第一个参数),可以有具有相同模块名称的类,所以我需要一个具有该模块名称的合同名称列表

我能够使用Reflection获取所有JIMSExport属性但无法生成字典

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

使用Caliburn Micro有没有更好的方法

3 个答案:

答案 0 :(得分:2)

也许你正在寻找这样的东西:

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;
        }

    }
}

答案 1 :(得分:0)

需要使用SelectManyGroupBy

// 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);
    }
}

答案 2 :(得分:0)

首先让我说我对Caliburn Micro不熟悉。因此,正常MEF使用可能不适用的可能性很小。

使用MEF的Export Metadata可以实现您的目标。将自定义ExportAttribute(JIMSExport)与导出元数据相结合,并修改导入以包含元数据。研究上一个链接中的“使用自定义导出属性”部分。

请注意,您不应该回退使用已加载程序集的反射。这可能会导致错误,因为反射会找到具有指定属性的所有类型,而您实际需要的是可以正确导出的类型。您需要组合容器可以使用的类型。