#725 – Dumping Out a List of Types in an Assembly

You can use reflection to get a list of all types in an assembly.  Each type is represented by an instance of the Type class, which you can then query to get information about the type.

Below is a code sample that dumps out all types defined in the running assembly.  Notice that we identify each type as one of the five custom types that you can define.

        static void Main()
        {
            foreach (Type t in Assembly.GetExecutingAssembly().GetTypes())
            {
                string dump =
                    string.Format("{0}\n  ({1} in {2})\n", t.FullName, TypeIndicator(t), t.Namespace);

                Console.WriteLine(dump);
            }
        }

        private static string TypeIndicator(Type t)
        {
            string typeIndicator = "?";

            if ((t.BaseType != null) &&
                (t.BaseType.FullName == "System.MulticastDelegate"))
                typeIndicator = "delegate";

            else if (t.IsClass)
            {
                if (t.IsNested)
                    typeIndicator = "Nested class";
                else
                    typeIndicator = "class";
            }
            else if (t.IsInterface)
                typeIndicator = "interface";

            else if (t.IsValueType)
                typeIndicator = "struct";

            else if (t.IsEnum)
                typeIndicator = "enum";

            return typeIndicator;
        }
Advertisement