如何做到的基础接口方法适当的反思 [英] How to do proper Reflection of base Interface methods

查看:121
本文介绍了如何做到的基础接口方法适当的反思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2接口和2班,我通过反射调查:

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


  • IParent

  • ICHILD - 从IParent派生


  • 儿童 - 从父派生

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

我奇怪的是,当我看着通过ICHILD的反射式我没有找到IParent方法。适用于儿童型

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);
}

请解释这种行为?

有任何解决方法,以反映该派生接口的类基础接口的方法?

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

推荐答案

虽然,我们使用的接口上进行同样的方式,我们使用继承(:);接口是不能继承的;它们将被执行。在这种情况下; 。继承混淆实施,因为它们都使用相同的操作符(:)中定义的

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 (":").

作为总结;
IA:IB A:IA 表示;实现IA任何类,应执行IB。在这种情况下; ,一个应执行IA和IB

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 表示一个类继承B级; 。它没有实现

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

混乱这里使用​​相同的操作符(:)派生的。

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

选中该页面接口继承

这篇关于如何做到的基础接口方法适当的反思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆