Purpose of C# constructor extern modifier

后端 未结 2 1250
遇见更好的自我
遇见更好的自我 2021-02-04 08:45

What is the Purpose of C# constructor extern modifier?

I know about usage of extern METHODS to invoke Win32 functions, but what about CONSTRUCTORS?

Please give t

相关标签:
2条回答
  • 2021-02-04 09:30

    The c# spec here indicates that apart from private, internal, protected and public, extern may be used and that this is standard external reference - see here. This to me says that the contstructor is linked into the class at a later time. Just like the PInvoke calls are. There's nothing, I'm guessing, stopping the c# compiler implementer allowing linking of external .net .modules containing said external constructors.

    I cannot give an example, but I suspect one way woud be to implement the constructor in MC++, or in fact just a simple IL .module.

    0 讨论(0)
  • 2021-02-04 09:37

    I believe one use/purpose of an extern ctor is to have the constructor implemented within the CLR itself. if you disassemble mscorlib.dll using Reflector and look at the System.String type, you'll see:

    [MethodImpl(MethodImplOptions.InternalCall)]
    public extern String(char[] value);
    

    Which basically tells us that the (char[]) ctor for the string class is externally implemented, as part of the .net runtime.

    0 讨论(0)
提交回复
热议问题