2010-01-21 9 views

risposta

2

Non sono sicuro se questo è il caso generale, ma penso di si. Provare quanto segue:

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

Grazie per la risposta rapida – djp

3

utilizzare il metodo della Method di proprietà del delegato GetCustomAttributes. Ecco un esempio:

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

Se il metodo ha l'attributo, var v lo conterrà; altrimenti sarà una matrice vuota.

+0

Grazie per la risposta rapida – djp

Problemi correlati