decompile .NET Replace method (v4.6.1)
Asked Answered
H

3

8

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?

Hoofbound answered 9/9, 2016 at 4:42 Comment(2)
ReplaceInternal ist extern, so most likely it is implemented as "unmanaged", native C++ code. See P/Invoke for details.Farkas
btw you dont need to decompile, you can see the source @ referencesource.microsoft.com/#mscorlib/system/…Heffernan
N
7

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)
Nutrient answered 9/9, 2016 at 5:0 Comment(1)
codeproject.com/Articles/1014073/…Nutrient
C
6

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

Cutaway answered 9/9, 2016 at 4:49 Comment(1)
Thank you very much, but where is the "another.dll" ? I want to see inside .. or what is his name?Hoofbound
T
0

To see how the function works, take a look at the reference source under http://referencesource.microsoft.com/.

Serach for mscorlib, go to System.String, serach for Replace and look: http://referencesource.microsoft.com/#mscorlib/system/string.cs,69fc1d0aa6df8a90,references

Tourmaline answered 9/9, 2016 at 5:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.