似乎无法使用c#中的最新autoMapper库映射自定义类型

时间:2011-09-19 18:32:51

标签: automapper

我似乎无法使用c#中的最新autoMapper库映射自定义类型。我正在使用示例应用程序中的示例,但添加了自定义类型。两个类都相似,只是代码名称不同。我确实希望这是一个“GlobalTypeConverter”,因为它使用了几次。

错误在于验证: //错误:未映射TestApp.Form1 + Destination上的以下1个属性:     mycode的 在TestApp.Form1 + Source。

上添加自定义映射表达式,忽略或重命名该属性
public class Source
{
  public string Value1 { get; set; }
  public string Value2 { get; set; }
  public string Value3 { get; set; }
  public standardCoding stdcode { get; set; }
}

public class Destination
{
  public int Value1 { get; set; }
  public DateTime Value2 { get; set; }
  public Type Value3 { get; set; }
  public myClass.code mycode { get; set; }
}

public class DateTimeTypeConverter : TypeConverter<string, DateTime>
{
  protected override DateTime ConvertCore(string source)
  {
    return System.Convert.ToDateTime(source);
  }
}

public class TypeTypeConverter : TypeConverter<string, Type>
{
  protected override Type ConvertCore(string source)
  {
    Type type = Assembly.GetExecutingAssembly().GetType(source);
    return type;
  }
}

public class standardCodingConverter : TypeConverter<standardCoding, myClass.code>
{
  protected override myClass.code ConvertCore(standardCoding source)
  {
    var result = new myClass.code();
    result.CodingSystem = source.StandardCodingSystem;
    result.Description = source.StandardCodeDescription;
    result.value = source.StandardCode;
    return result;
  }
}


public void btnAutoMapperTest_Click(object sender, EventArgs e)
{
  Mapper.CreateMap<string, int>().ConvertUsing(Convert.ToInt32);
  Mapper.CreateMap<string, DateTime>().ConvertUsing(new DateTimeTypeConverter());
  Mapper.CreateMap<string, Type>().ConvertUsing<TypeTypeConverter>();
  Mapper.CreateMap<standardCoding, myClass.code>().ConvertUsing(new standardCodingConverter());
  Mapper.CreateMap<Source, Destination>();
  Mapper.AssertConfigurationIsValid();

  var newcode = new standardCoding();
  newcode.StandardCode = "123";
  newcode.StandardCodeDescription = "my desc";
  newcode.StandardCodingSystem = "CodeSys";

  var source = new Source
  {
    Value1 = "5",
    Value2 = "01/01/2000",
    Value3 = "AutoMapperSamples.GlobalTypeConverters.GlobalTypeConverters+Destination", 
    stdcode = newcode
  };




/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "cds_dt")]
public partial class standardCoding
{

private string standardCodingSystemField;

private string standardCodeField;

private string standardCodeDescriptionField;

/// <remarks/>
public string StandardCodingSystem
{
  get
  {
    return this.standardCodingSystemField;
  }
  set
  {
    this.standardCodingSystemField = value;
  }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType = "token")]
public string StandardCode
{
  get
  {
    return this.standardCodeField;
  }
  set
  {
    this.standardCodeField = value;
  }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType = "token")]
public string StandardCodeDescription
{
  get
  {
    return this.standardCodeDescriptionField;
  }
  set
  {
    this.standardCodeDescriptionField = value;
  }
}
}

1 个答案:

答案 0 :(得分:0)

我在网上看到了它(示例代码“CustomValueResolvers.cs”对我来说不起作用)。

我的错误路线是: ... .ForMember(src =&gt; src.DiagnosisCode,opt =&gt; opt.ResolveUsing()。FromMember(r =&gt; r.DiagnosisProcedureCode))