Casting when using reflection

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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
 
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.
 
Back
Top