This problem was introduced by changes made in .NET
Framework 4.0. I found a solution posted by user "Alan-N" on CodeProject.
The big slowdown in execution time is caused when the DynamicMethod
gets associated with a "system-provided, fully trusted, security-transparent assembly," which happens if you use the DynamicMethod(string, Type, Type[], bool)
constructor. It appears that .NET 4 is doing more security checks than the previous versions although I have no insight into, or explanation for, what is actually going on.
Associating the DynamicMethod
with a Type
(by using the DynamicMethod(string, Type, Type[], Type, bool)
constructor instead; notice the additional Type
-valued parameter, 'owner') completely removes the speed penalty.
There are some notes on MSDN which may be relevant (if only I could understand them!):