samedi 26 septembre 2015

C#: Conversion fails between nullable double and int

I have a model, FittingProject, which I'm trying to map to another model, FittingsProjectSharepointModel. Unfortunately FittingProject and FittingsProjectSharepointModel only share values, both property names and types are different.

To ease the mapping process I have created a custom attribute for FittingProject which I use to look up the matching property on FittingsProjectSharepointModel. The problem is that most values fail during conversion, for example going from int to double?.

As you can see by snippet below I have attempt to use the Convert.ChangeType, but it still fails with the same Exception.

public ModelMapper MapModelToFittingsProjectSharepointModel(object model)
{
    if (model == null)
    {
        return null;
    }

    var propertiesWithCustomAttributes = model
        .GetType()
        .GetProperties()
        .Where(p => p.GetCustomAttributes(typeof(SharepointModelPropertyAttribute), true).Length > 0);

    foreach (var prop in propertiesWithCustomAttributes)
    {
        foreach (var customAttribute in prop.GetCustomAttributes(true))
        {
            SharepointModelPropertyAttribute sharePointModelPropertyAttribute = 
                customAttribute as SharepointModelPropertyAttribute;

            PropertyDescriptor sharePointModelprop = TypeDescriptor
                .GetProperties(typeof(FittingsProjectSharepointModel))
                .Find(sharePointModelPropertyAttribute.SharepointModelProperty, false);

            try
            {
                var projectValue = prop.GetValue(model);
                var projectValueConverted = Convert.ChangeType(projectValue, sharePointModelprop.PropertyType);

                sharePointModelprop.SetValue(FittingsProjectSharepointModel, projectValueConverted);
            }
            catch (Exception ex)
            {
                // 
            }


        }
    }
    return this;
}





Aucun commentaire:

Enregistrer un commentaire