Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this object's type show no interfaces via reflection, despite implementing at least two?

First, an example of something that works as expected: (all code was executed in VS2008 immediate window)

25 is IComparable
>> true

25.GetType().GetInterfaces()
>> {System.Type[5]}
>>   [0]: {Name = "IComparable" FullName = ...
>>   [1]: {Name = "IFormattable" FullName = ...
>>   ...

So far so good. Now let's try on an object where the interface is inherited via a base type:

class TestBase : IComparable
{
    public int CompareTo(object obj) { throw new NotImplementedException(); }
}

class TheTest : TestBase { }

In the immediate window:

(new TheTest()) is IComparable
>> true

(new TheTest()).GetType().GetInterfaces()
>> {System.Type[1]}
>>   [0]: {Name = "IComparable" FullName = "System.IComparable"}

No surprises here either. How come the following code doesn't show any interfaces then:

wcfChannel = ChannelFactory<IMyServiceApi>.CreateChannel(binding, endpointAddress);

wcfChannel is IMyServiceApi && wcfChannel is ICommunicationObject
>> true

typeof(IMyServiceApi).IsInterface && typeof(ICommunicationObject).IsInterface
>> true

wcfChannel.GetType().GetInterfaces()
>> {System.Type[0]}

How can all of the above be true at the same time?

(edit: added wcfChannel is ICommunicationObject above, which is at this time unexplained by the answer that explains how wcfChannel is IMyServiceApi is true.)

like image 607
Roman Starkov Avatar asked Mar 02 '10 16:03

Roman Starkov


People also ask

How do I know if I have generic type implements interface TypeScript?

Use a user-defined type guard to check if an object implements an interface in TypeScript. The user-defined type guard consists of a function, which checks if the passed in object contains specific properties and returns a type predicate.

What is reflection in programming C#?

Reflection provides objects (of type Type) that describe assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.

Is interface a type in C#?

Interfaces define properties, methods, and events, which are the members of the interface. Interfaces contain only the declaration of the members. Some of the interface types in C# include. IEnumerable − Base interface for all generic collections.

Which keyword is used to check if a class satisfies a particular interface?

The java instanceof operator is used to test whether the object is an instance of the specified type (class or subclass or interface).


1 Answers

It's because the type of wcfChannel is the interface itself:

>> channel.GetType().FullName
"MyService.IMyServiceApi"

>> channel.GetType().IsInterface
true

>> channel.GetType() == typeof(IMyServiceApi)
true

.GetInterfaces() returns only the interfaces inherited or implemented, but not the interface itself.

Admittedly, it is unusual for an object instance to actually be of an interface type, but as you mentioned in your comment on the question, the object is actually a transparent proxy. It makes sense for that proxy to be agnostic of the actual interface implementation, and care only about the interface. The fact that .GetType() returns the interface makes the proxy as transparent as possible.

like image 75
Timwi Avatar answered Nov 10 '22 19:11

Timwi