Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface inheritance in ComVisible classes in C#

Tags:

c#

interface

com

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?

like image 900
Saudargas Avatar asked Sep 09 '09 14:09

Saudargas


People also ask

CAN interfaces inherit from classes?

A class or struct can implement multiple interfaces. A class can inherit a base class and also implement one or more interfaces.

How many interface can a class inherit?

Implementing Multiple Interfaces. Although classes can inherit only one class, they can implement multiple interfaces. In the example above, we notice the use of the keyword implements to inherit from an interface.

What is the difference between class inheritance and interface inheritance?

Inheritance is the mechanism in java by which one class is allowed to inherit the features of another class. Interface is the blueprint of the class. It specifies what a class must do and not how.

Does interface use inheritance?

Interface inheritance refers to an interface inheriting from one or more other interfaces. A notable difference from class inheritance is that while a class can only directly inherit from one other class, an interface can inherit from multiple interfaces.


2 Answers

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

like image 101
bacar Avatar answered Nov 05 '22 07:11

bacar


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.

like image 37
Robban Avatar answered Nov 05 '22 08:11

Robban