Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decompile .NET Replace method (v4.6.1)

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.

View my Decompiled code

Please explain where is this method and what is inside?

like image 889
Pacurar Stefan Avatar asked Sep 09 '16 04:09

Pacurar Stefan


People also ask

How can I replace part of a string in C#?

C# String Replace()The Replace() method returns a new string by replacing each matching character/substring in the string with the new character/substring.

Is replace in C# case sensitive?

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.

How do I decompile DLL in Visual Studio?

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.


2 Answers

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)
like image 189
Veener Avatar answered Oct 03 '22 09:10

Veener


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

like image 25
Christos Avatar answered Oct 03 '22 09:10

Christos