Other Posts in Reflection

  1. Using Reflection to get Assembly Information in C#
  2. Overridding a Property With Reflection.Emit
  3. Shallow Copy of an Object Using Reflection
  4. Dynamically Parsing a String to Any Type When You Don't Know The Type
  5. Reflection.Emit the Simple Way

Using Reflection to get Assembly Information in C#

11/26/2008

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:

   1: /// <summary>
   2: /// Returns all assemblies and their properties
   3: /// </summary>
   4: /// <returns>An HTML formatted string that contains the assemblies and their information</returns>
   5: public static string DumpAllAssembliesAndProperties()
   6: {
   7:     StringBuilder Builder = new StringBuilder();
   8:     Assembly[] Assemblies = AppDomain.CurrentDomain.GetAssemblies();
   9:     foreach (Assembly Assembly in Assemblies)
  10:     {
  11:         Builder.Append("<strong>").Append(Assembly.GetName().Name).Append("</strong><br />");
  12:         Builder.Append(DumpProperties(Assembly)).Append("<br /><br />");
  13:     }
  14:     return Builder.ToString();
  15: }
  16:  
  17: /// <summary>
  18: /// Dumps the properties names and current values
  19: /// from an object
  20: /// </summary>
  21: /// <param name="Object">Object to dunp</param>
  22: /// <returns>An HTML formatted table containing the information about the object</returns>
  23: public static string DumpProperties(object Object)
  24: {
  25:     StringBuilder TempValue = new StringBuilder();
  26:     TempValue.Append("<table><thead><tr><th>Property Name</th><th>Property Value</th></tr></thead><tbody>");
  27:     Type ObjectType = Object.GetType();
  28:     PropertyInfo[] Properties = ObjectType.GetProperties();
  29:     foreach (PropertyInfo Property in Properties)
  30:     {
  31:         TempValue.Append("<tr><td>").Append(Property.Name).Append("</td><td>");
  32:         ParameterInfo[] Parameters = Property.GetIndexParameters();
  33:         if (Property.CanRead && Parameters.Length == 0)
  34:         {
  35:             try
  36:             {
  37:                 object Value = Property.GetValue(Object, null);
  38:                 TempValue.Append(Value == null ? "null" : Value.ToString());
  39:             }
  40:             catch { }
  41:         }
  42:         TempValue.Append("</td></tr>");
  43:     }
  44:     TempValue.Append("</tbody></table>");
  45:     return TempValue.ToString();
  46: }

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.



Comments