Use the GetCustomAttributes
which will return a collection of attributes the given object has. Then check if any are of your desired type
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
}
Reading more about IsDefined
as Александр Лысенко suggested it does seem to be what you are looking for:
Returns true if one or more instances of attributeType or any of its derived types is applied to this member; otherwise, false.