4

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

4

5 回答 5

5

我认为可以使用Reflector

于 2009-07-15T21:39:07.760 回答
3

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

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

于 2009-07-15T21:39:20.707 回答
3

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

如果您知道类型所在的程序集(我正在使用它所mscorlibstring的位置),您可以使用此方法构建一个列表:

.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列表将包含Types实现该接口的列表IList。您可以更改IList为您喜欢的任何界面IDictionaryICollection, 等。

编辑:

如果您想将此扩展到当前的所有程序集AppDomain,您可以执行以下操作:

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

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

于 2009-07-15T22:14:24.803 回答
1

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

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

应该做的伎俩

于 2009-07-15T21:38:22.113 回答
0

您有特定的用例吗?在我的脑海中,您可以使用:

System.Collections.ArrayList (or derived)
System.Collections.ObjectModel.Collection<T> derived
System.Collections.CollectionBase derived
System.Collections.DictionaryBase derived
System.Collections.Hashtable (or derived)
于 2009-07-15T21:39:29.537 回答