在C#中的ComVisible类中的接口继承 [英] Interface inheritance in ComVisible classes in C#
问题描述
从w / cscript无法访问继承的属性(P1)。
Inherited property (P1) is not accessable from w/cscript.
类结构如下所示:
[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";}}
}
js文件中的客户端代码: p>
Client code in js file :
try{
var obj = new ActiveXObject("Concrete");
WshShell.Popup(obj.P1); //<-- displays empty string
}catch(e)
{
WshShell.Popup(e.description);
}
如果我将属性P1添加到接口IB,一切正常,
但是什么是继承点呢?或者我在这里做了一些真的错误?
if i add property P1 to interface IB, everything works fine, but whats the point of inheritance then? Or am i doing here something really wrong?
推荐答案
我偷了这个从 COM Interop:不暴露给COM的基类属性链接C#暴露给COM - 接口继承
特别是该网站上的MVP声明:
In particular the MVP on that site states:
在COM接口中可以相互继承。然而,.NET实现暴露.NET接口到COM不支持继承。因此,您必须将基本接口中的任何接口成员复制到派生接口...在构建暴露的COM接口时,Interop代码不会查看基本接口类型。
In COM interfaces can inherit from one another. However the .NET implementation that exposes the .NET interface to COM does not support inheritance. 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.
它建议一些解决方法,例如从两个接口继承,或实现一个本地TLB(在IDL中编写界面并使用MIDL编译它 - 在vis studio中应该有这样的项目) 。
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).
这篇关于在C#中的ComVisible类中的接口继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!