How does the MethodImplAttribute work in .NET?

≯℡__Kan透↙ 提交于 2019-12-21 12:44:09

问题


I was investigating some framework code (the System.AppDomain.GetDynamicDir method) and this was all the assembler showed:

[MethodImpl(MethodImplOptions.InternalCall)]
private extern string GetDynamicDir();

What happens when this method is called? I don't mean this specific method, but methods with this attribute in general.


回答1:


From MSDN:

MethodImplOptions.InternalCall: Specifies an internal call. An internal call is a call to a method that is implemented within the common language runtime itself.

So basically, the CLR provides its own implementation of this method (which is probably in native code), which is why you can't see it in the disassembler.




回答2:


Answer here :

(...) MethodImplOptions.InternalCall is used in conjunction with extern to tell the runtime that the method is implemented internally within the system itself. This is done for many of the core .NET Framework methods that are better served by being implemented in unmanaged code. For example, many of the methods on the String, GC, and Math classes are marked as InternalCall. As you've noticed, Guid.CompleteGuid is also an InternalCall.



来源:https://stackoverflow.com/questions/1011702/how-does-the-methodimplattribute-work-in-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!