反编译.NET替换方法(v4.6.1)

Pac*_*fan 6 .net c# string replace decompiler

我想看看怎么样

public String Replace(String oldValue, String newValue);
Run Code Online (Sandbox Code Playgroud)

mscorlib.dll(System.String)中的方法有效.

我用dotPeek反编译了mscorlib.dll,并在方法内部调用了ReplaceInternal方法,我找不到它

string s = ReplaceInternal(oldValue, newValue);
Run Code Online (Sandbox Code Playgroud)

我甚至在GIT的开源.NET Core上搜索了这个方法,但没有运气.

查看我的反编译代码

请解释这种方法在哪里以及里面是什么?

Chr*_*tos 5

看看这里,你会注意到这一点:

// 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);
Run Code Online (Sandbox Code Playgroud)

的extern关键字的装置,该方法是在外部实现,在另一个DLL。

话虽如此,它甚至可能是用非托管 dll 编写的(很可能用 C++ 编写),由该模块使用。因此,您无法反编译或查看此代码,就像您通常对托管代码所做的那样。

更新

经过一番搜索,我在coreclr项目中找到了相应的代码:

https://github.com/dotnet/coreclr/blob/master/src/classlibnative/bcltype/stringnative.cpp


Vee*_*ner 5

外部 C++ 代码在这里。

https://github.com/gbarnett/shared-source-cli-2.0/blob/master/clr/src/vm/comstring.cpp

第 1578 行有

FCIMPL3(Object*, COMString::ReplaceString, StringObject* thisRefUNSAFE, StringObject* oldValueUNSAFE, StringObject* newValueUNSAFE)
Run Code Online (Sandbox Code Playgroud)