gpt4 book ai didi

.net - 如何在基类库中找到实现特定接口(interface)的类型列表?

转载 作者:行者123 更新时间:2023-12-02 02:27:20 25 4
gpt4 key购买 nike

有时我想找出实现特定接口(interface)的所有标准 .NET 类型的列表。通常是出于好奇,有时也有一些实际目的(但这不是重点)。

我试图从 MSDN 中获取它,但是类型的页面只包含类型的子链接,而不是实现接口(interface)的类型。

您知道如何执行此操作的任何技巧(或有帮助的工具)吗?

我写了这段代码(ICollection 是正在调查的类型):

        var result =
from assembly in AppDomain.CurrentDomain.GetAssemblies().ToList()
from type in assembly.GetTypes()
where typeof(ICollection).IsAssignableFrom(type)
select type;

foreach (var type in result)
{
Console.Out.WriteLine(type.FullName);
}

但这有一些限制:

  1. 它只搜索当前加载的程序集。
  2. 我想不出一种方法来为通用接口(interface)执行此操作(ICollection<> 行不通)。
  3. 如果它提供指向 MSDN 的链接就更好了(但我想这可以修复)。

感谢您的帮助!

最佳答案

It only searches currently loaded assemblies.

始终存在“添加引用”对话框,但您可能想查看以下问题:List all available .NET assemblies .

I couldn't figure out a way to do this for generic interfaces (ICollection<> wouldn't work)

试试这个查询:

from assembly in AppDomain.CurrentDomain.GetAssemblies()
from type in assembly.GetTypes()
where type.GetInterfaces()
.Any(i => i.IsGenericType
&& i.GetGenericTypeDefinition() == typeof(ICollection<>))
select type;

It would be nice if it provided links to MSDN.

.NET Reflector支持搜索实现接口(interface)的类型(在类型下选择“派生类型”)以及在 MSDN 中搜索类型文档(右键单击类型并选择“搜索 MSDN”)。

如果您不喜欢该选项,您当然可以尝试编写一些东西,使用该类型的全限定名称在 MSDN 上运行网络搜索。我不知道是否有任何元数据可以将类型映射到其 MSDN 页面或实现该目的的简洁方法。

关于.net - 如何在基类库中找到实现特定接口(interface)的类型列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5398382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com