Comment déterminer si un type d'interface implémente un attibute personnalisé?Comment déterminer si un type d'interface implémente un attribut personnalisé
0
A
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));
}
Questions connexes
- 1. Comment déterminer si un type implémente un type d'interface générique spécifique
- 2. Comment puis-je déterminer si System.Type est un type personnalisé ou un type de cadre?
- 3. Comment vérifier si un type est marqué avec un attribut?
- 4. Déterminer si un type est un type anonyme
- 5. C# - comment déterminer si un Type est un nombre
- 6. Déterminer si un type est statique
- 7. Déterminer si un type est un type de référence ou un type de valeur
- 8. Test si un objet implémente une interface
- 9. Comment récupérer un attribut personnalisé dans watir?
- 10. ActionScript: Déterminer si superclasse implémente une interface particulière?
- 11. Schéma XML: comment spécifier un attribut avec un type 'simpleType' personnalisé?
- 12. Comment déterminer si Type est une structure?
- 13. Trick Loop - Comment afficher un attribut si ...?
- 14. XPath: Comment vérifier si un attribut existe?
- 15. Comment vérifier si un type .NET implémente abstraitement certaines interfaces .NET?
- 16. C# comment savoir si un objet distant implémente une interface
- 17. Déterminer si StructureMap a un type spécifique configuré?
- 18. Comment avoir un attribut sur un élément de type chaîne
- 19. Comment détecter si le type est un autre type générique
- 20. Déterminer si une classe implémente une liste générique dans un modèle T4
- 21. .NET: Comment déterminer si un objet est un objet COM?
- 22. Comment obtenir la propriété appartenant à un attribut personnalisé?
- 23. Comment déterminer si un groupe est un groupe de sécurité?
- 24. Comment déterminer si un pthread est actif?
- 25. Comment déterminer si un gif est animé?
- 26. Comment déterminer si objet est un NSNumber
- 27. Déterminer si un type donné peut actuellement être résolu par un conteneur Castle Windsor
- 28. Comment appliquer un attribut .net à un type de retour
- 29. . Réflexion .NET: déterminer si un tableau de T serait convertible en un autre type
- 30. Détecter si le type implémente ICollection <T>
(chèques sont nuls pour les mauviettes!) – Will