Runtime Type Conversion

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 ...

}
 
B

Brian Pelton

I think that's exactly it! Somehow "ChangeType" didn't catch my eye..

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top