title: GetCustomAttributes vs GetCustomAttributesData
body: <p>I'm trying to figure out the best way to get custom attributes from a property.  I've been using <a href="https://docs.microsoft.com/en-us/dotnet/api/system.reflection.memberinfo.getcustomattributes" rel="noreferrer"><code>GetCustomAttributes()</code></a> for this but I've recently read that <code>GetCustomAttributes()</code> causes an instance of the attribute to be created and <a href="https://docs.microsoft.com/en-us/dotnet/api/system.reflection.memberinfo.getcustomattributesdata" rel="noreferrer"><code>GetCustomAttributesData()</code></a> just gets data about the attribute without having to create an instance of the attribute.</p>
  
  <p>With this in mind, it seems like <code>GetCustomAttributesData()</code> 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 <code>GetCustomAttributes()</code> running about 6ms and <code>GetCustomAttributesData()</code> running about 32ms.  </p>
  
  <p>Does anyone know why it takes <code>GetCustomAttributesData()</code> longer to run?</p>
  
  <p>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 <code>GetCustomAttributesData()</code> is slower than <code>GetCustomAttributes()</code>.</p>
  
  <p>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.</p>
  
  <pre><code>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&lt;MyCustomAttribute&gt;().Count() &gt; 0)
              continue;
  
          // This runs around 32ms on the first run
          if (prop.GetCustomAttributesData().Where(x =&gt; x.AttributeType == typeof(MyCustomAttribute)).Count() &gt; 0)
              continue;
  
          // Do some work...
      }
  }
  
  public class MyCustomAttribute : System.Attribute
  {
  }
  </code></pre>
  
  <hr>
  
  <p>Just a little while ago, I decided to try the <a href="https://docs.microsoft.com/en-us/dotnet/api/system.reflection.memberinfo.isdefined" rel="noreferrer"><code>IsDefined()</code></a> method after reading <a href="https://stackoverflow.com/questions/14719104/is-there-a-benefit-of-using-isdefined-over-getcustomattributes">this</a> post. It seems to be faster than both <code>GetCustomAttributes()</code> and <code>GetCustomAttributesData()</code>.</p>
  
  <pre><code>if (prop.IsDefined(typeof(MyCustomAttribute)))
      continue;
  </code></pre>