Using Reflection to get Assembly Information in C#


I've been working on improving my debug/error reporting capabilities (which includes unit testing, automation, etc.). One of the items on that list though, is what I actually record. Up until now that has included system information (OS, memory, etc.), basic profiling, error message, etc. However one of the items that would have been helpful and I simply wasn't recording was information about what assemblies were currently loaded by the system. It's usually easy to figure out (just look at the references list), but sometimes a few get in there that you aren't expecting. So to help out, I created a couple functions to find out this information:


        public static string DumpAllAssembliesAndProperties()
        {
            StringBuilder Builder = new StringBuilder();
            Assembly[] Assemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach (Assembly Assembly in Assemblies)
            {
                Builder.Append("<strong>" + Assembly.GetName().Name + "</strong><br />");
                Builder.Append(DumpProperties(Assembly)+"<br /><br />");
            }
            return Builder.ToString();
        }

        public static string DumpProperties(object Object)
        {
            try
            {
                StringBuilder TempValue = new StringBuilder();
                TempValue.Append("<table><thead><tr><th>Property Name</th><th>Property Value</th></tr></thead><tbody>");
                Type ObjectType = Object.GetType();
                PropertyInfo[] Properties = ObjectType.GetProperties();
                foreach (PropertyInfo Property in Properties)
                {
                    TempValue.Append("<tr><td>" + Property.Name + "</td><td>");
                    if (Property.GetIndexParameters().Length == 0)
                    {
                        try
                        {
                            TempValue.Append(Property.GetValue(Object, null) == null ? "null" : Property.GetValue(Object, null).ToString());
                        }
                        catch { }
                    }
                    TempValue.Append("</td></tr>");
                }
                TempValue.Append("</tbody></table>");
                return TempValue.ToString();
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

The above function DumpAllAssembliesAndProperties is the one you want to call. It in turn will figure out what assemblies are loaded and output (in an HTML formatted string) the name as well as property information (file name, etc.). It's pretty basic but thanks to the function I was able to find a couple dependencies and remove them and in one case discovered I was using the wrong version. Also note that the DumpProperties function will allow you to take any object and dump the properties of it along with their current values (another useful function to have around when debugging). Anyway, I hope this helps you out. So try out the code, leave feedback, and happy coding.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: C#
Posted by James Craig on Wednesday, November 26, 2008 2:27 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Related posts

Recent comments

None

Calendar

<<  January 2009  >>
SuMoTuWeThFrSa
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

Sponsors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2009