我想看看如何
public String Replace(String oldValue, String newValue);
mscorlib.dll(System.String)中的方法可以正常工作。
我使用dotPeek反编译了mscorlib.dll,并且在方法内部调用了ReplaceInternal方法,我无法找到它
string s = ReplaceInternal(oldValue, newValue);
我甚至在GIT的开源.NET Core上搜索了这个方法,但没有运气。
请说明这种方法在哪里以及里面是什么?
答案 0 :(得分:4)
看一眼here,您会注意到这一点:
// 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);
此方法在外部实施的extern
关键字means,在另一个dll中。
话虽这么说,它甚至可以用这个模块使用的非托管dll(很可能是C ++)编写。因此,您不能像通常使用托管代码那样反编译此代码或查看代码。
<强>更新强>
经过一番搜索后,我在coreclr项目中找到了相应的代码:
https://github.com/dotnet/coreclr/blob/master/src/classlibnative/bcltype/stringnative.cpp
答案 1 :(得分:4)
extern 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)
答案 2 :(得分:0)
要了解该功能的工作原理,请查看http://referencesource.microsoft.com/下的参考来源。
Serach for mscorlib
,转到System.String
,搜索Replace
并查看:http://referencesource.microsoft.com/#mscorlib/system/string.cs,69fc1d0aa6df8a90,references