1

我想从所有类中获取 CustomAtrribute 的 PropertyValue 作为字符串

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

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

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

现在我想要一本字典

Dictionary<string, List<string>> moduleDict

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

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

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

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

4

3 回答 3

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

    }
}
于 2013-04-07T11:46:58.023 回答
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);
    }
}
于 2013-04-07T12:30:50.913 回答
0

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

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

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

于 2013-04-08T15:09:33.273 回答