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?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Pacurar Stefan
  • 235
  • 4
  • 9
  • [`ReplaceInternal`](http://referencesource.microsoft.com/#mscorlib/system/string.cs,35ab9efe11757286) ist `extern`, so most likely it is implemented as "unmanaged", native C++ code. See [P/Invoke](https://en.wikipedia.org/wiki/Platform_Invocation_Services) for details. – Uwe Keim Sep 09 '16 at 04:49
  • 2
    btw you dont need to decompile, you can see the source @ http://referencesource.microsoft.com/#mscorlib/system/string.cs,69fc1d0aa6df8a90 – Bob Sep 09 '16 at 04:54

3 Answers3

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)
Cory Nelson
  • 29,236
  • 5
  • 72
  • 110
Veener
  • 4,771
  • 2
  • 29
  • 37
  • 1
    http://www.codeproject.com/Articles/1014073/Fastest-method-to-trim-all-whitespace-from-Strings?fid=1888684&df=90&mpp=25&prof=False&sort=Position&view=Thread&spc=Relaxed&fr=76 – Veener Sep 09 '16 at 05:04
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

Christos
  • 53,228
  • 8
  • 76
  • 108
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

Radinator
  • 1,048
  • 18
  • 58