Keep the delegate argument names when compiling C++/CLI for .Net

我的未来我决定 提交于 2019-12-20 05:24:16

问题


In C# I can get Visual Studio to keep the delegate's argument names.
For example if I have:

public delegate void Blah(object myArg);
public event Blah Foo;

Then when I add a method to the event, Visual Studio UI automatically keeps the names and creates the method:

void Form1_Foo(object myArg);

But, if I declare a delegate in C++/CLI:

public:
delegate void Blah(Object^ myArg);
event Blah^ Foo;

it doesn't keep the names and creates a method with nonsense names:

void Form1_Foo(object A_0)

How can I set meaningful names to the argument in C++/CLI ?

EDIT (Added ildasm results):

C++ CLI event:

.method public hidebysig specialname instance void 
        Invoke(object myArg) cil managed
{
} // end of method Blah::Invoke

C# event:

.method public hidebysig newslot virtual 
        instance void  Invoke(object myArg) runtime managed
{
} // end of method Blah::Invoke

回答1:


The question has no hint at the real problem and I could not get a repro. But later realized what might be happening, the C++ compiler is different from other managed compilers, and MSIL, it doesn't require parameters to be named in declarations. That panned out:

namespace CppClassLibrary {
    public ref class Example {
    public:
        delegate void Blah(int, int, int, int);
        event Blah^ Foo;
    };
}

Produces this auto-generated event handler in C#:

    void test_Foo(int A_0, int A_1, int A_2, int A_3) {
        throw new NotImplementedException();
    }

Looks like a slamdunk explanation. You simply forgot to name the parameters in the delegate declaration, the C++ compiler is forced to synthesize them in order to write correct MSIL. Easy to fix of course.



来源:https://stackoverflow.com/questions/25407876/keep-the-delegate-argument-names-when-compiling-c-cli-for-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!