2010-01-21 6 views

답변

2

일반적인 경우인지 확실하지 않지만 그렇게 생각합니다. 다음보십시오 :

class Program 
{ 
    static void Main(string[] args) 
    { 
     // display the custom attributes on our method 
     Type t = typeof(Program); 
     foreach (object obj in t.GetMethod("Method").GetCustomAttributes(false)) 
     { 
      Console.WriteLine(obj.GetType().ToString()); 
     } 

     // display the custom attributes on our delegate 
     Action d = new Action(Method); 
     foreach (object obj in d.Method.GetCustomAttributes(false)) 
     { 
      Console.WriteLine(obj.GetType().ToString()); 
     } 

    } 

    [CustomAttr] 
    public static void Method() 
    { 
    } 
} 

public class CustomAttrAttribute : Attribute 
{ 
} 
+0

빠른 응답 주셔서 감사합니다. – djp

3

대리자의 Method 속성의 GetCustomAttributes 방법을 사용합니다. 다음은 샘플입니다.

delegate void Del(); 

    [STAThread] 
    static void Main() 
    { 
     Del d = new Del(TestMethod); 
     var v = d.Method.GetCustomAttributes(typeof(ObsoleteAttribute), false); 
     bool hasAttribute = v.Length > 0; 
    } 

    [Obsolete] 
    public static void TestMethod() 
    { 
    } 

메서드에 var v가 포함되어 있으면; 그렇지 않으면 빈 배열이됩니다.

+0

빠른 응답 주셔서 감사합니다. – djp

관련 문제