我有一个模型FittingProject
,我正在尝试将其映射到另一个模型FittingsProjectSharepointModel
。
不幸的是FittingProject
和FittingsProjectSharepointModel
只共享值,属性名称和类型都不同。
为了简化映射过程,我为FittingProject
创建了一个自定义属性,用于查找FittingsProjectSharepointModel
上的匹配属性。问题是大多数值在转换期间失败,例如从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;
}
答案 0 :(得分:0)
ChangeType不支持:
Convert.ChangeType(1234, typeof(double)) //works
Convert.ChangeType(1234, typeof(double?)) //fails
所以你可能需要做自己的可空处理。只需使用Nullable.GetUnderlyingType
从目标类型中删除可空属性即可。您还需要处理这种情况,即要转换的值为null
。
答案 1 :(得分:0)
您可以使用AutoMapper。像这样:
考虑以下示例模型:
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
以下是您可以转换的方式:
FittingProject fitting_project = new FittingProject()
{
Name ="project_name",
Size = 16
};
FittingsProjectSharepointModel sharepoint_model =
AutoMapper.Mapper.Map<FittingsProjectSharepointModel>(fitting_project);