2013-01-21 28 views
9

动态获取类属性值我试图编写一个方法,该方法可以查找具有特定自定义属性的程序集中的所有类型。我还需要能够提供匹配的字符串值。需要注意的是,我希望能够在任何课程上运行此操作并返回任何值。从类型

例如: 我想执行像调用此

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

对于那些熟悉表情的人,我真的很想打个电话给李ke:

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

其中表达式使用我的泛型类型来选择我正在寻找的属性。

回答

13

这应该工作:

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"); 
+0

这工作完美无瑕。我不得不改变装配,因为我从另一个装置调用装配,但效果很好。谢谢一堆。 –

+0

如果我可以给你更多的信誉,我会;-) – Seabizkit

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

查看更多this file

+0

这很有帮助。只要我能弄清楚如何从一个实例类的属性中提取出值,该属性应该可以帮助我为我的方法创建一个漂亮的界面。谢谢! –