A Console application to determine the number of sealed and unsealed types in the framework
static class Program
{
static void Main(string[] args)
{
var publicFrameworkTypes = new HashSet<Type>(
from assembly in AppDomain.CurrentDomain.GetAssemblies()
from type in assembly.GetTypes()
where type.FullName.StartsWith("System")
where type.IsPublic
where !type.IsValueType
where !type.IsInterface
where !type.IsPointer
where !type.IsPrimitive
select type);
Console.WriteLine("publicFrameworkTypes.Count: " + publicFrameworkTypes.Count());
// Non-leaf types are types that have descendents within the framework.
var nonLeafTypes = new HashSet<Type>(
from type in publicFrameworkTypes
from nonLeafType in type.GetBaseTypes()
where nonLeafType.IsPublic
where publicFrameworkTypes.Contains(nonLeafType)
select nonLeafType);
Console.WriteLine("nonLeafTypes.Count: " + nonLeafTypes.Count());
// Leaf types are types that don't have any descendents within the framwork.
var leafTypes = new HashSet<Type>(publicFrameworkTypes);
leafTypes.ExceptWith(nonLeafTypes);
Console.WriteLine("leafTypes.Count: " + leafTypes.Count());
var sealedLeafTypes = (
from leafType in leafTypes
where leafType.IsSealed || !leafType.HasPublicConstructors()
select leafType).ToArray();
var openLeafTypes = (
from leafType in leafTypes
where !(leafType.IsSealed || !leafType.HasPublicConstructors())
select leafType).ToArray();
Console.WriteLine("Open types: {0} = {1:p}", openLeafTypes.Length,
(double)openLeafTypes.Length / (double)leafTypes.Count);
Console.WriteLine("Sealed types: {0} = {1:p}", sealedLeafTypes.Length,
(double)sealedLeafTypes.Length / (double)leafTypes.Count);
Console.WriteLine("Done");
Console.ReadLine();
}
private static IEnumerable<Type> GetBaseTypes(this Type type)
{
var baseType = GetGenericTypeDefinition(type.BaseType);
while (baseType != null)
{
yield return baseType;
baseType = GetGenericTypeDefinition(baseType.BaseType);
}
}
private static Type GetGenericTypeDefinition(Type type)
{
return type != null && type.IsGenericType ? type.GetGenericTypeDefinition() : type;
}
private static bool HasPublicConstructors(this Type type)
{
return type.GetConstructors(BindingFlags.Instance | BindingFlags.Public).Any();
}
}
Fork
0 Feedback
You must log in before you can give any feedback
You must log in before you can post a comment


220
0




Mark '.net' tag as 'like'
Mark '.net' tag as 'ignore'