2010-01-21 7 views

Respuesta

2

No estoy seguro de si este es el caso general, pero creo que sí. Pruebe lo siguiente:

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

Gracias por la respuesta rápida – djp

3

utilizan el método de la propiedad Method del delegado GetCustomAttributes. He aquí una muestra:

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

Si el método tiene el atributo de la var v contendrá ella; de lo contrario, será una matriz vacía.

+0

Gracias por la respuesta rápida – djp

Cuestiones relacionadas