jb.
jb.

Reputation: 1918

Converting and Casting an object based on a [Type] object

Hopefully a simple enough question, but I am struggling to find an answer.

How can I cast an 'object' to a type defined in a 'Type' object.

For example, basically I am trying to do the following with reflection, making it as generic as possible:

public class MyClass
{
    public string Prop1 { get; set; }
    public int Prop2 { get; set; }
}


public T FillMyClass<T>() where T : new()
{
    //The definitions here are suppled in code - each class we want to work with needs to be "mapped".
    string name1 = "Prop1";
    Type type1 = typeof(string);
    string name2 = "Prop2";
    Type type2 = typeof(int);

    //The values always start out as a string because of the way I'm receiving it.
    string val1 = "test";
    string val2 = "1";

    T t= new T();

    //Works fine, because val1 is already a string
    t.GetType().GetProperty(name1).SetValue(t, val1, null);

    //Having trouble with the below.
    object o = Convert.ChangeType(val2, type2);

    //Fails because o is not an int
    t.GetType().GetProperty(name2).SetValue(t, o, null);

}

So the type is defined by the user (or possibly even just by looking up the type of the property). But I just can't see how to cast the object to a [Type].

Upvotes: 2

Views: 620

Answers (1)

Tejs
Tejs

Reputation: 41266

I've come across this issue as well a couple of times. While I'm sure there is a more elegant solution to this problem, I've made the following extension method to get you 99% of the way there.

    public static object TryConvertToType(this object source, Type destinationType, object defaultValue = null)
    {
        try
        {
            if (source == null)
                return defaultValue;

            if (destinationType == typeof(bool))
            {
                bool returnValue = false;

                if (!bool.TryParse(source.ToString(), out returnValue))
                {
                    return Convert.ChangeType(source.ToString() == "1", destinationType);
                }
                else
                {
                    return Convert.ChangeType(returnValue, destinationType);
                }
            }
            else if (destinationType.IsSubclassOf(typeof(Enum)))
            {
                try
                {
                    return Enum.Parse(destinationType, source.ToString());
                }
                catch
                {
                    return Enum.ToObject(destinationType, source);
                }
            }
            else if (destinationType == typeof(Guid))
            {
                return Convert.ChangeType(new Guid(source.ToString().ToUpper()), destinationType);
            }
            else if (destinationType.IsGenericType && destinationType.GetGenericTypeDefinition() == typeof(Nullable<>))
            {
                Type genericType = destinationType.GetGenericArguments().First();

                return Convert.ChangeType(source, genericType);
            }
            else if (source.GetType().IsSubclassOf(destinationType))
            {
                return Convert.ChangeType(source, destinationType);
            }
            else if (!source.GetType().IsValueType
                && source.GetType() != typeof(string)
                && destinationType == typeof(string))
            {
                return Convert.ChangeType(source.GetType().Name, destinationType);
            }
            else
            {
                return Convert.ChangeType(source, destinationType);
            }
        }
        catch
        {
            return defaultValue;
        }
    }

Basically, in usage, it works like so:

t.GetType().GetProperty(name).GetValue(t, null).TryConvertToType(type2, 0);

Upvotes: 2

Related Questions