2011-06-30 124 views
15

Ive得到了这个自定义属性:访问自定义属性的值

[AttributeUsage(AttributeTargets.Method, AllowMultiple=false, Inherited = true)] 
class MethodTestingAttibute : Attribute 
{ 
    public string Value{ get; private set; } 
    public MethodTestingAttibute (string value) 
    { 
     this.Value= value; 

    } 
} 

要这样来使用:

[MethodTestingAttibute("2")] 
    public int m1() {return 3; } 

而我做太困难是采取的“2”的价值MethodTestingAttibute

object result = method.Invoke(obj, new Type[] {}); // here i get the return 

现在我想比较这个结果与方法TestingAttibute的值。我该怎么做?我试着往上走这条路,但没有成功:method.GetCustomAttributes(typeof运算(MethodTestAttibute),真)[0] ...

什么是正确获得访问Custoum领域属性?

+0

我很困惑。你说“3”的意思是“2”吗? –

+0

我的意思是2!对不起 – RCPT

+0

非常接近:[any-know-a-quick-way-to-get-to-custom-attributes-on-an-enum-value](http://stackoverflow.com/questions/17772/anyone-know -a-快速的方式对获得到自定义属性上-AN-枚举值) – nawfal

回答

21
var attribute = 
    (MethodTestingAttibute) 
    typeof (Vehicles) 
     .GetMethod("m1") 
     .GetCustomAttributes(typeof (MethodTestingAttibute), false).First(); 
Console.WriteLine(attribute.Value); 
1

投的对象MethodTestingAttibute

object actual = method.Invoke(obj, null); 

MethodTestingAttibute attribute = (MethodTestingAttibute)method.GetCustomAttributes(typeof(MethodTestAttribute), true)[0]; 
string expected = attribute.Value; 

bool areEqual = string.Equals(expected, actual != null ? actual.ToString() : null, StringComparison.Ordinal); 
1

检查这里的代码http://msdn.microsoft.com/en-us/library/bfwhbey7.aspx

摘录:

 // Get the AClass type to access its metadata. 
     Type clsType = typeof(AClass); 
     // Get the type information for Win32CallMethod. 
     MethodInfo mInfo = clsType.GetMethod("Win32CallMethod"); 
     if (mInfo != null) 
     { 
      // Iterate through all the attributes of the method. 
      foreach(Attribute attr in 
       Attribute.GetCustomAttributes(mInfo)) { 
       // Check for the Obsolete attribute. 
       if (attr.GetType() == typeof(ObsoleteAttribute)) 
       { 
        Console.WriteLine("Method {0} is obsolete. " + 
         "The message is:", 
         mInfo.Name); 
        Console.WriteLine(" \"{0}\"", 
         ((ObsoleteAttribute)attr).Message); 
       } 

       // Check for the Unmanaged attribute. 
       else if (attr.GetType() == typeof(UnmanagedAttribute)) 
       { 
        Console.WriteLine(
         "This method calls unmanaged code."); 
        Console.WriteLine(
         String.Format("The Unmanaged attribute type is {0}.", 
             ((UnmanagedAttribute)attr).Win32Type)); 
        AClass myCls = new AClass(); 
        myCls.Win32CallMethod(); 
       } 
      } 
     } 
0

获取属性属性的值,只投)由GetCustomAttributes(返回的对象:

{ 
    string val; 
    object[] atts = method.GetCustomAttributes(typeof(MethodTestAttibute), true); 
    if (atts.Length > 0) 
     val = (atts[0] as MethodTestingAttibute).Value; 
} 
0

Necromancing。
对于那些仍然要保持.NET 2.0,或那些想这样做没有LINQ:

public static object GetAttribute(System.Reflection.MemberInfo mi, System.Type t) 
{ 
    object[] objs = mi.GetCustomAttributes(t, true); 

    if (objs == null || objs.Length < 1) 
     return null; 

    return objs[0]; 
} 



public static T GetAttribute<T>(System.Reflection.MemberInfo mi) 
{ 
    return (T)GetAttribute(mi, typeof(T)); 
} 


public delegate TResult GetValue_t<in T, out TResult>(T arg1); 

public static TValue GetAttributValue<TAttribute, TValue>(System.Reflection.MemberInfo mi, GetValue_t<TAttribute, TValue> value) where TAttribute : System.Attribute 
{ 
    TAttribute[] objAtts = (TAttribute[])mi.GetCustomAttributes(typeof(TAttribute), true); 
    TAttribute att = (objAtts == null || objAtts.Length < 1) ? default(TAttribute) : objAtts[0]; 
    // TAttribute att = (TAttribute)GetAttribute(mi, typeof(TAttribute)); 

    if (att != null) 
    { 
     return value(att); 
    } 
    return default(TValue); 
} 

用法示例:

System.Reflection.FieldInfo fi = t.GetField("PrintBackground"); 
wkHtmlOptionNameAttribute att = GetAttribute<wkHtmlOptionNameAttribute>(fi); 
string name = GetAttributValue<wkHtmlOptionNameAttribute, string>(fi, delegate(wkHtmlOptionNameAttribute a){ return a.Name;}); 

或在您的情况简单地

MethodInfo mi = typeof(Vehicles).GetMethod("m1"); 
string aValue = GetAttributValue<MethodTestingAttibute, string>(mi, a => a.Value); 
0

用我的自定义属性:

[AttributeUsage(AttributeTargets.Method)] 
public class AttributeCustom : Attribute 
{ 
    public string MyPropertyAttribute { get; private set; } 

    public AttributeCustom(string myproperty) 
    { 
     this.MyPropertyAttribute = myproperty; 
    } 
} 

我创建获得属性与他值的方法:

public static AttributeCustom GetAttributeCustom<T>(string method) where T : class 
{ 
    try 
    { 
     return ((AttributeCustom)typeof(T).GetMethod(method).GetCustomAttributes(typeof(AttributeCustom), false).FirstOrDefault()); 
    } 
    catch(SystemException) 
    { 
     return null; 
    } 
} 

有了一个例子类(必须是不是静态的,因为T是通用)

public class MyClass 
{ 
    [AttributeCustom("value test attribute")]) 
    public void MyMethod() 
    { 
     //... 
    } 
} 

用法:

var customAttribute = GetAttributeCustom<MyClass>("MyMethod"); 
if (customAttribute != null) 
{ 
    Console.WriteLine(customAttribute.MyPropertyAttribute); 
}