【问题标题】:What are all of the generic collections/types in the 4.0 .NET Framework?4.0 .NET Framework 中的所有泛型集合/类型是什么?
【发布时间】:2010-11-29 21:13:28
【问题描述】:

.NET Framework 4.0 中是否有所有“基础”(不是在面向对象的意义上使用,而是更多地用于常识)泛型类型的详尽列表?我发现 this list 我经常将新/中级开发人员发送给他们,以便他们了解非泛型类型如何映射到泛型类型,但这绝不是详尽无遗的。我正在寻找的东西还包括 KeyValuePair<>Tuple<> 和其他可能不太为人所知的基本泛型。 IObservable<> 之类的接口会很好,但不一定是必需的。

【问题讨论】:

  • 也许我们可以写下我们认为有用的所有泛型作为下面的答案?你会从答案中生成类型列表吗?
  • 不幸的是,您需要更好地定义您的要求。 KeyValuePair<>Tuple<> 不是集合类型(尽管 KeyValuePairDictionary<> 密切相关)。 IObservable 也不是一个集合接口,尽管ObservableCollection<T> 确实使用了它。你想要像IQueryable 这样的类型,它不代表一个集合,而是一个你可以运行查询的存储库(大小未定义)?

标签: c# .net vb.net generics .net-4.0


【解决方案1】:

这是一个列出系统程序集中所有泛型类型的 powershell 片段

[AppDomain]::CurrentDomain.GetAssemblies() | 
    ? { $_.FullName -match "^System" -or $_.FullName -match "mscorlib"} |  
        % { $_.GetTypes() | ? { $_.ContainsGenericParameters } }

这可以进行调整以提供更全面的列表。如果你加载powershell w/ a .Net 4 config,你会得到4.0列表。

Count Name                      
----- ----                      
    1 System.Xml
   12 System.Data
   67 System
  187 mscorlib
  325 System.Core

【讨论】:

    【解决方案2】:

    在 Visual Studio 中打开对象资源管理器,选择 .NET 4 Framework,搜索 IEnumerable<T> 并展开派生类型列表。在这里粘贴太多了:-)。

    【讨论】:

      【解决方案3】:

      您可以反映所有 BCL 程序集并使用 IsGenericType 方法获取泛型类型列表。

      【讨论】:

        【解决方案4】:

        您可以在 System.Collections.Generic 命名空间中找到它 http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx

        【讨论】:

        • 未满,其他命名空间和程序集中还有其他类型和接口,如IQueryable
        【解决方案5】:

        这是我的代码。根据需要添加更多程序集。

            static void Main(string[] args)
            {
                Assembly[] assemblies = new Assembly[] { 
                    typeof(string).Assembly,
                    typeof(Uri).Assembly, 
                    typeof(System.Linq.Enumerable).Assembly};
        
                List<string> final = new List<string>();
        
                Debug.WriteLine("Checked assemblies: ");
                foreach (Assembly assembly in assemblies)
                {
                    Debug.WriteLine(assembly.FullName);
        
                    Type[] types = assembly.GetTypes();
                    IEnumerable<Type> genericTypes = types.Where(t => t.IsGenericType && t.IsPublic);
                    foreach (Type t in genericTypes)
                    {
                        final.Add(t.FullName);
                    }
                }
        
                final.Sort();
        
                Debug.WriteLine("Generic classes: ");
                foreach (string s in final)
                {
                    Debug.WriteLine(s);
                }
            }
        

        结果:

        • 检查的程序集:
        • mscorlib,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089
        • 系统,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089
        • System.Core,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089

        • 泛型类:

        • System.Action`1
        • System.Action`10
        • System.Action`11
        • System.Action`12
        • System.Action`13
        • System.Action`14
        • System.Action`15
        • System.Action`16
        • System.Action`2
        • System.Action`3
        • System.Action`4
        • System.Action`5
        • System.Action`6
        • System.Action`7
        • System.Action`8
        • System.Action`9
        • System.ArraySegment`1
        • System.Collections.Concurrent.BlockingCollection`1
        • System.Collections.Concurrent.ConcurrentBag`1
        • System.Collections.Concurrent.ConcurrentDictionary`2
        • System.Collections.Concurrent.ConcurrentQueue`1
        • System.Collections.Concurrent.ConcurrentStack`1
        • System.Collections.Concurrent.IProducerConsumerCollection`1
        • System.Collections.Concurrent.OrderablePartitioner`1
        • System.Collections.Concurrent.Partitioner`1
        • System.Collections.Generic.Comparer`1
        • System.Collections.Generic.Dictionary`2
        • System.Collections.Generic.EqualityComparer`1
        • System.Collections.Generic.HashSet`1
        • System.Collections.Generic.ICollection`1
        • System.Collections.Generic.IComparer`1
        • System.Collections.Generic.IDictionary`2
        • System.Collections.Generic.IEnumerable`1
        • System.Collections.Generic.IEnumerator`1
        • System.Collections.Generic.IEqualityComparer`1
        • System.Collections.Generic.IList`1
        • System.Collections.Generic.ISet`1
        • System.Collections.Generic.KeyValuePair`2
        • System.Collections.Generic.LinkedList`1
        • System.Collections.Generic.LinkedListNode`1
        • System.Collections.Generic.List`1
        • System.Collections.Generic.Queue`1
        • System.Collections.Generic.SortedDictionary`2
        • System.Collections.Generic.SortedList`2
        • System.Collections.Generic.SortedSet`1
        • System.Collections.Generic.Stack`1
        • System.Collections.ObjectModel.Collection`1
        • System.Collections.ObjectModel.KeyedCollection`2
        • System.Collections.ObjectModel.ObservableCollection`1
        • System.Collections.ObjectModel.ReadOnlyCollection`1
        • System.Collections.ObjectModel.ReadOnlyObservableCollection`1
        • System.Comparison`1
        • System.ComponentModel.BindingList`1
        • System.Converter`2
        • System.EventHandler`1
        • System.Func`1
        • System.Func`10
        • System.Func`11
        • System.Func`12
        • System.Func`13
        • System.Func`14
        • System.Func`15
        • System.Func`16
        • System.Func`17
        • System.Func`2
        • System.Func`3
        • System.Func`4
        • System.Func`5
        • System.Func`6
        • System.Func`7
        • System.Func`8
        • System.Func`9
        • System.IComparable`1
        • System.IEquatable`1
        • System.IObservable`1
        • System.IObserver`1
        • System.Lazy`1
        • System.Linq.EnumerableExecutor`1
        • System.Linq.EnumerableQuery`1
        • System.Linq.Expressions.Expression`1
        • System.Linq.IGrouping`2
        • System.Linq.ILookup`2
        • System.Linq.IOrderedEnumerable`1
        • System.Linq.IOrderedQueryable`1
        • System.Linq.IQueryable`1
        • System.Linq.Lookup`2
        • System.Linq.OrderedParallelQuery`1
        • System.Linq.ParallelQuery`1
        • System.Nullable`1
        • System.Predicate`1
        • System.Runtime.CompilerServices.CallSite`1
        • System.Runtime.CompilerServices.ConditionalWeakTable`2
        • System.Runtime.CompilerServices.ReadOnlyCollectionBuilder`1
        • System.Runtime.CompilerServices.RuleCache`1
        • System.Runtime.CompilerServices.StrongBox`1
        • System.Security.AccessControl.AccessRule`1
        • System.Security.AccessControl.AuditRule`1
        • System.Security.AccessControl.ObjectSecurity`1
        • System.Threading.Tasks.Task`1
        • System.Threading.Tasks.TaskCompletionSource`1
        • System.Threading.Tasks.TaskFactory`1
        • System.Threading.ThreadLocal`1
        • System.Tuple`1
        • System.Tuple`2
        • System.Tuple`3
        • System.Tuple`4
        • System.Tuple`5
        • System.Tuple`6
        • System.Tuple`7
        • System.Tuple`8

        【讨论】:

          【解决方案6】:

          这应该会给你一些答案:

          1. System.Collections.Generic Namespace;
          2. System.Collections.Concurrent Namespace

          System.Collections.Generic 命名空间提供了我们在 .NET 3.5 中已经知道的大多数通用集合。已经提供了三个线程安全的集合:

          System.Collections.Concurrent 命名空间命名空间提供线程安全的通用集合。

          【讨论】:

            猜你喜欢
            • 2010-10-30
            • 1970-01-01
            • 1970-01-01
            • 2016-06-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-02-06
            相关资源
            最近更新 更多