我试图通过反射找到界面授予我的所有方法.我有一个类型数组,我验证只有接口,从那里我需要提取所有方法.不幸的是,如果我做类似的事情(IList).GetMethods()它只返回IList上的方法而不是ICollection上的方法,或者IEnumerable我尝试了以下linq查询,但它不返回外部接口上找到的方法.如何修复查询?
from outerInterfaces in interfaces
from i in outerInterfaces.GetInterfaces()
from m in i.GetMethods()
select m
Run Code Online (Sandbox Code Playgroud)
如果这是SQL,我可以做一些类似于带有union all的递归CTE,但我不认为C#中存在这样的语法.有人可以帮忙吗?
这样的事情怎么样:
typeof(IList<>).GetMethods().Concat(typeof(IList<>)
.GetInterfaces()
.SelectMany(i => i.GetMethods()))
.Select(m => m.Name)
.ToList().ForEach(Console.WriteLine);
Run Code Online (Sandbox Code Playgroud)
编辑:回应评论.
使用以下代码对其进行测试:
public interface IFirst
{
void First();
}
public interface ISecond : IFirst
{
void Second();
}
public interface IThird :ISecond
{
void Third();
}
public interface IFourth : IThird
{
void Fourth();
}
Run Code Online (Sandbox Code Playgroud)
测试代码:
typeof(IFourth).GetMethods().Concat(typeof(IFourth)
.GetInterfaces()
.SelectMany(i => i.GetMethods()))
.Select(m => m.Name)
.ToList().ForEach(Console.WriteLine);
Run Code Online (Sandbox Code Playgroud)
输出是:
Fourth
Third
Second
First
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
852 次 |
| 最近记录: |