I have 2 interfaces and 2 classes that I investigate via Reflection:
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?
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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With