Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Something about .NET inheritance/casting that I don't understand?

See the following simple casting example:

int i = 1000;
object o = (object)i; // cast

i.CompareTo(1000);
o.CompareTo(1000); // error

I understand why the last line generates an error. Unlike ints, objects don't implement IComparable and therefore don't expose the CompareTo method. The following also generates an error:

string s = (string)i; // cast error

Since there's no inheritance between ints and strings, casting will not work here. Now, take a look at this:

AudioRender a = new AudioRender();
IBaseFilter b = (IBaseFilter)a; // cast

a.Run(1000); // error
b.Run(1000);

(These classes come from the DirectShowNet library.)

I don't understand this. The cast does not generate an error and throws no exceptions at runtime, so I assume that AudioRender implements IBaseFilter. However, AudioRender does not expose any of IBaseFilter's methods, indicating that my assumption above is wrong...

If a implements b, why doesn't a expose the methods of b?
Else, if a does not implement b, why can a be casted to b?
Also, can I reproduce this behaviour without the use of DirectShowNet?

like image 573
Rudey Avatar asked Dec 11 '12 10:12

Rudey


People also ask

Which inheritance is not supported in C#?

C# does not support multiple inheritance , because they reasoned that adding multiple inheritance added too much complexity to C# while providing too little benefit. In C#, the classes are only allowed to inherit from a single parent class, which is called single inheritance .

Does .NET support multiple inheritance?

NET does not support Multiple inheritance. Using interfaces we can implement two or more interface contract to a single class which I will…

What is multiple inheritance C#?

In Multiple inheritance, one class can have more than one superclass and inherit features from all its parent classes. As shown in the below diagram, class C inherits the features of class A and B. But C# does not support multiple class inheritance.

Does C# support multilevel inheritance?

Multiple Inheritance isn't supported in C#. To implement multiple inheritances, use Interfaces.


2 Answers

It is likely that AudioRender implements Conversion Operator.

However, having looked at code it seems that both AudioRender and IBaseFilter are Com Imports:

[ComImport, Guid("e30629d1-27e5-11ce-875d-00608cb78066")]
public class AudioRender { }


[ComImport, ("56a86895-0ad4-11ce-b03a-0020af0ba770"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IBaseFilter { .. }

As you can see AudioRender import class does not implement IBaseFilter, so you will not see it in intellisense, but it is likely that underlying COM object implements it, hence why you can cast.

like image 115
Jarek Kardas Avatar answered Oct 17 '22 05:10

Jarek Kardas


It is difficult to tell without access to the documentation of the AudioRender class, but a reasonable guess would be that the implementation of Run on it is a explicit interface implementation.

public AudioRender : IBaseFilter
{
  IBaseFilter.Run(...) {...}
}

That means you can only access the Run method when you access it through a IBaseFilter reference.

like image 14
Oded Avatar answered Oct 17 '22 05:10

Oded