Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert from string to any basic type

Tags:

c#

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);
}
like image 934
weston Avatar asked Nov 14 '12 15:11

weston


People also ask

How do you convert a string to an object?

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.

How do I convert a string to a number in Visual Basic?

You can use the following to convert string to int: CInt(String) for ints. CDec(String) for decimals.

Which function converts a string value to generic number value?

The isNaN() function converts the given value to a number before checking it the given number is equal to NaN .


1 Answers

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);
}
like image 119
Kirill Bestemyanov Avatar answered Sep 28 '22 12:09

Kirill Bestemyanov