I wondered, where can I find the mechanism of the System.Object methods? After all, all the time we only use or overload these methods, and where can we find exactly the mechanism of the same Equals ()? F12

VS unfortunately through the "Show Definition" gives only a description of the methods. Thank you in advance.

enter image description here

I do not see the source by skipping the library mscorlib.dll through ILdasm

  • 2
    You can see the source code - Grundy
  • @Grundy Do not tell me, in the development environment where can I see the source? - Rifter
  • In the development environment - nowhere. Maximum you can use any decompiler, but this is usually not necessary, especially when they are freely available - Grundy
  • @Grundy I do not want to make a discussion, but I do not understand why the bodies of the methods are hidden, but only their descriptions. Anyway, thanks to the link to the weights. - Rifter
  • one
    @Rifter, do not confuse the source code, and the compiled library. - Grundy

1 answer 1

You will not see the implementation of Object.Equals in .NET code. Calling this method in .NET calls native code. You can see the source of this method in clr source clr : https://github.com/dotnet/coreclr/blob/master/src/classlibnative/bcltype/objectnative.cpp#L153

 // // Compare by ref for normal classes, by value for value types. // // <TODO>@todo: it would be nice to customize this method based on the // defining class rather than doing a runtime check whether it is // a value type.</TODO> // FCIMPL2(FC_BOOL_RET, ObjectNative::Equals, Object *pThisRef, Object *pCompareRef) { CONTRACTL { FCALL_CHECK; INJECT_FAULT(FCThrow(kOutOfMemoryException);); } CONTRACTL_END; if (pThisRef == pCompareRef) FC_RETURN_BOOL(TRUE); // Since we are in FCALL, we must handle NULL specially. if (pThisRef == NULL || pCompareRef == NULL) FC_RETURN_BOOL(FALSE); MethodTable *pThisMT = pThisRef->GetMethodTable(); // If it's not a value class, don't compare by value if (!pThisMT->IsValueType()) FC_RETURN_BOOL(FALSE); // Make sure they are the same type. if (pThisMT != pCompareRef->GetMethodTable()) FC_RETURN_BOOL(FALSE); // Compare the contents (size - vtable - sync block index). DWORD dwBaseSize = pThisRef->GetMethodTable()->GetBaseSize(); if(pThisRef->GetMethodTable() == g_pStringClass) dwBaseSize -= sizeof(WCHAR); BOOL ret = memcmp( (void *) (pThisRef+1), (void *) (pCompareRef+1), dwBaseSize - sizeof(Object) - sizeof(int)) == 0; FC_GC_POLL_RET(); FC_RETURN_BOOL(ret); } FCIMPLEND 
  • Thanks for the answer, I probably do not understand the whole mechanism of compilation. As I imagine - After running the compiler, C # turns into IL modules with metadata, which are assembled and then compiled into native functions. At what stage does C ++ code mix with C #? And how does IntelSense then perceive C # syntax with libraries written in another language? - Rifter 1:58 pm
  • @Rifter IL builds are compiled into native with CLR, which is written in C ++. The CLR may, at its discretion, substitute some native method that is inside the CLR to the place where the .NET method is called. - Zergatul