COM互操作(CCW)中的重载 - IDispatch名称包括后缀(_2,_3等) [英] Overloads in COM interop (CCW) - IDispatch names include suffix (_2, _3, etc)

查看:285
本文介绍了COM互操作(CCW)中的重载 - IDispatch名称包括后缀(_2,_3等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含几个类的托管程序集,这些类有重载的方法。我通过

将程序集暴露给COM / IDispatch调用者

  [ComVisible(true)] 

..并且在程序集上设置正确的Guid。我没有为COM互操作定义一个显式接口。这一切都是动态的。我在受管的DLL上运行 regasm.exe / codebase ,并为COM互操作注册它。



当我运行OleView时,我可以看到程序集中各种类的ProgId。但是,浏览这些ProgIds,并扩展IDispatch节点,这些类没有TypeLib信息。



即使如此,从脚本中,我可以调用一个接受零参数的方法或接受一个参数的方法。如果还有一个重载接受多个参数,我不能通过名称调用该方法。我一直收到的错误是

  Microsoft VBScript运行时错误:错误的参数数或无效的属性赋值:< methodname& 

从这里我理解COM / IDispatch客户端不能正确解析暴露对象的重载方法通过COM互操作。






然后我添加了

  [ClassInterface(ClassInterfaceType.AutoDual)] 

。在DLL上的 regasm.exe 后,我可以在IDispatch节点下看到每个方法的typelib信息。



我发现重载方法会自动获取包含附加后缀的名称。 MethodX将在自动生成的typelib程序集中暴露重载,如MethodX,MethodX_2,MethodX_3等。



我发现通过引用带有这些后缀的方法名,我可以调用重载的方法,尽管不是使用通用名。



更有趣的是,如果我从类中删除 [ClassInterface(ClassInterfaceType.AutoDual)] 仍然以这种方式调用重载的方法,从而避免错误的参数数或无效的属性赋值错误。



我的问题是:这种行为 - 附加数字后缀到成员名称 - 稳定?记录?可信?

解决方案

COM不支持方法重载,因此.NET COM Interop层必须即兴。我不知道如果名称mangling如你所描述的任何地方,但即使是,我不认为使用它是一个好主意 - 这仍然是COM用户的API非常不方便。如果你想把你的类暴露给COM,最好的方法是写一个不同的COM友好的 [ComVisible] 接口,并隐藏类本身。以COM友好的方式处理重载的正确方法是使用一个具有一些 [可选] 参数的方法(并委托到相应的.NET重载)。


I have a managed assembly containing a few classes, and those classes have overloaded methods. I expose the assembly to COM/IDispatch callers via

[ComVisible(true)]

..and also setting the proper Guid, on the assembly itself. I do not define an explicit interface for the COM interop. It's all done dynamically. I run regasm.exe /codebase on the managed DLL and it registers it for COM interop.

When I run OleView, I can see the ProgId's of the various classes in the assembly. But, browsing into those ProgIds, and expanding IDispatch node, there is no TypeLib information for these classes.

Even so, from a script, I can invoke a method that accepts zero arguments or a method that accepts one argument. If there is also an overload that accepts more than one argument, I cannot invoke that method by name. The error I get, consistently, is

Microsoft VBScript runtime error: Wrong number of arguments or invalid property assignment:  <methodname>

From this I understood that COM/IDispatch clients were not able to properly resolve overloaded methods on an object exposed via COM interop.


Then I added

[ClassInterface(ClassInterfaceType.AutoDual)]

...to each of the classes in question. After regasm.exe on the DLL, I can see typelib information for each method, under the IDispatch node.

What I found is that overloaded methods automatically get a name that includes an appended suffix. MethodX will expose overloads in the auto-generated typelib assembly as MethodX, MethodX_2, MethodX_3, and so on.

And I found that by referencing the method names with those suffixes, I could invoke overloaded methods, although not with the common name.

More interestingly, if I then removed the [ClassInterface(ClassInterfaceType.AutoDual)] from the classes, I could still invoke the overloaded methods in this way, thus avoiding the Wrong number of arguments or invalid property assignment error.

My question is: is this behavior - appending numeric suffixes to the member names - stable? documented? dependable?

解决方案

COM does not support method overloading, so .NET COM Interop layer has to improvise. I'm not sure if name mangling as you described as documented anywhere, but even if it is, I don't think that using it is a good idea - it's still pretty inconvenient API for COM users. If you want to expose your classes to COM, the best way is to write a distinct COM-friendly [ComVisible] interface, and hide the class itself. The correct way to handle overloads in a COM-friendly way would be have a single method with some [Optional] arguments (and delegate to your corresponding .NET overloads).

这篇关于COM互操作(CCW)中的重载 - IDispatch名称包括后缀(_2,_3等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆