本文介绍了AutoMapper:如何解析从字符串为int,可以根据数据类型创建规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个型号为我的形式,一个ViewModel将把它和ControlModel从它的到来。该ControlModel具有所有的相同的字段名和等级,但所有的字段是一个字符串数据类型。

I have two models for my form, a ViewModel going to it, and a ControlModel coming from it. The ControlModel has all the same field names and hierarchy, but all of the fields are a string data type.

你会如何编写AutoMapper将字符串字段转换为整数?我试图Int32.Parse(MyString的),但的Int32是不可用的表达式中(给出一个错误)。

How would you code AutoMapper to convert a string field to integer? I tried Int32.Parse(myString) but Int32 is not available within the expression (gives an error).

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



在类的类型及其相应的转换类型:

The types in the class and their corresponding conversion types:

字符串,INT,INT?一倍,两倍?日期时间和bool

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

此外,有没有什么办法的方式来概括映射所有在目标整数均与该函数解析?换句话说,有没有方法来创建数据类型映射

Additionally, is there any way to generalize mappings in a way that all integers in the target are parsed with that function? In other words, is there a way to create mappings for data types?

编辑:

这看起来很有希望

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



编辑:
这样的是真正有用的。

推荐答案

最后我做这样的事情:

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

和它引用的类(初稿):

And the classes it references (first draft):

// 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

这篇关于AutoMapper:如何解析从字符串为int,可以根据数据类型创建规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 03:18