1

すべての Class から CustomAtrribute の PropertyValue を String として取得したい

これは、ExportAttribute から継承されたカスタム属性です。

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

この属性は多くのクラスに関連付けられていますが、パラメーターが異なります。最初のパラメーターは ContractName を示し、2 番目のパラメーターはそれが属するモジュールを示します。

今、私は辞書を手に入れたい

Dictionary<string, List<string>> moduleDict

すべての ModuleName (2 番目のパラメーター) と ContractName (1 番目のパラメーター) を使用すると、同じモジュール名を持つクラスが存在する可能性があるため、そのモジュール名を持つコントラクト名のリストが必要です

Reflection を使用してすべての 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 に答える