0
public class Trimmer<TModel>
{
public Trimmer()
{
Mapper.Initialize(c =>
{
c.CreateMap<string, string>().ConvertUsing(s => string.IsNullOrEmpty(s) ? s : s.Trim());
c.CreateMap<TModel, TModel>();
});
}
/// <summary>
/// Function take List of object of type TModel what supplied during initalization and applied trim on every property which is string.
/// </summary>
/// <param name="models">An model object of type TModel</param>
/// <returns>List of objects of type TModel with string properties that are trimmed (leading and trailing spaces removed)</returns>
public List<TModel> StringTrimmer(List<TModel> models)
{
if (models == null)
{
return null;
}
var modelList = models.Select(StringTrimmer).ToList();
return modelList;
}
/// <summary>
/// Function take object of type T which one supply during Initalization and applied trim on every property which is string.
/// </summary>
/// <param name="model">An model object of Type TModel</param>
/// <returns>Object of type TModel with string properties that are trimmed (leading and trailing spaces removed)</returns>
public TModel StringTrimmer(TModel model)
{
Mapper.AssertConfigurationIsValid();`enter code here`
var mappedObj = Mapper.Map<TModel,TModel>(model);
return mappedObj;
}
我创建了一个称为Trimmer的泛型类,它带有称为StringTrimmer的重载方法。这些方法的意图是使用Automapper修剪Tmodel对象属性的任何空间。它的工作很好,但有时则这些方法我得到了以下错误:使用自动映射器修剪对象属性期间的零星错误
Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type.
当不应该发生,因为我同一个对象类型转换为同一对象类型。
什么是TModel的是造成这个错误? –
TModel只是泛型类型。在我的情况下,它只是我想要修剪的对象属性的类名。例如,如果我有一个“甲板”类,那么我会做“var obj = new Trimmer()”。 –
@TimothyGhanem我确实发现了问题并回答了问题,因为我偶然发现错误。这是代码会给我错误的许多可能的测试案例之一。解决方案是移动Mapper.AssertConfigurationIsValid();在Mapper.Initialize中 –