观察 ILSpy
中的 System.String
类,我发现了以下索引器:
public extern char this[int index]
{
[__DynamicallyInvokable, SecuritySafeCritical]
[MethodImpl(MethodImplOptions.InternalCall)]
get;
}
该索引器通过索引从字符串中检索字符并限制其 setter (我认为使其不可变)。但这个 getter 是如何工作的呢? 它从哪里获取字符?调试器如何自动实现这个索引器 getter?
最佳答案
description MethodImplOptions.InternalCall
状态:
The call is internal, that is, it calls a method that is implemented within the common language runtime.
因此,getter 的实现方式取决于您使用的运行时(.NET 2.0、.NET 4.0,...),该实现不是包含 System.String 的库的一部分.
如果您对如何在 C# 中实现这样的方法感到好奇,可以查看
关于c# - 字符串索引器如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20993180/