AutoMapper:如何从String解析Int并可能根据数据类型创建规则?

我的表单有两个模型,一个ViewModel,以及一个来自它的ControlModel。 ControlModel具有所有相同的字段名称和层次结构,但所有字段都是字符串数据类型。

你如何编写AutoMapper将字符串字段转换为整数? 我尝试了Int32.Parse(myString)但是Int32在表达式中不可用(给出错误)。

Mapper.CreateMap() .ForMember(dest => dest.myInteger, opt => opt.MapFrom(src => src.myString)); 

类中的类型及其对应的转换类型:

string to int,int?,double,double?,DateTime和bool

另外,有没有办法以一种方式来推广映射,使用该函数解析目标中的所有整数? 换句话说,有没有办法为数据类型创建映射?

编辑:

这很有希望:

 AutoMapper.Mapper.CreateMap() .ConvertUsing(src => Convert.ToInt32(src)); 

编辑:这篇文章真的很有帮助

我最终做了这样的事情:

 Mapper.CreateMap().ConvertUsing(); Mapper.CreateMap().ConvertUsing(); Mapper.CreateMap().ConvertUsing(); Mapper.CreateMap().ConvertUsing(); Mapper.CreateMap().ConvertUsing(); Mapper.CreateMap().ConvertUsing(); Mapper.CreateMap().ConvertUsing(); Mapper.CreateMap().ConvertUsing(); Mapper.CreateMap().ConvertUsing(); Mapper.CreateMap().ConvertUsing(); Mapper.CreateMap(); Mapper.Map(mySourceObject, myDestinationObject); 

它引用的类(初稿):

 // TODO: Boil down to two with Generics if possible #region AutoMapTypeConverters // Automap type converter definitions for // int, int?, decimal, decimal?, bool, bool?, Int64, Int64?, DateTime // Automapper string to int? private class NullIntTypeConverter : TypeConverter { protected override int? ConvertCore(string source) { if (source == null) return null; else { int result; return Int32.TryParse(source, out result) ? (int?) result : null; } } } // Automapper string to int private class IntTypeConverter : TypeConverter { protected override int ConvertCore(string source) { if (source == null) throw new MappingException("null string value cannot convert to non-nullable return type."); else return Int32.Parse(source); } } // Automapper string to decimal? private class NullDecimalTypeConverter : TypeConverter { protected override decimal? ConvertCore(string source) { if (source == null) return null; else { decimal result; return Decimal.TryParse(source, out result) ? (decimal?) result : null; } } } // Automapper string to decimal private class DecimalTypeConverter : TypeConverter { protected override decimal ConvertCore(string source) { if (source == null) throw new MappingException("null string value cannot convert to non-nullable return type."); else return Decimal.Parse(source); } } // Automapper string to bool? private class NullBooleanTypeConverter : TypeConverter { protected override bool? ConvertCore(string source) { if (source == null) return null; else { bool result; return Boolean.TryParse(source, out result) ? (bool?) result : null; } } } // Automapper string to bool private class BooleanTypeConverter : TypeConverter { protected override bool ConvertCore(string source) { if (source == null) throw new MappingException("null string value cannot convert to non-nullable return type."); else return Boolean.Parse(source); } } // Automapper string to Int64? private class NullInt64TypeConverter : TypeConverter { protected override Int64? ConvertCore(string source) { if (source == null) return null; else { Int64 result; return Int64.TryParse(source, out result) ? (Int64?)result : null; } } } // Automapper string to Int64 private class Int64TypeConverter : TypeConverter { protected override Int64 ConvertCore(string source) { if (source == null) throw new MappingException("null string value cannot convert to non-nullable return type."); else return Int64.Parse(source); } } // Automapper string to DateTime? // In our case, the datetime will be a JSON2.org datetime // Example: "/Date(1288296203190)/" private class NullDateTimeTypeConverter : TypeConverter { protected override DateTime? ConvertCore(string source) { if (source == null) return null; else { DateTime result; return DateTime.TryParse(source, out result) ? (DateTime?) result : null; } } } // Automapper string to DateTime private class DateTimeTypeConverter : TypeConverter { protected override DateTime ConvertCore(string source) { if (source == null) throw new MappingException("null string value cannot convert to non-nullable return type."); else return DateTime.Parse(source); } } #endregion 

您可以在源类中创建属性,将属性转换为目标中存在的类型。 然后以简单的方式使用AutoMapper。

 public class source { public int _myfield; public string MyField { get { return _myfield.ToString(); } } } public class destination { public string MyField { get; set; } }