According to
, I should be able to create and use an example of a custom type converter in this way: < Previous> I have the following source and destination types: and the following types of converters: It should be a simple test that one of the date properties changes correctly: But before loud I get an exception: I tested this code and it worked using the following code: Pre> and additional converters: Automapper was unable to type string and mapping for string to int, besides I had to remove the following line and I do not know about the "ConstructUsingServiceLocator ()" option, but in this case it works except ... (I do not know that leaving it will lead to other problems Until U till now I have not used this option while working with Optopomper.) Please note that since the Convertment failed I must add the "value 3" parameter ... To change a zero value type Much Mush Can be skewed ... (and I do not know what kind of conversion is happening here ...) var dest = Mapper.Map & lt; Source, Destination & gt; (New source {value = 15}, opt => opt. Constants usage (childContainer.GetInstance);
public square source {public string value1 {get; Set; } Public String Value2 {get; Set; } Public String Value3 {get; Set; }} Public Class Destination {Public Int Value1 {get; Set; } Public Date Time Price 2 {Receive; Set; } Public Type Value3 {get; Set; }}
public class datetime type converters: ITepe Convert & lt; String, DateTime & gt; {Public Date Time Convert (Result Reference Reference) {Return to System. Convert Dondetime (context.SourceValue); }} Public class SourceDestinationTypeConverter: ITypeConverter & lt; Source, Destination & gt; {Convert public destination (resolution reference reference) {var dest = new destination (); // some conversion return destination; }}
System.InvalidCastException: to type the 'SourceDestinationTypeConverter' type of object to be deleted Unable to 'destination' . . My question is now, how can I use custom code converter
ConstructServicesUsing () ?
Public Zero TestMethod1 () {Mapper.CreateMap & lt; String, DateTime & gt; (). Convertible (new dateTimetype converter ()); Mapper.CreatMap & lt; String, type & gt; (). ConvertUzing (new stringantap कनવર ()); Mapper Createmap & lt; String, int & gt; (). ConvertUzing (new string important ()); Mapper Createmap & lt; Source, Destination & gt; (); Var destination = mapper Map & lt; Source, Destination & gt; (New source {value1 = "15", value 2 = "01/01/2000", value 3 = "system string"}, option => option.Construct services usage (type = & gt; new sourceDestinetype Converter ())); Decide. First (destination value 2. years, 2000); }
public class string type converters: ITypeConverter & lt; String, type & gt; {Public type convert (resolution reference context) {return type.GetType (context.SourceValue.ToString ()); }} Public class string import converter: ITypeConverter & lt; String, int & gt; {Public Int Convert (Resolution Contact Reference) {return IN32.Parse (context.SourceValue.ToString ()); }}
Mapper. Createmap & lt; Source, Destination & gt; (). ConstructUsingServiceLocator ();
Mapper. Crescent & lt; Source, Destination & gt; ();
No comments:
Post a Comment