Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
629 views
in Technique[技术] by (71.8m points)

com - Interface inheritance in ComVisible classes in C#

Inherited property (P1) is not accessable from w/cscript.

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 :

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? Or am i doing here something really wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"

In particular the MVP on that site states:

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.

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...