3

I wanted to answer this question and thought I would look inside of the Array source code to see how it is implemented. Therefore, I looked in .NET source code for the CreateInstance method and found that it calls an external method whose body is a semi-colon and implemented elsewhere. Here is what it looks like:

private unsafe static extern Array 
    InternalCreate(void* elementType,int rank,int *pLengths,int *pLowerBounds);

Question:

How do I find where the implementation for the above external method is?

Community
  • 1
  • 1
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64

1 Answers1

10

To find the source code for any extern methods, do the following:

  1. Find the name of the extern method. In my case it is InternalCreate.
  2. Go here and find the mapping of the method to the external method. In my case I needed to find InternalCreate and here is what the mapping looks like. The name of the class is ArrayNative and the method is CreateInstance:

    FCFuncElement("InternalCreate", ArrayNative::CreateInstance)
    
  3. Find the mapped class here. In my case I needed arraynative and I needed the method CreateInstance. The implementation is right there and I am copying it here but removing the body for brevity:

    FCIMPL4(Object*, ArrayNative::CreateInstance, 
        void* elementTypeHandle, INT32 rank, INT32* pLengths, INT32* pLowerBounds)
    {
        //...
    }
    

There you will find the implementation and study the code.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • 3
    One small note, those links are for the .NET Core runtime. The runtime used by the full .NET framework can be different than the code shown in the `dotnet/coreclr` repo, for example it is possible there could be a native type in the CLR that is used in http://referencesource.microsoft.com/ that is not in the Core CLR (Not that I know of any that are). One possible work around, use https://source.dot.net/ instead to do the intial browsing so you see the external dependencies of .NET Core instead of the full .NET framework. – Scott Chamberlain Apr 24 '17 at 17:41
  • @ScottChamberlain thanks for that note-I did not even realize it was the .NET Core Runtime I was looking at. Can you please elaborate on your workaround? If I wanted to put your workaround in my answer, what step will it go into? – CodingYoshi Apr 24 '17 at 17:58
  • It is okay, they maintained the same codebase for both the full and the core versions of the CLR. Just a bunch of #ifdefs to turn off features. That might not be true forever, coreclr is likely to diverge over time, but true enough for now. – Hans Passant Apr 24 '17 at 18:05
  • The workaround would go in to the step in the question instead of the answer. You would change *"Therefore, I looked in [.NET source code](http://referencesource.microsoft.com/#mscorlib/system/array.cs,9afc2f2b6acee49e) for the `CreateInstance` method"* to *"Therefore, I looked in [.NET source code](https://source.dot.net/System.Private.CoreLib/src/System/Array.cs.html#9afc2f2b6acee49e) for the `CreateInstance` method"* (click the two links and check your URL bar) – Scott Chamberlain Apr 24 '17 at 19:15