How to find the implementation Code of Public extern bool equals (string value? [C #, C ++, BCl, CLR]

Source: Internet
Author: User

How to find the implementation of public extern bool equals (string value)Code? [C #, C ++, BCl, CLR]

 

Written by Allen Lee

 

Q: The rotor provided by MicrosoftSource code, I found that the string. Equals (string value) Code only has the following two lines:

// Code #01
[Methodimplattritions (methodimploptions. internalcall)]
Public   Extern   Bool Equals (string value );

So where can I find the implementation code of this method?

A: Open the sscli \ CLR \ SRC \ VM \ eCall. cpp file in the rotor source code and search for the "equals" keyword. You will find the following array members:

// Code #02
Static Ecfunc gstringfuncs [] =   {

// Other members here

{Fcfuncelement ("Equals",&Gsig_im_str_retbool, (lpvoid) comstring: ipvsstring )} ,

// Other members here
} ;

From this array Member, we can find that we are looking for the comstring: javassstring function. So where is the implementation code of the comstring: javassstring function?

Open the sscli \ CLR \ SRC \ VM \ comstring. cpp file in the rotor source code and search for the "comstring: javassstring" keyword. How can I find the implementation code?

Q: Yes, I found:

// Code #03
Fcimpl2 int32 (comstring: javassstring, stringobject * Thisstr, stringobject * Valuestr)
{
Validateobjectref (thisstr );
Validateobjectref (valuestr );

Int32 RET =   False ;
If (Null = Thisstr)
Fcthrow (knullreferenceexception );

If ( ! Valuestr)
{
Fc_gc_poll_ret ();
ReturnRET;
}

RET = Wcharcomparehelper (stringref (thisstr), stringref (valuestr ));
Fc_gc_poll_ret ();
Return RET;
}
Fcimplend

Can you briefly describe it?

A: The Function Definition of code #03 uses a macro (macro ). In the sscli \ CLR \ SRC \ VM \ fcall. h file, you will find the following statement:

// Code #04
# Define Fcdecl2 (rettype, funcname, a1, a2) rettype f_call_conv funcname (a1, a2)

This is the macro definition of the function. Therefore, the C ++ code you found above can be expanded:

// Code #05
Int32 f_call_conv comstring: jsonsstring (stringobject * Thisstr, stringobject * Valuestr)
{
// 
}

The macro shown in code #04 corresponds to the x86 system. For non-x86 systems, the macro definition of this function is:

// Code #06
# Define Fcimpl2 (rettype, funcname, a1, a2) rettype funcname (a1, a2) {fcimpl_prolog (funcname)

In addition, this function actually handed over the real comparison work to the wcharcomparehelper function. You can find its real body in the same file (comstring. cpp:

// Code #07
Bool Wcharcomparehelper (stringref thisstr, stringref valuestr)
{
DWORD * Thischars, * Valuechars;
Int Thislength, valuelength;

// Get all of our required data.
Refinterpretgetstringvaluesdangerousforgc (thisstr, (wchar ** ) & Thischars, & Thislength );
Refinterpretgetstringvaluesdangerousforgc (valuestr, (wchar ** ) & Valuechars, & Valuelength );

// If they're different lengths, they're not an exact match.
If (Thislength ! = Valuelength) {
Return False;
}

// Loop comparing a DWORD (2 wchars) at a time.
While (Thislength -=   2 ) > =   0 )
{
If ( * Thischars ! =   * Valuechars)
Return   False ;
++ Thischars;
++ Valuechars;
}

// Handle an extra wchar.
If (Thislength =   - 1 )
Return ( * (Wchar * ) Thischars) =   * (Wchar * ) Valuechars ));

Return   True ;
}

So far, you have found the rotor's string. Equals (string value) ImplementationAlgorithm.

Q: What is the eCall. cpp file used?

A: This file contains a large number of arrays. These arrays act as a table and are used to mark [methodimplattritions (methodimploptions. internalcall)] property method ing is an unmanaged C ++ implementation. The array members actually use macros. Code #02

// Code #08
Fcfuncelement ( " Equals " , & Gsig_im_str_retbool, (lpvoid) comstring: ipvsstring)

Corresponding to the macro definition of code #09:

// Code #09
# Define Fcfuncelement (A, B, C) a, B, c, null, corinfo_intrinsic_illegal

Q: What is fcall. h used?

A: This file also defines a large number of macros. These macros act as "function templates" to organize functions with similar signatures and expand them in a unified manner. At the beginning of the file, there are related notes to describe the functions, working principles, and precautions of these macros.

Q: Under what circumstances can we use this method to find the method code that is not provided in Rotor's Bcl?

A: We know that there is no specific implementation code method. We need to add extern to indicate that the implementation can be found somewhere else. Generally, this modifier is used in combination with dllimportattribute:

// Code #10
[Dllimport ( " Uxtheme. dll " )]
Static   Public   Extern   Int Setwindowtheme (intptr hwnd, stringbuilder appid, stringbuilder classid );
Static   Public   Void Disablewindowsxptheme (intptr hwnd)
{
// Disable using the Window XP theme for the window handle
// Passed as a parameter
Stringbuilder applicationname =   New Stringbuilder ( "   " , 1 );
Stringbuilder classids =   New Stringbuilder ( "   " , 1 );
Win32.setwindowtheme (hwnd, applicationname, classids );
}

In this way,. NET will automatically find the corresponding implementation code from uxtheme. dll. This call method is called pinvoke. Of course, in this case, you cannot obtain the specific implementation code.

However, there are a considerable number of methods in BCl that are modified using extern and methodimplattritions. In this case, the parameter passed to the methodimplattribute constructor is methodimploptions. internalcall enumeration indicates that you can use the method I introduced to you to find the implementation code. The bottom line is that you have these underlying unmanaged source code for your search!

Q: What does methodimplattribute do?

A: This attribute is located in the namespace of system. runtime. compilerservices. It is used in combination with the methodimploptions enumeration to describe the implementation method of the method or constructor. [Methodimplattritions (methodimploptions. internalcall)] in code #01 indicates that the specific implementation of this method can be found in CLR. This attribute is essentially a pseudo-attribute. Unlike common custom attributes, it is stored in metadata table in the form of bits, you can only use methodbase. getmethodimplementationflags to obtain relevant information.

Q: From the above discussion, we can understand that the javassstring function belongs to a class called comstring. What is the relationship between the comstring class and the string class in the Bcl?

A: Good question! We can find this sentence in string. CS in rotor Bcl:

Actual implementations are in string. cpp

In fact, this string. cpp is comstring. cpp; similarly, in comstring. cpp, we can also see this sentence:

Purpose: The implementation of the string class.

We found that many methods in string. CS do not provide specific implementation code, and the real bodies of these methods are actually hidden in comstring. cpp.

Q: What are your suggestions for exploring the Internal principles of. Net?

A: In fact, the rotor source code provided by Microsoft and the mono open-source project already have rich comments for learning. Of course, I 'd like to introduce several useful connections to you:

    • Rotor source code: This website displays the code of rotor Bcl in the form of a webpage, and the relevant code uses the C # language.
    • Rotor VM documentation: This website displays the code of the rotor VM in the form of web pages. The relevant code is in the C ++ language.
    • Jason Whittington, Shared Source CLI provides source code for a freebsd implementation of. net.

I hope these materials will help you better explore the Internal principles of. net.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.