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

标签 c# automapper casting

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

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

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

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

字符串到 int、int?、double、double?、DateTime 和 bool

此外,是否有任何方法可以通过使用该函数解析目标中的所有整数来概括映射?换句话说,有没有办法为数据类型创建映射?

编辑:

这看起来很有希望:

AutoMapper.Mapper.CreateMap<string, int>()
          .ConvertUsing(src => Convert.ToInt32(src));

编辑: 这post真的很有帮助

最佳答案

我最终做了这样的事情:

Mapper.CreateMap<string, int>().ConvertUsing<IntTypeConverter>();
Mapper.CreateMap<string, int?>().ConvertUsing<NullIntTypeConverter>();
Mapper.CreateMap<string, decimal?>().ConvertUsing<NullDecimalTypeConverter>();
Mapper.CreateMap<string, decimal>().ConvertUsing<DecimalTypeConverter>();
Mapper.CreateMap<string, bool?>().ConvertUsing<NullBooleanTypeConverter>();
Mapper.CreateMap<string, bool>().ConvertUsing<BooleanTypeConverter>();
Mapper.CreateMap<string, Int64?>().ConvertUsing<NullInt64TypeConverter>();
Mapper.CreateMap<string, Int64>().ConvertUsing<Int64TypeConverter>();
Mapper.CreateMap<string, DateTime?>().ConvertUsing<NullDateTimeTypeConverter>();
Mapper.CreateMap<string, DateTime>().ConvertUsing<DateTimeTypeConverter>();

Mapper.CreateMap<SourceClass, DestClass>();

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<string, int?>
{   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<string, int>
{   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<string, decimal?>
{   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<string, decimal>
{   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<string, bool?>
{   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<string, bool>
{   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<string, Int64?>
{   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<string, Int64>
{   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<string, DateTime?>
{   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<string, DateTime>
{   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

关于c# - AutoMapper:如何从 String 解析 Int 并可能根据数据类型创建规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4101516/

相关文章:

c# - API - 用单词片段过滤大列表

c# - 使用 DTO 和实体是否违反 DRY 原则?

c# - 如何在 Automapper 中从源代码制作部分 map

c# - 如何使用 AutoMapper 映射子列表对象

c# - 在 Windows 窗体中将 int 转换为 Color

php - 类型转换函数参数

c# - 如何让代码在 Response.end 之后执行

c# - 具有多个可选一对一关系的 Entity Framework 表

C# 进程终止通知

c++ - 为什么unique_ptr不允许const_cast?