【问题标题】:decompile .NET Replace method (v4.6.1)反编译 .NET Replace 方法 (v4.6.1)
【发布时间】:2023-03-25 02:47:01
【问题描述】:

我想看看

public String Replace(String oldValue, String newValue);

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

我用 dotPeek 反编译了 mscorlib.dll,在方法内部有一个对 ReplaceInternal 方法的调用,但我找不到它

string s = ReplaceInternal(oldValue, newValue);

我什至在 GIT 的开源 .NET Core 上搜索过这种方法,但没有运气。

请解释一下这个方法在哪里,里面是什么?

【问题讨论】:

标签: c# .net string replace decompiler


【解决方案1】:

看看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

【讨论】:

  • 非常感谢,但是“another.dll”在哪里?我想看看里面..或者他叫什么名字?
【解决方案2】:

外部 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)
【解决方案3】:

要了解该函数的工作原理,请查看http://referencesource.microsoft.com/ 下的参考源。

搜索mscorlib,转到System.String,搜索Replace 并查看:http://referencesource.microsoft.com/#mscorlib/system/string.cs,69fc1d0aa6df8a90,references

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    • 2021-10-20
    • 2010-09-20
    • 2014-06-26
    相关资源
    最近更新 更多