我使用反射器查看了 C++/CLI 和 C#/VB.Net 版本之间的差异,它们看起来完全一样。
然后使用了 ILDASM,现在我想我知道它为什么不起作用了(在阅读了这篇文章之后)。
这是 il 代码:
C++/CLI
.method public hidebysig instance string
Caller([opt] string methodName) cil managed
{
.param [1]
.custom instance void [System]System.Runtime.InteropServices.DefaultParameterValueAttribute::.ctor(object) = ( 01 00 0E 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.CallerMemberNameAttribute::.ctor() = ( 01 00 00 00 )
// Code size 2 (0x2)
.maxstack 1
IL_0000: ldarg.1
IL_0001: ret
} // end of method ClassCPP::Caller
C#
.method public hidebysig instance string
Caller([opt] string methodName) cil managed
{
.param [1] = ""
.custom instance void [mscorlib]System.Runtime.CompilerServices.CallerMemberNameAttribute::.ctor() = ( 01 00 00 00 )
// Code size 2 (0x2)
.maxstack 1
.locals init ([0] string CS$1$0000)
IL_0000: ldarg.1
IL_0001: ret
} // end of method ClassCS::Caller
来自 VB.Net 的 IL 代码与 C# 的不同之处如下:
.param [1] = nullref
我怀疑因为 C++/CLI 发出 DefaultParameterValue 而不是使用默认值初始化 .param[1] ,所以 C# 编译器不会将该值转换为调用者成员名称。
如果 MSDN 页面描述了 C++/CLI 项目的此类限制,将会很方便。会为我们节省很多时间。