I want to see how the
public String Replace(String oldValue, String newValue);
method that is inside mscorlib.dll (System.String) works.
I decompiled the mscorlib.dll with dotPeek and inside the method there is a call to ReplaceInternal method which I cannot find it
string s = ReplaceInternal(oldValue, newValue);
I have search for this method even on the open source .NET Core from GIT but no luck.
Please explain where is this method and what is inside?
C# String Replace()The Replace() method returns a new string by replacing each matching character/substring in the string with the new character/substring.
The String. Replace() method allows you to easily replace a substring with another substring, or a character with another character, within the contents of a String object. This method is very handy, but it is always case-sensitive.
To do this, go to the Modules window and from the context menu of a . NET assembly, and then select the Decompile source code command. Visual Studio generates a symbol file for the assembly and then embeds the source into the symbol file. In a later step, you can extract the embedded source code.
The extern C++ code is here.
https://github.com/gbarnett/shared-source-cli-2.0/blob/master/clr/src/vm/comstring.cpp
Line 1578 has
FCIMPL3(Object*, COMString::ReplaceString, StringObject* thisRefUNSAFE, StringObject* oldValueUNSAFE, StringObject* newValueUNSAFE)
Having a look here, you will notice this:
// This method contains the same functionality as StringBuilder Replace.
// The only difference is that
// a new String has to be allocated since Strings are immutable
[System.Security.SecuritySafeCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private extern String ReplaceInternal(String oldValue, String newValue);
The extern
keyword means that this method is implemented externally, in another dll.
That being said, it may be even written in a not managed dll (in C++ quite possibly), that is used by this module. So you can't decompile this code or see it, as you usually do with managed code.
Update
After a little searching I found the corresponding code in the coreclr project:
https://github.com/dotnet/coreclr/blob/master/src/classlibnative/bcltype/stringnative.cpp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With