您可以使用反射来查找 BCL 中不同类型的数量,但是您希望通过这些信息完成什么?
以下是如何获取该信息的示例:
using System;
using System.Linq;
using System.Reflection;
class Example
{
static void Main()
{
Assembly mscorlib = typeof(String).Assembly;
// Number of classes
Console.WriteLine(mscorlib.GetTypes().Where(t => t.IsClass).Count());
// Number of value types (structs and enums)
Console.WriteLine(mscorlib.GetTypes().Where(t => t.IsValueType).Count());
// Number of interfaces
Console.WriteLine(mscorlib.GetTypes().Where(t => t.IsInterface).Count());
}
}
请注意,您需要为框架中的每个程序集执行此操作以获取总数。
编辑:这是一个快速而肮脏的解决方案,可以让您大致了解 BCL 中的类型数量:
using System;
using System.Linq;
using System.Reflection;
using System.IO;
using System.Runtime.InteropServices;
class Example
{
static void Main()
{
// Get all DLLs in the current runtime directory
var assemblies = Directory.GetFiles(
RuntimeEnvironment.GetRuntimeDirectory())
.Where(f => f.EndsWith(".dll"));
Int32 classes = 0;
Int32 valueTypes = 0;
Int32 interfaces = 0;
foreach (String name in assemblies)
{
// We need to catch BadImageFormatException
// because not all DLLs in the runtime directory
// are CLR assemblies.
try
{
var types = Assembly.LoadFile(name).GetTypes();
classes += types.Where(t => t.IsClass).Count();
valueTypes += types.Where(t => t.IsValueType).Count();
interfaces += types.Where(t => t.IsInterface).Count();
}
catch (BadImageFormatException) { }
}
Console.WriteLine("Classes: {0}", classes);
Console.WriteLine("Value types: {0}", valueTypes);
Console.WriteLine("Interfaces: {0}", interfaces);
}
}