@@ -63,8 +63,9 @@ public static GetMemberDelegate GetConverter(Type fromType, Type toType)
6363 ? converter
6464 : null ;
6565 }
66-
67- public static T ConvertTo < T > ( this object from )
66+
67+ public static T ConvertTo < T > ( this object from ) => from . ConvertTo < T > ( skipConverters : false ) ;
68+ public static T ConvertTo < T > ( this object from , bool skipConverters )
6869 {
6970 if ( from == null )
7071 return default ( T ) ;
@@ -73,7 +74,7 @@ public static T ConvertTo<T>(this object from)
7374 if ( fromType == typeof ( T ) )
7475 return ( T ) from ;
7576
76- return ( T ) ConvertTo ( from , typeof ( T ) ) ;
77+ return ( T ) ConvertTo ( from , typeof ( T ) , skipConverters ) ;
7778 }
7879
7980 public static T CreateCopy < T > ( this T from )
@@ -83,7 +84,7 @@ public static T CreateCopy<T>(this T from)
8384
8485 if ( typeof ( IEnumerable ) . IsAssignableFrom ( typeof ( T ) ) )
8586 {
86- var listResult = TryConvertCollections ( from . GetType ( ) , typeof ( T ) , from ) ;
87+ var listResult = TranslateListWithElements . TryTranslateCollections ( from . GetType ( ) , typeof ( T ) , from ) ;
8788 return ( T ) listResult ;
8889 }
8990
@@ -97,29 +98,38 @@ public static To ThenDo<To>(this To to, Action<To> fn)
9798 return to ;
9899 }
99100
100- public static object ConvertTo ( this object from , Type type )
101+ public static object ConvertTo ( this object from , Type toType ) => from . ConvertTo ( toType , skipConverters : false ) ;
102+ public static object ConvertTo ( this object from , Type toType , bool skipConverters )
101103 {
102104 if ( from == null )
103105 return null ;
104106
105- if ( from . GetType ( ) == type )
107+ var fromType = from . GetType ( ) ;
108+ if ( fromType == toType )
106109 return from ;
107110
108- if ( from . GetType ( ) . IsValueType || type . IsValueType )
109- return ChangeValueType ( from , type ) ;
111+ if ( ! skipConverters )
112+ {
113+ var converter = GetConverter ( fromType , toType ) ;
114+ if ( converter != null )
115+ return converter ( from ) ;
116+ }
117+
118+ if ( fromType . IsValueType || toType . IsValueType )
119+ return ChangeValueType ( from , toType ) ;
110120
111121 if ( from is string str )
112- return TypeSerializer . DeserializeFromString ( str , type ) ;
122+ return TypeSerializer . DeserializeFromString ( str , toType ) ;
113123 if ( from is ReadOnlyMemory < char > rom )
114- return TypeSerializer . DeserializeFromSpan ( type , rom . Span ) ;
124+ return TypeSerializer . DeserializeFromSpan ( toType , rom . Span ) ;
115125
116- if ( typeof ( IEnumerable ) . IsAssignableFrom ( type ) )
126+ if ( typeof ( IEnumerable ) . IsAssignableFrom ( toType ) )
117127 {
118- var listResult = TryConvertCollections ( from . GetType ( ) , type , from ) ;
128+ var listResult = TryConvertCollections ( fromType , toType , from ) ;
119129 return listResult ;
120130 }
121131
122- var to = type . CreateInstance ( ) ;
132+ var to = toType . CreateInstance ( ) ;
123133 return to . PopulateWithNonDefaultValues ( from ) ;
124134 }
125135
0 commit comments