Casting when using reflection

G

Guest

I am writing a method that kind of works like the default implmentation of
the serializing logic when using the Serializable attribute.
I have a base "serializer" that is using reflection to determine what
properties to serialize out. When I am deserializing I know the name of the
property and use reflection to get it. I know the type of the property from
the PropertyInfo PropertyType. The problem I have is that I need to take the
serialized value which is a string an convert it to the correct type say a
bool. How do I do this with the information I have?

Thanks
 
G

Guest

Hi Oldman

Have you looked at

Convert.ChangeType(object, Type)

According to MSDN
"Returns an Object with the specified Type and whose value is equivalent to
the specified object."

That should do the trick ;-)

HTH

Ged
 
G

Guest

Thanks Ged! I will look into this. I didn't see that method on the Convert
object. I aslo found that the IConvertible interface supports a ToType
method.
I will report back if either of these solutions work.
 

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