C++/CLI:如何在C++/CLI中使用CallerMemberNameAttribute



在C#和VB.Net中,我可以使用CallerMemberNameAttribute以字符串形式获取调用程序的名称:

public void Caller([CallerMemberName]string memberName = "")
{
   Debug.Print(memberName);
}

我想在C++/CLI中也这样做,但不知怎么的,我无法使它正常工作。尝试了几种构造,我开始怀疑C++/CLI编译器是否支持此属性。

这里有一个(简化的)实现:

using namespace System::Runtime::CompilerServices;
public ref class InvokeExample
{
    Invoke([CallerMemberName][Optional]String^ name)
    {
        Debug::Print(name);
    }
}

在C#应用程序中调用此方法时,name的值为null。还尝试了属性DefaultParameterValue,但也没有帮助。现在没什么想法了。

显而易见的答案是,为什么不在C#中实现呢?好吧,在这种特定的情况下,我仅限于C++/CLI。

我使用reflector查看C++/CLI和C#/VB.Net版本之间的差异,它们看起来完全相同。

然后使用ILDASM,现在我想我知道为什么它不起作用了(看完这篇文章后)。

这是识别码:

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项目的这些限制,那将非常方便。会为我们节省很多时间。

最新更新