2010-07-26 3 views

Répondre

5

Utilisation GetCustomAttributes:

typeof(IWhatever).GetCustomAttributes(typeof(CustomAttribute), false) 

retourne un tableau d'attributs. Vide s'il ne met pas en œuvre celui que vous recherchez.

-1
Type iType = typeof(IMyInterface); 
var attributes = iType.GetCustomAttributes(typeof(MyCustomAttribute), true); 

Si attributes est vide, l'interface ne met pas en oeuvre votre attribut.

0

Essayez ceci sur la taille:

private static bool HasAttribute(this Type me, Type attribute) 
{ 
    if (!typeof(Attribute).IsAssignableFrom(attribute)) 
     throw new ArgumentException("attribute does not extend System.Attribute."); 
    return me.GetCustomAttributes(attribute, true).Length > 0; 
} 
private static bool HasAttribute<T>(this Type me) where T : System.Attribute 
{ 
    return me.HasAttribute(typeof(T)); 
} 
+0

(chèques sont nuls pour les mauviettes!) – Will

Questions connexes