11

特定のカスタム属性を持つアセンブリ内のすべての型を検索するメソッドを作成しようとしています。また、一致する文字列値を指定できる必要があります。注意点は、これを任意のクラスで実行して、任意の値を返すことができるようにすることです。

例:このような呼び出しを実行したい

Type tTest = TypeFinder.GetTypesWithAttributeValue(Assembly.Load("MyAssembly"), typeof(DiagnosticTestAttribute), "TestName", "EmailTest");

これまでの私の方法は次のようになります。

public static Type GetTypesWithAttributeValue(Assembly aAssembly, Type tAttribute, string sPropertyName, object oValue)
{
    object oReturn = null;
    foreach (Type type in aAssembly.GetTypes())
    {
        foreach (object oTemp in type.GetCustomAttributes(tAttribute, true))
        {
            //if the attribute we are looking for matches
            //the value we are looking for, return the current type.
        }
    }
    return typeof(string); //otherwise return a string type
}

私の属性は次のようになります。

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class DiagnosticTestAttribute : Attribute
{
    private string _sTestName = string.Empty;

    public string TestName
    {
        get { return _sTestName; }
    }
    public DiagnosticTest(string sTestName)
    {
        _sTestName = sTestName;
    }
}

表現に精通している方のために、私は本当に次のような電話をかけられるようにしたいと思います。

TypeFinder.GetTypesWithAttributeValue<DiagnosticTestAttribute>(Assembly.Load("MyAssembly"), x=> x.TestName, "EmailTest");

式がジェネリック型を使用して、探しているプロパティを選択する場合。

4

2 に答える 2

14

これはうまくいくはずです:

public static Type GetTypeWithAttributeValue<TAttribute>(Assembly aAssembly, Func<TAttribute, object> pred, object oValue) {
  foreach (Type type in aAssembly.GetTypes()) {
    foreach (TAttribute oTemp in type.GetCustomAttributes(typeof(TAttribute), true)) {
      if (Equals(pred(oTemp), oValue)) {
        return type;
      }
    }
  }
  return typeof(string); //otherwise return a string type
}

またはさらに良い:

public static Type GetTypeWithAttributeValue<TAttribute>(Assembly aAssembly, Predicate<TAttribute> pred) {
  foreach (Type type in aAssembly.GetTypes()) {
    foreach (TAttribute oTemp in type.GetCustomAttributes(typeof(TAttribute), true)) {
      if (pred(oTemp)) {
        return type;
      }
    }
  }
  return typeof(string); //otherwise return a string type
}

呼び出しは次のようになります。

  GetTypeWithAttributeValue<DefaultValueAttribute>(Assembly.GetCallingAssembly(), attribute => attribute.Value,
                                                    "string");

そしてこれはそれぞれ:

  GetTypeWithAttributeValue<DefaultValueAttribute>(Assembly.GetCallingAssembly(),
                                                    attribute => attribute.Value == "string");
于 2013-01-21T17:18:00.903 に答える
2

ずっと前に、式からプロパティ名を抽出するヘルパー メソッドをいくつか開発しました。

私はそれがあなたに役立つと思います。

public static string Item<TItem, TMember>(this TItem obj, Expression<Func<TItem, TMember>> expression)
{
    if (expression.Body is MemberExpression)
    {
        return ((MemberExpression)(expression.Body)).Member.Name;
    }
    if (expression.Body is UnaryExpression)
    {
        return ((MemberExpression)((UnaryExpression)(expression.Body)).Operand).Member.Name;
    }
    if (expression.Body is ParameterExpression)
    {
        return expression.Body.Type.Name;
    }
    throw new InvalidOperationException();
}

詳細については、このファイルを参照してください。

于 2013-01-21T17:13:03.773 に答える