C# exposing to COM - interface inheritance

前端 未结 4 1859
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 15:13

Say I have a class BaseClass that implements IBaseClass

Then I have an interface IClass that inherits IBaseClass.

Then I have a class named class that implem

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-01 15:29

    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.

    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.

提交回复
热议问题