Search

List All Assembly exceptions

This C# program lists the exceptions available within an assembly. Tip: call it from the command line with C:\ExceptionReflection.exe > Exceptions.txt.

using System;
using System.Collections;
using System.Reflection;
using System.Text;
 
namespace ExceptionReflection
{
 
   class Program
   {
 
      static void Main(string[] args)
      {
         // load the assemblies to analyze
         LoadAssemblies();
         ArrayList ExceptionTree = new ArrayList();
         foreach (Assembly Assembly in AppDomain.CurrentDomain.GetAssemblies())
         {
            foreach (Type Type in Assembly.GetTypes())
            {
               if (!Type.IsClass || !Type.IsPublic) continue;
 
               StringBuilder TypeHierarchy = 
                  new StringBuilder(Type.FullName, 5000);
               Boolean IsDerivedFromException = false;
               Type BaseType = Type.BaseType;
               while ((BaseType != null) && !IsDerivedFromException)
               {
                  TypeHierarchy.Append("-" + BaseType);
                  IsDerivedFromException = (BaseType == typeof(Exception));
                  BaseType = BaseType.BaseType;
               }
 
               if (!IsDerivedFromException) continue;
 
               String[] Hierarchy = TypeHierarchy.ToString().Split('-');
               Array.Reverse(Hierarchy);
 
               ExceptionTree.Add(String.Join
                  ("-", Hierarchy, 1, Hierarchy.Length - 1));
               ExceptionTree.Sort();
               foreach (String Identifier in ExceptionTree)
               {
                  String[] Trace = Identifier.Split('-');
                  Console.WriteLine(new String(' ', 3*Trace.Length) 
                     + Trace[Trace.Length-1]);
               }
            }
         }
            Console.ReadLine();
      }
 
      static void LoadAssemblies()
      {
         String[] Assemblies =
         {
            /* uncomment as needed */
            //"System, PublicKeyToken={0}",
            //"System.Data, PublicKeyToken={0}",
            //"System.Design, PublicKeyToken={1}", 
            //"System.DirectoryServices, PublicKeyToken={1}", 
            //"System.Drawing, PublicKeyToken={1}",
            //"System.Drawing.Design, PublicKeyToken={1}",
            //"System.EnterpriseServices, PublicKeyToken={1}",
            //"System.Management, PublicKeyToken={1}",
            //"System.messaging, PublicKeyToken={1}",
            //"System.Runtime.Remoting, PublicKeyToken={0}",
            //"System.Security, PublicKeyToken={1}",
            //"System.serviceProcess, PublicKeyToken={1}",
            //"System.Web, PublicKeyToken={1}",
            //"System.Web.RegularExpressions, PublicKeyToken={1}",
            //"System.Web.Services, PublicKeyToken={1}",
            //"System.Windows.Forms, PublicKeyToken={0}",
            //"System.Xml, PublicKeyToken={0}",
            "MSCorLib, PublicKeyToken={0}"
         };
 
         // change this if you want to use assemblies that are not from microsoft
         String EcmaPublicKeyToken = "B77A5C561934E089";
         String MSPublicKeyToken   = "B03F5F7F11D50A3A";
 
         // get the newest version
         Version Version = typeof(System.Object).Assembly.GetName().Version;
 
         // load the assemblies
         foreach (String AssemblyName in Assemblies)
         {
            String AssemblyIdentity = 
               String.Format(AssemblyName, EcmaPublicKeyToken, 
               MSPublicKeyToken) + 
               ", Culture=neutral, Version=" + Version;
            Assembly.Load(AssemblyIdentity);
         }
      }
   }
}