【发布时间】:2015-07-04 07:16:47
【问题描述】:
question 激起了我的好奇心,我开始寻找 List.Clear() 的实现。当我像这样在System.Collections.dll 上运行ildasm 时:
cd c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5
"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\ildasm.exe" System.Collections.dll
看了System.Collections.Generic.List`1::Clear : void(),发现是这样的:
.method public hidebysig newslot virtual final
instance void Clear() cil managed
{
// Code size 1 (0x1)
.maxstack 8
IL_0000: ret
} // end of method List`1::Clear
这不是我要找的。p>
我知道一些 .NET dll 是一个间接级别,因此可以将不同版本的框架定向到。如果是这种情况,我如何确定实际代码在哪里?或者,如果还有其他事情发生,关于如何使用ildasm 完成此类任务的提示将不胜感激。我更擅长使用ildasm 来查看我自己的东西,但在使用它来检查框架代码方面没有经验。
更新
在正确的方向上使用 Hans 的指针,我最终做了以下事情:
cd C:\Windows\Microsoft.NET\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089
"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\ildasm.exe" mscorlib.dll
这让我看到System.Collections.Generic.List`1::Clear : void() 上的 Clear() 方法的以下内容:
.method public hidebysig newslot virtual final
instance void Clear() cil managed
{
.custom instance void __DynamicallyInvokableAttribute::.ctor() = ( 01 00 00 00 )
// Code size 49 (0x31)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldfld int32 class System.Collections.Generic.List`1<!T>::_size
IL_0006: ldc.i4.0
IL_0007: ble.s IL_0022
IL_0009: ldarg.0
IL_000a: ldfld !0[] class System.Collections.Generic.List`1<!T>::_items
IL_000f: ldc.i4.0
IL_0010: ldarg.0
IL_0011: ldfld int32 class System.Collections.Generic.List`1<!T>::_size
IL_0016: call void System.Array::Clear(class System.Array,
int32,
int32)
IL_001b: ldarg.0
IL_001c: ldc.i4.0
IL_001d: stfld int32 class System.Collections.Generic.List`1<!T>::_size
IL_0022: ldarg.0
IL_0023: dup
IL_0024: ldfld int32 class System.Collections.Generic.List`1<!T>::_version
IL_0029: ldc.i4.1
IL_002a: add
IL_002b: stfld int32 class System.Collections.Generic.List`1<!T>::_version
IL_0030: ret
} // end of method List`1::Clear
所以这导致我查看System.Array::Clear(),它给了我:
.method public hidebysig static void Clear(class System.Array 'array',
int32 index,
int32 length) cil managed internalcall
{
.custom instance void System.Security.SecuritySafeCriticalAttribute::.ctor() = ( 01 00 00 00 )
.custom instance void System.Runtime.ConstrainedExecution.ReliabilityContractAttribute::.ctor(valuetype System.Runtime.ConstrainedExecution.Consistency,
valuetype System.Runtime.ConstrainedExecution.Cer) = ( 01 00 03 00 00 00 02 00 00 00 00 00 )
.custom instance void __DynamicallyInvokableAttribute::.ctor() = ( 01 00 00 00 )
} // end of method Array::Clear
这又是一条死胡同,但至少我现在知道去哪里获取此类信息了。
【问题讨论】:
-
这不是一个完全的死胡同。为.NET Core 5 调用的Here is the native CLR 代码。它不是 .NET Framework,但我敢打赌它会非常接近。如果你想要 2.0 CLR 的源代码,你可以得到它here
-
另外如果你不了解,一定要看看referencesource.microsoft.com,虽然不像看IL那样“具体细节”,但应该还算准确。
-
@ScottChamberlain,非常感谢。你们的两个cmets都给我指出了我不知道的地方。