354

I want to convert a string to an object property value, whose name I have as a string. I am trying to do this like so:

string modelProperty = "Some Property Name";
string value = "SomeValue";
var property = entity.GetType().GetProperty(modelProperty);
if (property != null) {
    property.SetValue(entity, 
        Convert.ChangeType(value, property.PropertyType), null);
}

The problem is this is failing and throwing an Invalid Cast Exception when the property type is a nullable type. This is not the case of the values being unable to be Converted - they will work if I do this manually (e.g. DateTime? d = Convert.ToDateTime(value);) I've seen some similiar questions but still can't get it to work.

1
  • 1
    I'm using ExecuteScalar<int?> with PetaPoco 4.0.3 and it fails for the same reason : return (T)Convert.ChangeType(val, typeof(T)) at line 554
    – Larry
    Oct 14, 2011 at 13:05

7 Answers 7

485

Untested, but maybe something like this will work:

string modelProperty = "Some Property Name";
string value = "Some Value";

var property = entity.GetType().GetProperty(modelProperty);
if (property != null)
{
    Type t = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;

    object safeValue = (value == null) ? null : Convert.ChangeType(value, t);

    property.SetValue(entity, safeValue, null);
}
6
  • 14
    Just needed that piece of code myself. Thanks for Nullable.GetUnderlyingType! Helped me out a lot when I built a poor man's ModelBinder for a project that needed it. I owe you a beer! Feb 22, 2012 at 19:43
  • 3
    Maybe instead of (value == null) ? null use (value == null) ? default(t)?
    – threadster
    Jan 11, 2017 at 14:28
  • Does not seem to work for uniqueidentifier to string. Nov 5, 2018 at 10:32
  • Is there any particular reason to create the safeValue variable as opposed to just reassigning to value?
    – jrnxf
    May 13, 2019 at 20:01
  • 2
    @threadster You can't use default operator ona variable of type 'Type'. See stackoverflow.com/questions/325426/…
    – andy250
    Dec 3, 2019 at 16:53
82

You have to get the underlying type in order to do that...

Try this, I've used it successfully with generics:

//Coalesce to get actual property type...
Type t = property.PropertyType();
t = Nullable.GetUnderlyingType(t) ?? t;

//Coalesce to set the safe value using default(t) or the safe type.
safeValue = value == null ? default(t) : Convert.ChangeType(value, t);

I use it in a number of places in my code, one example is a helper method I use for converting database values in a typesafe manner:

public static T GetValue<T>(this IDataReader dr, string fieldName)
{
    object value = dr[fieldName];

    Type t = typeof(T);
    t = Nullable.GetUnderlyingType(t) ?? t;

    return (value == null || DBNull.Value.Equals(value)) ? 
        default(T) : (T)Convert.ChangeType(value, t);
}

Called using:

string field1 = dr.GetValue<string>("field1");
int? field2 = dr.GetValue<int?>("field2");
DateTime field3 = dr.GetValue<DateTime>("field3");

I wrote a series of blog posts including this at http://www.endswithsaurus.com/2010_07_01_archive.html (Scroll down to the Addendum, @JohnMacintyre actually spotted the bug in my original code which led me down the same path you're on now). I have a couple of small modifications since that post that includes conversion of enum types also so if your property is an Enum you can still use the same method call. Just add a line in to check for enum types and you're off to the races using something like:

if (t.IsEnum)
    return (T)Enum.Parse(t, value);

Normally you'd have some error checking or use TryParse instead of Parse, but you get the picture.

3
  • Thanks - I'm still missing a step or not understanding something. I am trying to set a property value, why am I getting the object that it is in's Underlying Type? I am also not sure how to get from my code to an extension method like yours. I won't know what the type will be in order to do something like value.Helper<Int32?>().
    – iboeno
    Aug 20, 2010 at 14:08
  • @iboeno - Sorry, was off in a meeting so I couldn't help you connect the dots. Glad you got a solution though. Aug 20, 2010 at 15:32
  • 1
    In the first code block, where t is a variable (i.e. a value), how can you write default(t)? You can use default() only with a (compile-time) type (can be a generic type). Dec 8, 2020 at 14:08
12

This works perfectly even for Nullable types:

TypeConverter conv = TypeDescriptor.GetConverter(type);
return conv.ConvertFrom(value);

For type safety you should also call conv.CanConvertFrom(type) method before calling ConvertFrom(). In case it returns false you can fallback to ChangeType or something else.

5
  • great answer - and very simple!
    – Lee Z
    Jun 22, 2021 at 0:47
  • Note if value isn't already the underlying type - e.g. try double to decimal?.
    – NetMage
    Oct 6, 2022 at 20:05
  • @NetMage not sure if I get your comment. If you asking if type cannot be converted or not you can validate it by calling CanConvertFrom() method. It is in the answer.
    – Major
    Oct 7, 2022 at 16:39
  • 1
    The original question is about converting from a type to a Nullable type - ConvertFrom only works if the type of the value is the same as the underlying type of the Nullable. So e.g. you can use it for decimal => decimal? but not double => decimal?. Whether that matters to the OP depends on the types in their columns versus the target class field types.
    – NetMage
    Oct 7, 2022 at 19:25
  • 1
    @NetMage Sorry TypeDescriptor.GetConverter() works perfectly for converting a STRING to a nullable type as the OP clearly asked. Helpful to know about the other limitations but not relevant to the actual question.
    – Ash
    Nov 8, 2023 at 4:30
10

This is a little bit long-ish for an example, but this is a relatively robust approach, and separates the task of casting from unknown value to unknown type

I have a TryCast method that does something similar, and takes nullable types into account.

public static bool TryCast<T>(this object value, out T result)
{
    var type = typeof (T);

    // If the type is nullable and the result should be null, set a null value.
    if (type.IsNullable() && (value == null || value == DBNull.Value))
    {
        result = default(T);
        return true;
    }

    // Convert.ChangeType fails on Nullable<T> types.  We want to try to cast to the underlying type anyway.
    var underlyingType = Nullable.GetUnderlyingType(type) ?? type;

    try
    {
        // Just one edge case you might want to handle.
        if (underlyingType == typeof(Guid))
        {
            if (value is string)
            {
                value = new Guid(value as string);
            }
            if (value is byte[])
            {
                value = new Guid(value as byte[]);
            }

            result = (T)Convert.ChangeType(value, underlyingType);
            return true;
        }

        result = (T)Convert.ChangeType(value, underlyingType);
        return true;
    }
    catch (Exception ex)
    {
        result = default(T);
        return false;
    }
}

Of course TryCast is a Method with a Type Parameter, so to call it dynamically you have to construct the MethodInfo yourself:

var constructedMethod = typeof (ObjectExtensions)
    .GetMethod("TryCast")
    .MakeGenericMethod(property.PropertyType);

Then to set the actual property value:

public static void SetCastedValue<T>(this PropertyInfo property, T instance, object value)
{
    if (property.DeclaringType != typeof(T))
    {
        throw new ArgumentException("property's declaring type must be equal to typeof(T).");
    }

    var constructedMethod = typeof (ObjectExtensions)
        .GetMethod("TryCast")
        .MakeGenericMethod(property.PropertyType);

    object valueToSet = null;
    var parameters = new[] {value, null};
    var tryCastSucceeded = Convert.ToBoolean(constructedMethod.Invoke(null, parameters));
    if (tryCastSucceeded)
    {
        valueToSet = parameters[1];
    }

    if (!property.CanAssignValue(valueToSet))
    {
        return;
    }
    property.SetValue(instance, valueToSet, null);
}

And the extension methods to deal with property.CanAssignValue...

public static bool CanAssignValue(this PropertyInfo p, object value)
{
    return value == null ? p.IsNullable() : p.PropertyType.IsInstanceOfType(value);
}

public static bool IsNullable(this PropertyInfo p)
{
    return p.PropertyType.IsNullable();
}

public static bool IsNullable(this Type t)
{
    return !t.IsValueType || Nullable.GetUnderlyingType(t) != null;
}
0
7

I had a similar need, and the answer from LukeH pointed me in the direction. I came up with this generic function to make it easy.

    public static Tout CopyValue<Tin, Tout>(Tin from, Tout toPrototype)
    {
        Type underlyingT = Nullable.GetUnderlyingType(typeof(Tout));
        if (underlyingT == null)
        { return (Tout)Convert.ChangeType(from, typeof(Tout)); }
        else
        { return (Tout)Convert.ChangeType(from, underlyingT); }
    }

Usage is like this:

        NotNullableDateProperty = CopyValue(NullableDateProperty, NotNullableDateProperty);

Note the second parameter is just used as a prototype to show the function how to cast the return value, so it doesn't actually have to be the destination property. Meaning you can do also do something like this:

        DateTime? source = new DateTime(2015, 1, 1);
        var dest = CopyValue(source, (string)null);

I did it this way instead of using an out because you can't use out with properties. As is, it can work with properties and variables. You could also create an overload to pass the type instead if you wanted.

2

I did it in this way

public static List<T> Convert<T>(this ExcelWorksheet worksheet) where T : new()
    {
        var result = new List<T>();
        int colCount = worksheet.Dimension.End.Column;  //get Column Count
        int rowCount = worksheet.Dimension.End.Row;

        for (int row = 2; row <= rowCount; row++)
        {
            var obj = new T();
            for (int col = 1; col <= colCount; col++)
            {

                var value = worksheet.Cells[row, col].Value?.ToString();
                PropertyInfo propertyInfo = obj.GetType().GetProperty(worksheet.Cells[1, col].Text);
                propertyInfo.SetValue(obj, Convert.ChangeType(value, Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType), null);

            }
            result.Add(obj);
        }

        return result;
    }
0
1

Thanks @LukeH
I changed a little:

public static object convertToPropType(PropertyInfo property, object value)
{
    object cstVal = null;
    if (property != null)
    {
        Type propType = Nullable.GetUnderlyingType(property.PropertyType);
        bool isNullable = (propType != null);
        if (!isNullable) { propType = property.PropertyType; }
        bool canAttrib = (value != null || isNullable);
        if (!canAttrib) { throw new Exception("Cant attrib null on non nullable. "); }
        cstVal = (value == null || Convert.IsDBNull(value)) ? null : Convert.ChangeType(value, propType);
    }
    return cstVal;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.