I have a string
and a Type
, and I want to return the string
value converted to that Type
.
public static object StringToType(string value, Type propertyType)
{
return Convert.ChangeType(value, propertyType, CultureInfo.InvariantCulture);
}
This returns an object
that I can use in a property set value call:
public static void SetBasicPropertyValueFromString(object target,
string propName,
string value)
{
PropertyInfo prop = target.GetType().GetProperty(propName);
object converted = StringToType(value, prop.PropertyType);
prop.SetValue(target, converted, null);
}
This works for most basic types, except nullables.
[TestMethod]
public void IntTest()
{ //working
Assert.AreEqual(1, ValueHelper.StringToType("1", typeof (int)));
Assert.AreEqual(123, ValueHelper.StringToType("123", typeof (int)));
}
[TestMethod]
public void NullableIntTest()
{ //not working
Assert.AreEqual(1, ValueHelper.StringToType("1", typeof (int?)));
Assert.AreEqual(123, ValueHelper.StringToType("123", typeof (int?)));
Assert.AreEqual(null, ValueHelper.StringToType(null, typeof (int?)));
}
NullableIntTest
fails on first line with:
System.InvalidCastException: Invalid cast from 'System.String' to 'System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.
I'm having difficulty detemining if the type is nullable and changing the behaiour of the StringToType
method.
Behaviour I am after:
If string is null or empty, return null, else convert as per the non-nullable type.
Result
Like Kirill's answer, only with one ChangeType
call.
public static object StringToType(string value, Type propertyType)
{
var underlyingType = Nullable.GetUnderlyingType(propertyType);
if (underlyingType != null)
{
//an underlying nullable type, so the type is nullable
//apply logic for null or empty test
if (String.IsNullOrEmpty(value)) return null;
}
return Convert.ChangeType(value,
underlyingType ?? propertyType,
CultureInfo.InvariantCulture);
}
You can convert String to Object by using the assignment operator. An assignment operator assigns the string into the reference variable of the Object class. In the following example, we have taken a variable str of type String and initialized “book” into it. An Object is a super class of all classes.
You can use the following to convert string to int: CInt(String) for ints. CDec(String) for decimals.
The isNaN() function converts the given value to a number before checking it the given number is equal to NaN .
You cannot use Convert.ChangeType on nullable types cause it is not inherited from IConvertible. You should rewrite your method.
public static object StringToType(string value, Type propertyType)
{
var underlyingType = Nullable.GetUnderlyingType(propertyType);
if(underlyingType == null)
return Convert.ChangeType(value, propertyType, CultureInfo.InvariantCulture);
return String.IsNullOrEmpty(value)
? null
: Convert.ChangeType(value, underlyingType, CultureInfo.InvariantCulture);
}
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