I'm trying to figure out the best way to get custom attributes from a property. I've been using GetCustomAttributes() for this but I've recently read that GetCustomAttributes() causes an instance of the attribute to be created and GetCustomAttributesData() just gets data about the attribute without having to create an instance of the attribute.
With this in mind, it seems like GetCustomAttributesData() should be faster because it doesn't create an instance of the attribute. However, I'm not seeing this expected result in tests. When looping through the properties in a class, the first iteration has GetCustomAttributes() running about 6ms and GetCustomAttributesData() running about 32ms.
Does anyone know why it takes GetCustomAttributesData() longer to run?
My primary goal is to test for the existence of an attribute and ignore any property containing this attribute. I don't particularly care which method I end up using and I'm not really concerned with what either method returns apart from understanding why GetCustomAttributesData() is slower than GetCustomAttributes().
Here's some sample code I used to test with. I tested each of these if statements independently by commenting out one and then the other.
public static void ListProperties(object obj)
{
PropertyInfo[] propertyInfoCollection = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in propertyInfoCollection)
{
// This runs around 6ms on the first run
if (prop.GetCustomAttributes<MyCustomAttribute>().Count() > 0)
continue;
// This runs around 32ms on the first run
if (prop.GetCustomAttributesData().Where(x => x.AttributeType == typeof(MyCustomAttribute)).Count() > 0)
continue;
// Do some work...
}
}
public class MyCustomAttribute : System.Attribute
{
}
Just a little while ago, I decided to try the IsDefined() method after reading this post. It seems to be faster than both GetCustomAttributes() and GetCustomAttributesData().
if (prop.IsDefined(typeof(MyCustomAttribute)))
continue;