简体   繁体   English

C#中ComVisible类中的接口继承

[英]Interface inheritance in ComVisible classes in C#

Inherited property (P1) is not accessable from w/cscript. 无法从w / cscript访问继承的属性(P1)。

Class structure looks something like this : 类结构看起来像这样:

[ComVisible]
public interface IA 
{
     string P1{get;} 
} 

[ComVisible]
public interface IB : IA
{
    string P2{get;} 
}

[ComVisible]
public abstract class Base : IA
{
    public string P1{get{return "somestring";}}
}   

[ComVisible]
public class Concrete : Base, IB
{
   public string P2{get{return "P2somestring";}}
}

Client code in js file : js文件中的客户端代码:

try{
var obj = new ActiveXObject("Concrete");
WshShell.Popup(obj.P1); //<-- displays empty string
}catch(e)
{
WshShell.Popup(e.description);
}

if i add property P1 to interface IB, everything works fine, but whats the point of inheritance then? 如果我将属性P1添加到接口IB,一切正常,但那么继承点是什么呢? Or am i doing here something really wrong? 或者我在这里做错了什么?

I'm stealing the answer to this from the COM Interop: Base class properties not exposed to COM link given in the very similar question "C# exposing to COM - interface inheritance" 我正在从COM Interop窃取这个问题的答案在类似的问题“C#暴露给COM - 接口继承”中给出的没有暴露给COM链接的基类属性

In particular the MVP on that site states: 特别是该网站上的MVP声明:

In COM interfaces can inherit from one another. 在COM接口中可以相互继承。 However the .NET implementation that exposes the .NET interface to COM does not support inheritance. 但是,向.NET公开.NET接口的.NET实现不支持继承。 Therefore you must replicate any interface members in a base interface to the derived interface... The interop code does not look at base interface types when building the exposed COM interface. 因此,您必须将基接口中的任何接口成员复制到派生接口...在构建公开的COM接口时,互操作代码不会查看基接口类型。

It does suggest some workarounds, such as inheriting from both interfaces, or implementing a 'native' TLB (write the inteface in IDL and compile it with MIDL - there should be projects for this in vis studio). 它确实提出了一些解决方法,例如从两个接口继承,或实现“本机”TLB(在IDL中编写接口并使用MIDL编译它 - 在vis studio中应该有这样的项目)。

The code seems fine and it should work as intended except of course that you have omitted the return type of the two properties, they should be: 代码似乎很好,它应该按预期工作,除了你已经省略了两个属性的返回类型,它们应该是:

[ComVisible]
public abstract class Base : IA
{
    public string P1{get{return "somestring";}}
}   

[ComVisible]
public class Concrete : Base, IB
{
   public string P2{get{return "P2somestring";}}
}

But I am assuming this is just an overlook in the code you wrote in your post. 但我认为这只是你在帖子中写的代码中的一个忽略。

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

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