title: .NET reflection 3 times slower in .NET 4.7.x than in 3.5
body: <p>I have an application that we upgraded form .NET 3.5 to .NET 4.7.2. The only problem really is performance from part of our code that uses reflection. Entire situation is best explained in a simple sample I uploaded to Gist: <a href="https://gist.github.com/vkocjancic/3e8a6b3496c412a75b1c85a1d2ba1111" rel="nofollow noreferrer">https://gist.github.com/vkocjancic/3e8a6b3496c412a75b1c85a1d2ba1111</a></p>
  
  <p>Basically, we have a POCO class, which property methods throw exceptions, if value is not set for properties of non-nullable types.</p>
  
  <blockquote>
    <p><strong>[EDIT]:</strong></p>
    
    <ol>
    <li><p>Yes, I know that is not correct or a good pattern to use, but, the application started in .NET 1.1.</p></li>
    <li><p>Yes, it should have been fixed long ago. It wasn't.</p></li>
    </ol>
  </blockquote>
  
  <p>A reflection is then used to get property names and values of instances of POCO class and popluate it to DataTable.</p>
  
  <p>The sample and real life project source code is exactly the same. The only difference is that in one case it is compiled using .NET 3.5 and in second using .NET 4.7.2.</p>
  
  <p>This is the average time elapsed in milliseconds for 10 invocations:</p>
  
  <pre><code>.NET 3.5      -&gt;  231.1 ms
  .NET 4.7.2    -&gt;  713.5 ms
  .NET Core 2.2 -&gt; 1013.2 ms
  </code></pre>
  
  <p>Can anyone elaborate why reflection is about 3 times slower in .NET 4.7.2 than in .NET 3.5 and how to fix that. The lag only happens, when properties are not set and throw exceptions. If you populate property values, there is no difference in performance.</p>
  
  <p>If I run sample in debugger, .NET 3.5 build never triggers MissingFieldException. Build using .NET 4.7.2 triggers every exception.</p>
  
  <blockquote>
    <p><strong>[EDIT]</strong></p>
    
    <p>As @bevan mentioned, the slowdown actually occurs when switching to .NET 4.0. > Also the problem boils down to exception being thrown and handled 3 times faster in .NET 3.5 than it is in .NET 4.0</p>
  </blockquote>