Return to total directory
Custom type Conversions
Sometimes, you need to fully control the conversion of one type to another. A type is not like another type at all, and the conversion function already exists, in which case you want to convert from a "loose" type to a stronger type, such as a string source type to a int32 target type.
Here are two classes of source and destination, to map the former to the latter, the code is as follows:
public class source{ string Value1 {get ; set public string Value2 {get ; set public string Value3 {get ; set
Public class destination{ publicintgetset;} Public Get Set ; } Public Get Set ; }}
By the time the official documentation was described, "because AutoMapper does not know the mapping from string to Int,datetime or type, AutoMapper throws an exception if the mapping is to be attempted (when mapping and configuration checking)." to create these types of mappings, we have to provide a custom type converter, and we have three ways to do this:
void Convertusing (Func<tsource, tdestination>void convertusing (Itypeconverter<tsource, tdestination>voidwhere Ttypeconverter:itypeconverter<tsource, tdestination>;
However, I do not implement a custom type converter first try:
namespacethirdautomapper{classProgram {Static voidMain (string[] args) {Mapper.createmap<source, destination>(); varSource =NewSource {Value1="5", Value2="05/11/2015", Value3="Thirdautomapper.source" }; varDestination = mapper.map<destination>(source); Console.WriteLine ("destination. VALUE1={0}", destination. VALUE1); Console.WriteLine ("destination. VALUE2={0}", destination. Value2); Console.read (); } }}
The following error occurred, but from the error it appears that there was an error only when mapping from string to type type, what are the other two types?
Now let's comment out the third attribute of the source type and target type Value3, and comment out the Value3 property of the Source type object before the mapping, and try again.
Sure enough, the mapping was successful. Tell me about the latest version of AutoMapper. By default, string types can be mapped to int and datetime types. However, a string cannot be mapped to type.
For the three conversion methods given by the AutoMapper API above, the first option is simply to enter a function that returns a destination for a source, which is valid for a simple occasion, but becomes difficult to handle for larger cases. In more complex cases, we can create a custom type converter by implementing the Itypeconverter<tsource, Tdestination> interface:
The first method:
public class Customtypeconverter: Itypeconverter<source, Destination>{ Destination Convert (Resolutioncontext context) {Source src = Context. Sourcevalue as Source; var dest = new Destination {Value1 = System.Convert.ToInt32 (src. Value1), Value2 = System.Convert.ToDateTime (src. Value2), Value3 = context. SourceType}; return dest; }}
It then gives AutoMapper a custom type of converter or a simplified type that automapper can instantiate at run time. The mapping configuration for the source/target type above is implemented:
Modify the code in the Main method to:
Static void Main (string[] args) { mapper.createmap<source, destination> (). Convertusing<customtypeconverter>(); var New Source { "5", "05/11/2015" , Value3 = typeof (Source). ToString ()
}; var destination = mapper.map<destination>(source); Console.WriteLine ("destination. value1={0}", destination. VALUE1); Console.WriteLine ("destination. value2={0}", destination. Value2); Console.WriteLine (destination. Value3.tostring ()= =source. VALUE3); Console.WriteLine (destination. VALUE3); Console.read ();}
The test results are as follows and the mapping succeeds!
The second method:
Each type implements the Itypeconverter interface separately:
Public classdatetimetypeconverter:itypeconverter<string,datetime>{ PublicDateTime Convert (Resolutioncontext context) {returnSystem.Convert.ToDateTime (context. Sourcevalue); }} Public classtypetypeconverter:itypeconverter<string, type>{ PublicType Convert (Resolutioncontext context) {returnContext. Sourcevalue = =typeof(Source). ToString ()?typeof(Source):typeof(Destination); }}
Static voidMain (string[] args) { //I see a lot of this kind of writing on the Internet, including official documents, but I write a compilation error, perhaps the current automapper do not support it//mapper.createmap<string, int> (). Convertusing (Convert.ToInt32);Mapper.createmap<string,int> (). Convertusing (context, intnum) =Convert.ToInt32 (context. Sourcevalue)); Mapper.createmap<string, Datetime> (). Convertusing (NewDatetimetypeconverter (). Convert); Mapper.createmap<string, Type> (). Convertusing<typetypeconverter>(); Mapper.createmap<source, destination>(); //Mapper.createmap<source, destination> (). Convertusing<customtypeconverter> (); varSource =NewSource {Value1="5", Value2="05/11/2015", Value3=typeof(Source). ToString ()}; varDestination = mapper.map<destination>(source); Console.WriteLine ("destination. VALUE1={0}", destination. VALUE1); Console.WriteLine ("destination. VALUE2={0}", destination. Value2); Console.WriteLine (destination. Value3.tostring ()==source. VALUE3); Console.WriteLine (destination. VALUE3); Console.read ();}
The test results are also perfectly passed.
AutoMapper (iii)