views:

73

answers:

3

Hi there

I want to convert a string to a generic type

I have this:

string inputValue = myTxtBox.Text;    

PropertyInfo propInfo = typeof(MyClass).GetProperty(myPropertyName);
Type propType = propInfo.PropertyType;

object propValue = ?????

I want to convert 'inputString' to the type of that property, to check if it's compatible how can I do that?

tks

A: 

Try Convert.ChangeType

SWeko
+1  A: 

I don't really think I understand what your are trying to archieve, but.. you mean a dynamic casting? Something like this:

 TypeDescriptor.GetConverter(typeof(String)).ConvertTo(myObject, typeof(Program));

Cheers.

vtortola
+3  A: 
TypeConverter typeConverter = TypeDescriptor.GetConverter(propType);
object propValue = typeConverter.ConvertFromString(inputValue);
Lee