.net - C# AutoMapper exception when mapping nullable byte to enum -


i'm using automapper (http://automapper.org/) automatically map 1 object other.

source object:

public partial class srcobject {     public int id { get; set; }      ...      public nullable<byte> qcrea { get; set; }     public nullable<byte> qcpyg { get; set; }      ...  } 

destination object:

[datacontract] public class dstobject {     [datamember]     public int id { get; set; }      ...      [datamember]     public qualitycontrolstate qcrea { get; set; }      [datamember]     public qualitycontrolstate qcpyg { get; set; }      ... } 

enum:

public enum qualitycontrolstate {     ok = 0,     warning = 1,     error = 2 } 

mappers:

mapper.createmap<datetime, nullable<datetime>>().constructusing(messagedtoconverter.tonullable); mapper.createmap<float, nullable<float>>().constructusing(messagedtoconverter.tonullable); mapper.createmap<short, nullable<short>>().constructusing(messagedtoconverter.tonullable); mapper.createmap<qualitycontrolstate, nullable<byte>>().constructusing(messagedtoconverter.tonullable); mapper.createmap<nullable<byte>, qualitycontrolstate>().constructusing(messagedtoconverter.toqualitycontrolstate); mapper.createmap<byte, nullable<byte>>().constructusing(messagedtoconverter.tonullable); mapper.createmap<bool, nullable<bool>>().constructusing(messagedtoconverter.tonullable); mapper.createmap<int, nullable<int>>().constructusing(messagedtoconverter.tonullable);  mapper.createmap<srcobject, dstobject>()     .formember(d => d.rawresults, s => s.ignore())     .formember(d => d.correctedimage, s => s.ignore())     .formember(d => d.segmentedimage, s => s.ignore())     .formember(d => d.qcrearotationanglevertical, s => s.ignore())     .formember(d => d.reatohcw, s => s.ignore());  mapper.assertconfigurationisvalid(); 

mapping methods:

public static qualitycontrolstate toqualitycontrolstate(nullable<byte> value) {     return (value != null) ? (qualitycontrolstate)enum.parse(typeof(qualitycontrolstate), value.tostring()) : qualitycontrolstate.ok; }  public static nullable<datetime> tonullable(datetime value) {     return (value != null) ? value : new datetime(); }  public static nullable<float> tonullable(float value) {     return (value != null) ? value : 0.0f; }  public static nullable<short> tonullable(short value) {     return (value != null) ? value : (short)0; }  public static nullable<byte> tonullable(qualitycontrolstate value) {     return (value != null) ? (byte)value : (byte)0; }  public static nullable<byte> tonullable(byte value) {     return (value != null) ? value : (byte)0; }  public static nullable<bool> tonullable(bool value) {     return (value != null) ? value : false; }  public static nullable<int> tonullable(int value) {     return (value != null) ? value : 0; } 

exception:

_innerexception =  {"object reference not set instance of object."}  base {system.exception} = {"\r\n\r\nmapping types:\r\nnullable`1 -> qualitycontrolstate\r\nsystem.nullable`1[[system.byte, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089]] -> qualitycontrolstate\r\n\r\ndestination path:\r...  membername = "qcrea"  sourcetype = {name = "nullable`1" fullname = "system.nullable`1[[system.byte, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089]]"}  destinationtype = {name = "qualitycontrolstate" fullname = "qualitycontrolstate"} 

seems custom mapper "nullable enum" not called.

mapper.assertconfigurationisvalid() has no problems.

but why? , how convert this?

any ideas? thank you

try using typeconverter. should work:

public class qualitycontrolstatetypeconverter                            : typeconverter<nullable<byte>, qualitycontrolstate> {     protected override qualitycontrolstate convertcore(nullable<byte> source)     {         return (source != null)             ? (qualitycontrolstate)enum.parse(typeof(qualitycontrolstate),                                               source.tostring())             : qualitycontrolstate.ok;     } } 

use in mapping configuration this:

mapper.createmap<nullable<byte>, qualitycontrolstate>()       .convertusing(new qualitycontrolstatetypeconverter()); 

i tested code , seemed work fine:

var qcs = mapper.map<nullable<byte>, qualitycontrolstate>(null); // ok qcs = mapper.map<nullable<byte>, qualitycontrolstate>(0);        // ok qcs = mapper.map<nullable<byte>, qualitycontrolstate>(1);        // warning qcs = mapper.map<nullable<byte>, qualitycontrolstate>(2);        // error qcs = mapper.map<nullable<byte>, qualitycontrolstate>(3);        // 3 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -