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
You convert a string to a number by calling the Parse or TryParse method found on numeric types ( int , long , double , and so on), or by using methods in the System. Convert class. It's slightly more efficient and straightforward to call a TryParse method (for example, int.
using System.ComponentModel; TypeConverter typeConverter = TypeDescriptor.GetConverter(propType); object propValue = typeConverter.ConvertFromString(inputValue);
Try Convert.ChangeType
object propvalue = Convert.ChangeType(inputValue, propType);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With