Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do proper Reflection of base Interface methods

I have 2 interfaces and 2 classes that I investigate via Reflection:

  • IParent
  • IChild - derives from IParent
  • Parent
  • Child - derives from Parent

Strange thing for me is the fact that when I look through reflection on IChild type I don't find IParent method.

Same code applied to Child type works as expected - reflection shows Parent method.

interface IParent
{
     void ParentMethod();
}

interface IChild : IParent
{
     void ChildMethod();
}

class Parent 
{
     public void ParentMethod(){}
}

class Child : Parent
{
     public void ChildMethod(){}
}

void Main()
{
    //investigate derived interface
     Type t = typeof(IChild);
     var info = t.GetMethod("ChildMethod");//ok
     Console.WriteLine(info); 
     info = t.GetMethod("ParentMethod");//returns null!
     Console.WriteLine(info); 
     //investigate derived class
     t = typeof(Child);
     info = t.GetMethod("ChildMethod");//ok
     Console.WriteLine(info);
     info = t.GetMethod("ParentMethod");//ok
     Console.WriteLine(info);
}

Please explain such behaviour?

Is there any workaround to reflect base interface's methods from the derived interface's type?

like image 430
Konstantin Chernov Avatar asked May 11 '12 12:05

Konstantin Chernov


People also ask

Can you derive an interface from a base interface?

Interfaces can inherit from one or more interfaces. The derived interface inherits the members from its base interfaces. A class that implements a derived interface must implement all members in the derived interface, including all members of the derived interface's base interfaces.

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.

Why interface has no method body?

Interfaces in contrast only state the name and arguments of methods in sort of a contract. How an implementing class implements those methods is up to the class, thus the interface cannot provide behaviour since it doesn't have an implementation of the method.

CAN interface have internal methods?

Access modifiers such as “public” and “internal” are not allowed for interface members. That's because the access modifier for the interface itself determines the access level for all members defined in the interface. Hence, adding an access modifier to an interface member would be redundant.


2 Answers

If you are dealing with an interface, use

t.GetInterfaces()

then you can check for methods on the types returned above.

Finding interface members by name is not relyable, be mindful that whilst in C# interface members cannot be renamed on implementation, in the CLR the names may be modified. (IDisposable.Dispose() is sometimes renamed to Close). In il there is an instruction called .implements that allows one to change names. I believe VB.Net also has this feature.

like image 27
MaLio Avatar answered Oct 12 '22 01:10

MaLio


Although, we use the interfaces as the same way we use inheritance (":"); interfaces are not inherited; they are to be implemented. In such a case; inheritance is confused with implementation since they are defined using the same operator (":").

As a summary; IA : IB and A:IA means; any class implementing IA, shall implement IB. In this case; A shall implement IA and IB.

A:B means A class inherits B class; it does not implement.

The confusion here derives from using the same operator (":").

Check this page interface inheritance

like image 128
daryal Avatar answered Oct 12 '22 01:10

daryal