使用GetCustomAttributes
将返回属性给定的对象具有的集合。然后检查如果有任何你想要的类型
public static void Main(string[] args)
{
Set(new TestClass());
}
public static void Set(object objectToCache)
{
var result = objectToCache.GetType().GetCustomAttributes(false)
.Any(att => att is ProtoContractAttribute);
// Or other overload:
var result2 = objectToCache.GetType().GetCustomAttributes(typeof(ProtoContractAttribute), false).Any();
// result - true
}
阅读更多关于IsDefined
作为АлександрЛысенко建议它似乎是你在找什么:如果一个或多个
返回true attributeType或其任何派生类型的实例应用于此成员;否则,是错误的。
'objectToCache.GetType()GetCustomAttributes()'或其变型。 –