5

While disassembling the .Net Source Code using Reflector, I came upon the Equals implementation in the Object Class and it refers to

bool InternalEquals(object objA, object objB);

Which again refers to

internal static extern bool InternalEquals(object objA, object objB);

I am now confused regarding where to find the implementation of this InternalEquals(object objA, object objB) function and how is it using this function and in which .Net assembly is this function defined and also if each and everything is written from scratch for the .Net Source Code, then why am I unable to find this function's implementation.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Gunwant Saini
  • 317
  • 3
  • 10
  • 17

2 Answers2

12

It's declared as [MethodImpl(MethodImplOptions.InternalCall)]. It means that it's implemented in the CLR itself, as a native procedure, not a .NET assembly.

You can view a similar CLR source code by looking at Microsoft SSCLI (aka Rotor).

In SSCLI 2.0 it's implemented as (in sscli20/clr/src/vm/comobject.cpp):

FCIMPL2(FC_BOOL_RET, ObjectNative::Equals, Object *pThisRef, Object *pCompareRef)
{
    CONTRACTL
    {
        THROWS;
        DISABLED(GC_NOTRIGGER);
        INJECT_FAULT(FCThrow(kOutOfMemoryException););
        MODE_COOPERATIVE;
        SO_TOLERANT;          
    }
    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->IsValueClass())
         FC_RETURN_BOOL(FALSE);

    // Make sure they are the same type.
    if (pThisMT != pCompareRef->GetMethodTable())
        FC_RETURN_BOOL(FALSE);

    // Compare the contents (size - vtable - sink block index).
    BOOL ret = memcmp(
        (void *) (pThisRef+1), 
        (void *) (pCompareRef+1), 
        pThisRef->GetMethodTable()->GetBaseSize() - sizeof(Object) - sizeof(int)) == 0;

    FC_GC_POLL_RET();

    FC_RETURN_BOOL(ret);
}
FCIMPLEND
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • Link to SSCLI: http://www.microsoft.com/downloads/details.aspx?FamilyId=8C09FD61-3F26-4555-AE17-3121B4F51D4D&displaylang=en – Fake Jim Dec 21 '08 at 10:43
0

First, find in sscli20\clr\src\vm\ecall.cpp function mapping ("InternalEquals" -> ObjectNative::Equals):

FCFuncStart(gObjectFuncs)
    FCIntrinsic("GetType", ObjectNative::GetClass, CORINFO_INTRINSIC_Object_GetType)
    FCFuncElement("InternalGetHashCode", ObjectNative::GetHashCode)
    FCFuncElement("InternalEquals", ObjectNative::Equals)
    FCFuncElement("MemberwiseClone", ObjectNative::Clone)
FCFuncEnd()

Next, find an implementation ObjectNative::Equals (published by Mehrdad Afshari)

Viacheslav Ivanov
  • 1,535
  • 13
  • 22