Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#接口继承_C#_Reflection_Inheritance_Interface - Fatal编程技术网

C#接口继承

C#接口继承,c#,reflection,inheritance,interface,C#,Reflection,Inheritance,Interface,鉴于: 原因: ? 我想说清楚: typeof(IB).GetMethods().Count() == 0; 工作(返回5) 作为奖励: public class A { public void TestMethod() { } } public class B : A { } typeof(B).GetMethods().Count(); 您必须在GetMethods()中定义一些Bindingflags 试一试 这似乎是GetMethods函数的设计。它不支持

鉴于:

原因:

?

我想说清楚:

typeof(IB).GetMethods().Count() == 0;
工作(返回5)

作为奖励:

public class A
{
    public void TestMethod()
    {
    }
}

public class B : A
{
}

typeof(B).GetMethods().Count();

您必须在GetMethods()中定义一些Bindingflags

试一试


这似乎是GetMethods函数的设计。它不支持接口中的继承成员。如果要发现所有方法,需要直接查询每个接口类型


查看的社区内容部分。

以下是获取IA和IB计数的代码:

typeof(IB).GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).Count();
请注意,在生产代码中,我不会使用
GetInterfaces()[0]
,因为在我使用此功能的代码中,我不能假设我总是至少有一个接口

我还尝试了bindingflags,如下所示:

var ibCount = typeof(IB).GetMethods().Count(); // returns 0
var iaCount = typeof (IB).GetInterfaces()[0].GetMethods().Count(); // return 1

但是,这仍然会返回0,因为接口
IB
仍然没有实现方法
TestMethod()
。接口
IA
do。如果
IA
IB
都是类,那么使用绑定标志就可以了。但是,在这种情况下,返回值为5。不要忘记IA隐式派生自类
对象

将IA视为IB的接口,而不是其基础。

我使用了BindingFlags,因为它们没有帮助;)。绑定标志在这种情况下没有帮助。当涉及到课程时,他们会很有帮助。谢谢你的回答。我知道我可以迭代接口并从中获取方法。我的问题是为什么我不能那样做。
typeof(IB).GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).Count();
var ibCount = typeof(IB).GetMethods().Count(); // returns 0
var iaCount = typeof (IB).GetInterfaces()[0].GetMethods().Count(); // return 1
const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy;
var ibCount = typeof(IB).GetMethods(bindingFlags).Count();