#955 – C# Is a Managed Language

When you author code in C#, you’re writing managed code.  That is, you’re creating a managed application.

Managed code runs within the context of the Common Language Runtime (CLR).  The CLR is a run-time environment that executes managed code and provides basic services to your application like a standard type system, memory management, and exception handling.

Managed code can be written in a number of different languages, including (but not limited to) C#, Visual Basic, and Managed C++.  The compiler for the associated language converts the code into Microsoft Intermediate Language (MSIL), which is consumed by the CLR and then compiled by a Just in Time Compiler (JIT) into machine-specific instructions.

Managed code can execute not only within Microsoft’s CLR, running on Windows, but also on other third-party CLR implementations. (E.g. The Mono project, which implements a runtime for running managed code on Android, Linux and OS X).

#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