When creating a DynamicMethod in C# for which bypassing visibility is not necessary or irrelevant, what is the best value to specify for the skipVisibility parameter of the DynamicMethod constructor? Is there a performance penalty, i.e., associated with a runtime security demand, for specifying true for the skipVisibility, and if so, would the penalty occur on every call, or just once at JIT-time, prior to first access?
MSDN doc: DynamicMethod
I found some relevant code in the .NET reference source for
DynamicMethod. As shown below, specifyingskipVisibilityforces a security demand forReflectionPermission. (The code also has another case where this permission is demanded regardless of theskipVisibilityparameter).So an answer would be that there may be a performance penalty when specifying true for
skipVisibility. However, depending on the various other argument types you use withnew DynamicMethod(...)and perhaps the IL that you emit, yourDynamicMethodmay actually requireReflectionPermission, and you will not be able to specify false. The error given in such cases is: