有没有办法找到哪些 .NET 类实现了某个接口?

发布于 2024-07-29 01:20:03 字数 75 浏览 5 评论 0原文

例如,如果我想查看我的 .NET 选项用于实现 IList 或 IDictionary。 有没有办法找到它,例如在 MSDN 文档中?

For example if I wanted to see what my .NET options were for something implementing IList or IDictionary. Is there a way to find that for example in the MSDN documentation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风向决定发型 2024-08-05 01:20:03

我认为可以使用 Reflector

I think it's possible using Reflector

隔岸观火 2024-08-05 01:20:03

要在 MSDN 中查找它,我通常会转到 Google,输入类似“MSDN IList”的内容,然后访问 IList 接口 其中有一个部分“实现 IList 的类”。 对于任何接口类都是如此。

如果您找到一个基类,例如 DictionaryBase ,将有一个名为派生类的链接,它将带您到显示继承层次结构

To find it in MSDN, I usually go to Google, type something like "MSDN IList", and to get to IList Interface which has a section "Classes that Implement IList". That's true for any of the interface classes.

If you find a base class such as DictionaryBase, there will be a link called Derived Classes which will take you to a tree showing the inheritance hierarchy.

陈独秀 2024-08-05 01:20:03

您也可以通过编程方式执行此操作。

如果您知道类型所在的程序集(我使用 mscorlib 因为它是 string 所在的位置),您可以使用以下方法构建一个列表:

.Net 3.0

List<Type> implementors = 
   Assembly.GetAssembly(typeof(string))
    .GetTypes()
    .Where(type => type.GetInterfaces().Contains(typeof(IList)))
    .ToList();

.Net 2.0

List<Type> implementors = new List<Type>();

foreach (Type type in Assembly.GetAssembly(typeof(string)).GetTypes())
{
    foreach (Type interfaceType in type.GetInterfaces())
    {
        if (interfaceType == typeof(IList))
        {
            implementors.Add(type);
        }
    }
}

implementors 列表将保存实现接口 IListTypes 列表。 您可以将 IList 更改为您想要的任何接口,IDictionaryICollection 等。

编辑:

如果您想将其扩展到所有程序集在当前的 AppDomain 中,您可以这样做:

List<Type> implementors = 
AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assembly => assembly.GetTypes()
                        .Where(type => type.GetInterfaces().Contains(typeof(IList)))
            ).ToList();

这实际上完全取决于您对数据的处理方式。 如果您只是为了个人满意而查看它们,Reflector 将是您最简单的选择 - 特别是如果您想要超越应用程序域中加载的程序集(假设您有一个应用程序开始)。 我想在这种情况下您可以从 GAC 加载所有程序集...但这基本上就是 Reflector 可以,只是你可以单独挑选你想要的。

You can also do this programmatically.

If you know the assembly where the types are located (I'm using mscorlib as that is where string is located) you can build a list using this method:

.Net 3.0

List<Type> implementors = 
   Assembly.GetAssembly(typeof(string))
    .GetTypes()
    .Where(type => type.GetInterfaces().Contains(typeof(IList)))
    .ToList();

.Net 2.0

List<Type> implementors = new List<Type>();

foreach (Type type in Assembly.GetAssembly(typeof(string)).GetTypes())
{
    foreach (Type interfaceType in type.GetInterfaces())
    {
        if (interfaceType == typeof(IList))
        {
            implementors.Add(type);
        }
    }
}

The implementors list will hold a list of of Types that implement the interface IList. You can change IList to any interface you'd like, IDictionary, ICollection, etc.

Edit:

If you want to extend this to all assemblies in the current AppDomain, you could do:

List<Type> implementors = 
AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assembly => assembly.GetTypes()
                        .Where(type => type.GetInterfaces().Contains(typeof(IList)))
            ).ToList();

It really all depends on what you're doing with the data. If you just want to view them for your own personal satisfaction, Reflector is going to be your easiest option - especially if you want to look beyond assemblies loaded in your application domain (assuming you have an application to begin with). I suppose you could load all the assemblies from the GAC in that case... but this is basically what Reflector does except you get to pick and choose which ones you want individually.

不语却知心 2024-08-05 01:20:03

您可以使用此方法查找某种类型实现的接口:

http ://msdn.microsoft.com/en-us/library/system.type.getinterfaces.aspx

应该可以解决问题

You can lookup the interfaces a certain type implements with this method:

http://msdn.microsoft.com/en-us/library/system.type.getinterfaces.aspx

Should do the trick

一抹淡然 2024-08-05 01:20:03

您有具体的用例吗? 在我的脑海中,你可以使用:

System.Collections.ArrayList (or derived)
System.Collections.ObjectModel.Collection<T> derived
System.Collections.CollectionBase derived
System.Collections.DictionaryBase derived
System.Collections.Hashtable (or derived)

Do you have a specific use case? Off the top of my head you could use:

System.Collections.ArrayList (or derived)
System.Collections.ObjectModel.Collection<T> derived
System.Collections.CollectionBase derived
System.Collections.DictionaryBase derived
System.Collections.Hashtable (or derived)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文