#728 – Dumping Out All Types in the .NET Framework

Here’s some code that looks through all assemblies in the .NET Framework directory and uses to reflection to dump out all of the types, organized by namespace.

        static void Main()
        {
            int totalNamespaces = 0;
            int totalTypes = 0;

            AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += CurrentDomain_ReflectionOnlyAssemblyResolve;

            SortedList<string, SortedList<string, Type>> fullList =
                new SortedList<string, SortedList<string, Type>>();

            DirectoryInfo di = new DirectoryInfo(RuntimeEnvironment.GetRuntimeDirectory());
            foreach (FileInfo fi in di.GetFiles())
            {
                try
                {
                    Assembly assem = Assembly.LoadFrom(fi.FullName);

                    if (assem != null)
                    {
                        ExtractListOfTypes(assem, ref fullList, ref totalNamespaces, ref totalTypes);
                    }
                }
                catch { }
            }

            Console.WriteLine(string.Format("{0} types, in {1} namespaces", totalTypes, totalNamespaces));
            DumpTypeList(fullList);
        }

        static Assembly CurrentDomain_ReflectionOnlyAssemblyResolve(object sender, ResolveEventArgs args)
        {
            return Assembly.ReflectionOnlyLoad(args.Name);
        }

        private static void ExtractListOfTypes(Assembly assem, ref SortedList<string, SortedList<string, Type>> theList,
            ref int totalNamespaces, ref int totalTypes)
        {
            if (theList == null)
                throw new Exception("Uninitialized list");

            foreach (Type t in assem.GetTypes())
            {
                string theNamespace = t.Namespace != null ? t.Namespace : "(global)";

                // Add namespace if it's not already in list
                if (!theList.ContainsKey(theNamespace))
                {
                    theList.Add(theNamespace, new SortedList<string, Type>());
                    totalNamespaces++;
                }

                // And add type under appropriate namespace
                if (!theList[theNamespace].ContainsKey(t.FullName))
                {
                    theList[theNamespace].Add(t.FullName, t);
                    totalTypes++;
                }
            }

            return;
        }

        private static void DumpTypeList(SortedList<string, SortedList<string, Type>> theList)
        {
            foreach (KeyValuePair<string,SortedList<string,Type>> kvp in theList)
            {
                Console.WriteLine(string.Format("Namespace: [{0}]", kvp.Key));

                foreach (KeyValuePair<string, Type> kvpInner in kvp.Value)
                {
                    Console.WriteLine(string.Format("  Type: [{0}]", ((Type)kvpInner.Value).FullName));
                }

                Console.WriteLine();
            }
        }

When I run this against .NET 4.0.30319, I get a total of 40,166 types, in 657 namespaces.

728-001

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #728 – Dumping Out All Types in the .NET Framework

  1. please make it simple says:

    you keep help us .. really thank you sir

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: