C# 可为null的double和int之间的转换失败

C# 可为null的double和int之间的转换失败,c#,reflection,type-conversion,C#,Reflection,Type Conversion,我有一个模型,FittingProject,我正试图将它映射到另一个模型,fittingprojectsharepointmodel 不幸的是FittingProject和fittingprojectsharepointmodel仅共享值,属性名称和类型都不同 为了简化映射过程,我为FittingProject创建了一个自定义属性,用于查找fittingprojectsharepointmodel上的匹配属性。问题是大多数值在转换过程中都会失败,例如从int到double? 正如您从下面的代码片

我有一个模型,
FittingProject
,我正试图将它映射到另一个模型,
fittingprojectsharepointmodel

不幸的是
FittingProject
fittingprojectsharepointmodel
仅共享值,属性名称和类型都不同

为了简化映射过程,我为
FittingProject
创建了一个自定义属性,用于查找
fittingprojectsharepointmodel
上的匹配属性。问题是大多数值在转换过程中都会失败,例如从
int
double?

正如您从下面的代码片段中看到的,我尝试使用
Convert.ChangeType
,但仍然失败,出现了相同的异常

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;
}

ChangeType不支持以下情况:

Convert.ChangeType(1234, typeof(double)) //works
Convert.ChangeType(1234, typeof(double?)) //fails

因此,您可能需要自己进行可为空的处理。只需使用
nullable.getUnderlineType
从目标类型中删除nullable属性即可。您还需要处理这样的情况,即要转换的值为
null

ChangeType不支持以下情况:

Convert.ChangeType(1234, typeof(double)) //works
Convert.ChangeType(1234, typeof(double?)) //fails
因此,您可能需要自己进行可为空的处理。只需使用
nullable.getUnderlineType
从目标类型中删除nullable属性即可。您还需要处理这样的情况,即要转换的值是
null

,您可以使用。像这样:

考虑以下示例模型:

public class FittingProject
{
    public string Name { get; set; }
    public int Size { get; set; }
}

public class FittingsProjectSharepointModel
{
    public string Display { get; set; }
    public double? Volume { get; set; }
}
现在,您可以在模型之间创建一个映射,如下所示:

AutoMapper.Mapper.CreateMap<FittingProject, FittingsProjectSharepointModel>()
    .ForMember(dest => dest.Display, opts => opts.MapFrom(src => src.Name)) //Map Name to Display
    .ForMember(dest => dest.Volume, opts => opts.MapFrom(src => src.Size)); //Map Size to Volume
AutoMapper.Mapper.CreateMap()
.ForMember(dest=>dest.Display,opts=>opts.MapFrom(src=>src.Name))//要显示的映射名称
.ForMember(dest=>dest.Volume,opts=>opts.MapFrom(src=>src.Size))//将大小映射到体积
以下是如何转换:

FittingProject fitting_project = new FittingProject()
{
    Name ="project_name",
    Size = 16
};

FittingsProjectSharepointModel sharepoint_model =
    AutoMapper.Mapper.Map<FittingsProjectSharepointModel>(fitting_project);
FittingProject fitting\u project=新FittingProject()
{
Name=“项目名称”,
尺寸=16
};
FittingsProjectSharepointModel sharepoint\U模型=
AutoMapper.Mapper.Map(fitting_项目);
您可以使用。像这样:

考虑以下示例模型:

public class FittingProject
{
    public string Name { get; set; }
    public int Size { get; set; }
}

public class FittingsProjectSharepointModel
{
    public string Display { get; set; }
    public double? Volume { get; set; }
}
现在,您可以在模型之间创建一个映射,如下所示:

AutoMapper.Mapper.CreateMap<FittingProject, FittingsProjectSharepointModel>()
    .ForMember(dest => dest.Display, opts => opts.MapFrom(src => src.Name)) //Map Name to Display
    .ForMember(dest => dest.Volume, opts => opts.MapFrom(src => src.Size)); //Map Size to Volume
AutoMapper.Mapper.CreateMap()
.ForMember(dest=>dest.Display,opts=>opts.MapFrom(src=>src.Name))//要显示的映射名称
.ForMember(dest=>dest.Volume,opts=>opts.MapFrom(src=>src.Size))//将大小映射到体积
以下是如何转换:

FittingProject fitting_project = new FittingProject()
{
    Name ="project_name",
    Size = 16
};

FittingsProjectSharepointModel sharepoint_model =
    AutoMapper.Mapper.Map<FittingsProjectSharepointModel>(fitting_project);
FittingProject fitting\u project=新FittingProject()
{
Name=“项目名称”,
尺寸=16
};
FittingsProjectSharepointModel sharepoint\U模型=
AutoMapper.Mapper.Map(fitting_项目);

谢谢您的回复。然而,这种方法需要相当多的重构,这并不是我能考虑的。不过,下次我会考虑使用AutoMapper。谢谢您的回复。然而,这种方法需要相当多的重构,这并不是我能考虑的。不过,下次我会考虑使用AutoMapper。