问题描述
在C#中我能得到的Visual Studio保持委托的参数名称结果
为例,如果我有:
公共委托无效布拉赫(对象myArg);
公共事件布拉赫富;
然后,当我添加一个方法时,Visual Studio的用户界面自动保持名称和创建方法
无效Form1_Foo(对象myArg);
但是,如果我在C ++ / CLI声明一个代理:
市民:
委托无效布拉赫(对象^ myArg);
事件胡说^富;
它不保留名称和无义的名字创建了一个方法:
无效Form1_Foo(对象A_0)
如何设置有意义的名字在C ++ / CLI参数
修改(由ILDASM结果):
C ++ CLI事件:
。方法公开hidebysig specialname实例无效
调用(对象myArg)CIL管理
{
} //方法布拉赫结束::调用
C#事件:
。方法公开hidebysig newslot虚拟
实例无效调用(对象myArg)运行管理
{
} //方法布拉赫结束::调用
这个问题具有真正的问题不提示,我不能得到一个摄制。但后来意识到了可能发生的,C ++编译器是从其他托管编译器不同,MSIL,它不需要参数在声明命名。那摇出:
命名空间CppClassLibrary {
公共文献类示例{
公众:
委托无效布拉赫(INT,INT,INT,INT);
事件胡说^富;
};
}
生成这个自动生成的事件处理在C#中:
无效test_Foo(INT A_0,A_1 INT,INT A_2,A_3 INT){
抛出新NotImplementedException();
}
看起来像一个灌篮高手解释。您只需忘了名字的委托声明的参数,C ++编译器被强制合成它们才能写出正确的MSIL。很容易修复,当然。
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
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.
这篇关于对于.NET编译C ++ / CLI时,请委托参数名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!