This post serves to highlight a convenience method that is somewhat useful when one is attempting examine state of serveral different kinds of Exceptions. The ability to quickly capture and parse all types of Exceptions within a delegate can be quite useful.
I recalled that Objective-C has something called MemberOf (object) . This method allows one to determine whether a given object may be subclassed from a known class. In trying to find a corresponding method in C# I came accross the following paradigm:
typeof(Type).IsAssignableFrom (object.GetType())
if (inner != null)
{
if (typeof(Exception).IsAssignableFrom(inner.GetType()))
{
Exception temp = (Exception)inner;
Trace.WriteLine(temp.GetType().ToString());
Trace.WriteLine(temp.Message);
if (temp.InnerException != null)
Trace.WriteLine(temp.InnerException.Message);
}
}
Naturally we could refactor this into fewer lines but the example is just to illustrate the ability to determine base classes in short order. Hopefully this post will help someone avoid digging through the arcane documentation.
0 comments:
Post a Comment