简体   繁体   English

使用ComVisible导出到COM时是否可以隐藏类的父级?

[英]Is it possible to hide a class parent when exported to COM with ComVisible?

In a C# class library (.Net 3.5), I have a very simple class (some string and long properties, no methods) which inherit from System.Web.Services.Protocols.SoapHeader . 在C#类库(.Net 3.5)中,我有一个非常简单的类(一些字符串和长属性,没有方法),该类继承自System.Web.Services.Protocols.SoapHeader My class is tagged as ComVisible(true) because I need to use it in Delphi. 我的班级被标记为ComVisible(true),因为我需要在Delphi中使用它。

For some unknown reason, this class can't be created in Delphi, when the class interface is automatically exported (aka attribute ClassInterface(ClassInterfaceType.AutoDispatch)). 由于某些未知的原因,当类接口自动导出时(即属性ClassInterface(ClassInterfaceType.AutoDispatch)),无法在Delphi中创建此类。 I made a simple C++ program to test about that, and I get each time the same error: 0x80131509 (Methods parameters or return type cannot be invoked via IDispatch). 我制作了一个简单的C ++程序对此进行测试,并且每次都出现相同的错误:0x80131509(无法通过IDispatch调用方法参数或返回类型)。 After some digging, the problem is directly related to SoapHeader. 经过一番挖掘,问题直接与SoapHeader有关。 When I remove it or use it as property of my class, all run fine. 当我删除它或将其用作类的属性时,一切运行良好。

In C++, when using IDispatch to access my class exported with ClassInterfaceType.None, all work fine. 在C ++中,当使用IDispatch访问通过ClassInterfaceType.None导出的类时,一切正常。 But I can't relay on IDispatch in Delphi, so I'm searching a way to avoid the type library exporter to export SoapHeader stuff. 但是我无法在Delphi中中继IDispatch,因此我正在寻找一种避免类型库导出程序导出SoapHeader内容的方法。

Is there a way to do that ? 有没有办法做到这一点 ? A .Net attribute to inform the type library exporter to avoid exposing a class parent ? 一个.Net属性,以通知类型库导出程序避免暴露类父级? To expose only a part of a class even if there is more stuff ? 即使有更多的东西,也只公开一部分课程?

Declare an interface so you can explicitly name the members that are visible to COM clients. 声明一个接口,以便您可以明确命名对COM客户端可见的成员。 Like this: 像这样:

using System;
using System.Runtime.InteropServices;

[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IThingsIWantToExpose {
    void mumble();
    // etc..
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class MyComClass : SomeBaseClass, IThingsIWantToExpose {
    // Actual implementation of the methods
    public void mumble() { }
    // etc, plus everything else that you want to do in the class
    //...
}

Using ComInterfaceType.InterfaceIsDual allows both early and late binding (IDispatch). 使用ComInterfaceType.InterfaceIsDual可以同时进行早期绑定和后期绑定(IDispatch)。 ClassInterfaceType.None hides the class members so the base class isn't exposed either. ClassInterfaceType.None隐藏类成员,因此也不会公开基类。 Including the System.Object members btw. 包括System.Object成员btw。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM