How to convert a string to any type

I want to convert a string to a generic type

I have it:

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 this property to check if it is compatible, how can I do this?

tks

+46
generics reflection c #
May 27 '10 at 16:18
source share
3 answers
 using System.ComponentModel; TypeConverter typeConverter = TypeDescriptor.GetConverter(propType); object propValue = typeConverter.ConvertFromString(inputValue); 
+72
May 27 '10 at 16:27
source share

Try Convert.ChangeType

 object propvalue = Convert.ChangeType(inputValue, propType); 
+11
May 27 '10 at 16:24
source share

I really don't think I understand that you are trying to archive, but .. do you mean dynamic casting? Something like that:

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

Greetings.

+2
May 27 '10 at 16:25
source share



All Articles