Runtime Type Conversion

  • Thread starter Thread starter Brian Pelton
  • Start date Start date
B

Brian Pelton

My basic question is this:

Is there a Conversion class in the .Net framework that you can pass and
object to and a type to and get back an object of the given type?

For example. Let's say I have a string value of "32". Is there a
method that will take "32" and Type.GetType("System.Int32") and return
an object of the in32 type?

What I have now is a utility class that does that, but I was hoping that
it might already exist somewhere in the framework.

Right now my utility class looks like this:

public static object ConvertToGivenType(Object ValueToConvert, Type
DesiredType)
{
if (DesiredType.FullName == "System.Int32")
return Convert.ToInt32(ValueToConvert);

... lots more ifs ...

}
 
Back
Top