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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…