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

Dynamically Parsing a String to Any Type When You Don't Know The Type


Last post I showed a very basic Object to Object Mapper. It works quite well for my needs, although I'm modifying it further to have a couple features that I need. The first is the simple fact that I need it to dynamically convert an object to a string (and allow for formatting) and back from that string to the object. To be honest, it's pretty simple. Every object inherits from object, a simple base class that has a couple of functions including ToString, however the object base class doesn't allow for formatting. For example, if the object is of type DateTime but we're only passed it as an object, all we can do is call the default ToString, right? Not exactly. We can actually call the ToString that allows formatting by simply using reflection:

   1: public static string FormatToString(object Input, string Format)
   2: {
   3:     if (Input == null)
   4:         return "";
   5:     if (!string.IsNullOrEmpty(Format))
   6:     {
   7:         Type InputType = Input.GetType();
   8:         Type[] InputVariableTypes = new Type[1];
   9:         InputVariableTypes[0] = typeof(string);
  10:         MethodInfo Property = InputType.GetMethod("ToString", InputVariableTypes);
  11:         if (Property != null)
  12:         {
  13:             object[] InputVariables = new object[1];
  14:             InputVariables[0] = Format;
  15:             return (string)Property.Invoke(Input, InputVariables);
  16:         }
  17:         return Input.ToString();
  18:     }
  19:     return "";
  20: }

That's it. With that simple function you can take anything as an input with any format string and it will output the object formatted correctly. Well assuming it has a ToString function which allows for formatting. And to be honest, going back to the object is just as simple:

   1: public static object Parse(string Input, Type OutputType)
   2: {
   3:     if (string.IsNullOrEmpty(Input) || OutputType == null)
   4:         return null;
   5:     Type[] MethodInputType = new Type[1];
   6:     MethodInputType[0] = typeof(string);
   7:     MethodInfo ParseMethod = OutputType.GetMethod("Parse", MethodInputType);
   8:     if (ParseMethod != null)
   9:     {
  10:         object Item = OutputType.Assembly.CreateInstance(OutputType.FullName);
  11:         object[] Values = new object[1];
  12:         Values[0] = Input;
  13:         return ParseMethod.Invoke(Item, Values);
  14:     }
  15:     return null;
  16: }

In this case, since we don't know what we're parsing the string into, we need to know the output type. From there we simply look for the Parse function and assuming it's there pass in the string to be parsed. Really simple and allows us to parse the string into pretty much anything. Of course it's better if you simply know the type beforehand and just call int.Parse or float.Parse... But for the instances where you can't do that, this function will work in a pinch. So try it out, leave feedback, and happy coding.


February 10, 2010 9:59 AM

Big thanks!You save me from inventing the bike :)