重载的COM互操作(CCW) - IDispatch的名字包括后缀(_2,_3等)后缀、名字、操作、CCW

由网友(信命卜认命)分享简介:我有一个包含了几类托管程序集,而这些类重载的方法。我揭露大会COM / IDispatch的呼叫者通过I have a managed assembly containing a few classes, and those classes have overloaded methods. I expose the...

我有一个包含了几类托管程序集,而这些类重载的方法。我揭露大会COM / IDispatch的呼叫者通过

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)]

..而且还设置了适当的Guid,在大会本身。我不定义一个明确的接口的COM互操作。它的所有动态完成的。我跑 regasm.exe / codeBase的的托管DLL并为其注册COM互操作。

..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.

当我运行OLEVIEW,我可以看到的ProgID的各类别中的装配。但是,浏览到这些的ProgID,扩大的IDispatch节点,没有类型库信息为这些类。

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>

从此我明白了COM / IDispatch的客户端不能够妥善解决,通过COM互操作暴露在对象上重载方法。

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

然后我说

[ClassInterface(ClassInterfaceType.AutoDual)]

...每个班有问题的。经过 regasm.exe 的DLL,我可以看到类型库信息的每一个方法,在IDispatch的节点。

...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.

我发现的是,重载方法自动获得,包括后缀的名称。 MethodX会暴露在过载自动生成的类型库组件,MethodX,MethodX_2,MethodX_3,等等。

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.

更有趣的是,如果我再删除了 [ClassInterface(ClassInterfaceType.AutoDual)] 从类,我可以的还是的调用重载方法这样一来,从而避免了数量的参数或无效的属性赋值错误错误。

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不支持方法重载,因此.NET COM互操作层凑合。我不知道,如果像你描述的记录任何地方名称重整,但即使是这样,我不认为使用它是一个好主意 -​​ 它仍然是pretty的不便API为COM用户。如果你想你的类暴露给COM,最好的办法是写一个不同的COM友好 [标记有​​ComVisible特性] 接口,并隐藏类本身。正确的方法来处理过载在COM友好的方式将有一定 [可选] 参数一个方法(和委派给您相应的.NET重载)。

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).

阅读全文

相关推荐

最新文章