C#中暴露COM - 接口继承 [英] C# exposing to COM - interface inheritance

查看:319
本文介绍了C#中暴露COM - 接口继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个类BaseClass的实现IBaseClass

Say I have a class BaseClass that implements IBaseClass

然后,我有一个接口的iCLASS继承IBaseClass。

Then I have an interface IClass that inherits IBaseClass.

然后,我有一个实现的iCLASS一类命名的类。

Then I have a class named class that implements IClass.

例如:

[ComVisible(true), InterfaceType(ComInterfaceType.IsDual), Guid("XXXXXXX")]
public interface IBaseClass
{
  [PreserveSig]
  string GetA()
}

[ComVisible(true), InterfaceType(ComInterfaceType.IsDual), Guid("XXXXXXX")]  
public interface IClass : IBaseClass
{
  [PreserveSig]
  string GetB()
}

[ComVisible(true), ClassInterface(ClassInterfaceType.None), Guid("XXXXXXX")]
public class BaseClass : IBaseClass
{
  public string GetA() { return "A"; }
}

[ComVisible(true), ClassInterface(ClassInterfaceType.None), Guid("XXXXXXX")]
public class Class : BaseClass, IClass
{
  public string GetB() { return "B"; }
}

当暴露在COM,如果我做类的一个实例,它不会允许我这样称呼木屐()。

When exposing to COM, if I make an instance of "Class" it does not allow me to call GetA().

在看我的IDL在.tlb文件,我的iCLASS界面如下:

When looking my IDL in the .tlb file, my IClass interface looks like:

[
  odl,
  uuid(XXXXXXXXXXXXXXXXXXXXX),
  version(1.0),
  dual,
  oleautomation,

]
interface IClass : IDispatch {
    [id(0x60020000)]
    BSTR GetB();
}

它甚至没有模样的iCLASS从IBaseClass派生!

It doesn't even look like IClass derives from IBaseClass!

如果我冒了出来,其中的iCLASS源于IBaseClass,只是方法​​添加到界面,它的工作原理。

If I take out where IClass derives from IBaseClass and just add the methods to the interface, it works.

我怎样才能让C#实现这个继承COM?我宁愿没有重新实现接口的时候我能继承他们。

How can I make C# enable this inheritance in COM? I'd rather not re-implement interfaces when I can inherit them.

废话:检查此链接<一个href="http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/7313191a-10db-4a16-9cdd-de9fb80b378a/">.Net COM限制

如果有人有一个答案,这是为什么,甚至比复制粘贴到我的派生接口更好的解决办法,让我知道。如果没有,我会标志着几天的答案。

If someone has an answer to why this is, or a better workaround than copy-paste to my "derived" interface, let me know. If not, I'll mark an answer in a couple days.

推荐答案

这是不是一个.NET的问题,事情是这样的COM工程的结果。它不支持继承。你会解决这个问题在客户端,它需要调用QueryInterface()的IID为IBaseClass得到一个接口指针IBaseClass接口,以便它可以调用木屐()。 .NET互操作自动提供一个QI实施,使这项工作。然而,这是不是很方便用户在这种情况下,设计你的C#方code,以方便客户端使用的另一种方式的类,而不是周围。您通常会需要一个班轮覆盖方法是委托给基C#类实现。

This is not a .NET problem, it is a consequence of the way COM works. It doesn't support inheritance. You would fix this at the client side, it needs to call QueryInterface() with the IID for IBaseClass to get an interface pointer to the IBaseClass interface so it can call GetA(). .NET interop automatically provides a QI implementation that makes this work. However, it is not very user-friendly in this case, design your C# side code to make it easy for the client to use your class instead of the other way around. You'd typically need a one-liner override method that delegates to the base C# class implementation.

请注意,有一个与你的方法签名问题,使用[preserveSig]属性的。他们不是调用通过IDispatch也不能进行自动编组。需要与一个HRESULT作为返回值类型的方法。当你删除属性它是自动的。

Note that there's a problem with your method signatures and the use of the [PreserveSig] attribute. They are not callable through IDispatch nor can they be auto-marshaled. That requires a method with a HRESULT as the return value type. It is automatic when you remove the attribute.

这篇关于C#中暴露COM - 接口继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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