2020
2121namespace ServiceStack . Text
2222{
23- public static class TranslateListWithElements
24- {
23+ public static class TranslateListWithElements
24+ {
2525 private static Dictionary < Type , ConvertInstanceDelegate > TranslateICollectionCache
2626 = new Dictionary < Type , ConvertInstanceDelegate > ( ) ;
2727
28- public static object TranslateToGenericICollectionCache ( object from , Type toInstanceOfType , Type elementType )
29- {
28+ public static object TranslateToGenericICollectionCache ( object from , Type toInstanceOfType , Type elementType )
29+ {
3030 ConvertInstanceDelegate translateToFn ;
3131 if ( TranslateICollectionCache . TryGetValue ( toInstanceOfType , out translateToFn ) )
3232 return translateToFn ( from , toInstanceOfType ) ;
@@ -45,16 +45,16 @@ public static object TranslateToGenericICollectionCache(object from, Type toInst
4545 } while ( ! ReferenceEquals (
4646 Interlocked . CompareExchange ( ref TranslateICollectionCache , newCache , snapshot ) , snapshot ) ) ;
4747
48- return translateToFn ( from , toInstanceOfType ) ;
49- }
48+ return translateToFn ( from , toInstanceOfType ) ;
49+ }
5050
5151 private static Dictionary < ConvertibleTypeKey , ConvertInstanceDelegate > TranslateConvertibleICollectionCache
5252 = new Dictionary < ConvertibleTypeKey , ConvertInstanceDelegate > ( ) ;
5353
54- public static object TranslateToConvertibleGenericICollectionCache (
55- object from , Type toInstanceOfType , Type fromElementType )
56- {
57- var typeKey = new ConvertibleTypeKey ( toInstanceOfType , fromElementType ) ;
54+ public static object TranslateToConvertibleGenericICollectionCache (
55+ object from , Type toInstanceOfType , Type fromElementType )
56+ {
57+ var typeKey = new ConvertibleTypeKey ( toInstanceOfType , fromElementType ) ;
5858 ConvertInstanceDelegate translateToFn ;
5959 if ( TranslateConvertibleICollectionCache . TryGetValue ( typeKey , out translateToFn ) ) return translateToFn ( from , toInstanceOfType ) ;
6060
@@ -72,105 +72,105 @@ public static object TranslateToConvertibleGenericICollectionCache(
7272
7373 } while ( ! ReferenceEquals (
7474 Interlocked . CompareExchange ( ref TranslateConvertibleICollectionCache , newCache , snapshot ) , snapshot ) ) ;
75-
75+
7676 return translateToFn ( from , toInstanceOfType ) ;
77- }
77+ }
7878
7979 public static object TryTranslateCollections ( Type fromPropertyType , Type toPropertyType , object fromValue )
80- {
80+ {
8181 var args = typeof ( IEnumerable < > ) . GetGenericArgumentsIfBothHaveSameGenericDefinitionTypeAndArguments (
82- fromPropertyType , toPropertyType ) ;
82+ fromPropertyType , toPropertyType ) ;
8383
8484 if ( args != null )
85- {
86- return TranslateToGenericICollectionCache (
87- fromValue , toPropertyType , args [ 0 ] ) ;
88- }
85+ {
86+ return TranslateToGenericICollectionCache (
87+ fromValue , toPropertyType , args [ 0 ] ) ;
88+ }
8989
9090 var varArgs = typeof ( IEnumerable < > ) . GetGenericArgumentsIfBothHaveConvertibleGenericDefinitionTypeAndArguments (
91- fromPropertyType , toPropertyType ) ;
92-
93- if ( varArgs != null )
94- {
95- return TranslateToConvertibleGenericICollectionCache (
96- fromValue , toPropertyType , varArgs . Args1 [ 0 ] ) ;
97- }
98-
99- return null ;
100- }
101-
102- }
103-
104- public class ConvertibleTypeKey
105- {
106- public Type ToInstanceType { get ; set ; }
107- public Type FromElemenetType { get ; set ; }
108-
109- public ConvertibleTypeKey ( )
110- {
111- }
112-
113- public ConvertibleTypeKey ( Type toInstanceType , Type fromElemenetType )
114- {
115- ToInstanceType = toInstanceType ;
116- FromElemenetType = fromElemenetType ;
117- }
118-
119- public bool Equals ( ConvertibleTypeKey other )
120- {
121- if ( ReferenceEquals ( null , other ) ) return false ;
122- if ( ReferenceEquals ( this , other ) ) return true ;
123- return Equals ( other . ToInstanceType , ToInstanceType ) && Equals ( other . FromElemenetType , FromElemenetType ) ;
124- }
125-
126- public override bool Equals ( object obj )
127- {
128- if ( ReferenceEquals ( null , obj ) ) return false ;
129- if ( ReferenceEquals ( this , obj ) ) return true ;
130- if ( obj . GetType ( ) != typeof ( ConvertibleTypeKey ) ) return false ;
131- return Equals ( ( ConvertibleTypeKey ) obj ) ;
132- }
133-
134- public override int GetHashCode ( )
135- {
136- unchecked
137- {
138- return ( ( ToInstanceType != null ? ToInstanceType . GetHashCode ( ) : 0 ) * 397 )
139- ^ ( FromElemenetType != null ? FromElemenetType . GetHashCode ( ) : 0 ) ;
140- }
141- }
142- }
143-
144- public class TranslateListWithElements < T >
145- {
146- public static object CreateInstance ( Type toInstanceOfType )
147- {
91+ fromPropertyType , toPropertyType ) ;
92+
93+ if ( varArgs != null )
94+ {
95+ return TranslateToConvertibleGenericICollectionCache (
96+ fromValue , toPropertyType , varArgs . Args1 [ 0 ] ) ;
97+ }
98+
99+ return null ;
100+ }
101+
102+ }
103+
104+ public class ConvertibleTypeKey
105+ {
106+ public Type ToInstanceType { get ; set ; }
107+ public Type FromElemenetType { get ; set ; }
108+
109+ public ConvertibleTypeKey ( )
110+ {
111+ }
112+
113+ public ConvertibleTypeKey ( Type toInstanceType , Type fromElemenetType )
114+ {
115+ ToInstanceType = toInstanceType ;
116+ FromElemenetType = fromElemenetType ;
117+ }
118+
119+ public bool Equals ( ConvertibleTypeKey other )
120+ {
121+ if ( ReferenceEquals ( null , other ) ) return false ;
122+ if ( ReferenceEquals ( this , other ) ) return true ;
123+ return Equals ( other . ToInstanceType , ToInstanceType ) && Equals ( other . FromElemenetType , FromElemenetType ) ;
124+ }
125+
126+ public override bool Equals ( object obj )
127+ {
128+ if ( ReferenceEquals ( null , obj ) ) return false ;
129+ if ( ReferenceEquals ( this , obj ) ) return true ;
130+ if ( obj . GetType ( ) != typeof ( ConvertibleTypeKey ) ) return false ;
131+ return Equals ( ( ConvertibleTypeKey ) obj ) ;
132+ }
133+
134+ public override int GetHashCode ( )
135+ {
136+ unchecked
137+ {
138+ return ( ( ToInstanceType != null ? ToInstanceType . GetHashCode ( ) : 0 ) * 397 )
139+ ^ ( FromElemenetType != null ? FromElemenetType . GetHashCode ( ) : 0 ) ;
140+ }
141+ }
142+ }
143+
144+ public class TranslateListWithElements < T >
145+ {
146+ public static object CreateInstance ( Type toInstanceOfType )
147+ {
148148 if ( toInstanceOfType . IsGeneric ( ) )
149149 {
150- if ( toInstanceOfType . HasAnyTypeDefinitionsOf (
151- typeof ( ICollection < > ) , typeof ( IList < > ) ) )
152- {
153- return typeof ( List < T > ) . CreateInstance ( ) ;
154- }
155- }
156-
157- return toInstanceOfType . CreateInstance ( ) ;
158- }
159-
160- public static IList TranslateToIList ( IList fromList , Type toInstanceOfType )
161- {
162- var to = ( IList ) toInstanceOfType . CreateInstance ( ) ;
163- foreach ( var item in fromList )
164- {
165- to . Add ( item ) ;
166- }
167- return to ;
168- }
169-
170- public static object LateBoundTranslateToGenericICollection (
171- object fromList , Type toInstanceOfType )
172- {
173- if ( fromList == null )
150+ if ( toInstanceOfType . HasAnyTypeDefinitionsOf (
151+ typeof ( ICollection < > ) , typeof ( IList < > ) ) )
152+ {
153+ return typeof ( List < T > ) . CreateInstance ( ) ;
154+ }
155+ }
156+
157+ return toInstanceOfType . CreateInstance ( ) ;
158+ }
159+
160+ public static IList TranslateToIList ( IList fromList , Type toInstanceOfType )
161+ {
162+ var to = ( IList ) toInstanceOfType . CreateInstance ( ) ;
163+ foreach ( var item in fromList )
164+ {
165+ to . Add ( item ) ;
166+ }
167+ return to ;
168+ }
169+
170+ public static object LateBoundTranslateToGenericICollection (
171+ object fromList , Type toInstanceOfType )
172+ {
173+ if ( fromList == null )
174174 return null ; //AOT
175175
176176 if ( toInstanceOfType . IsArray )
@@ -180,64 +180,64 @@ public static object LateBoundTranslateToGenericICollection(
180180 return result . ToArray ( ) ;
181181 }
182182
183- return TranslateToGenericICollection (
184- ( ICollection < T > ) fromList , toInstanceOfType ) ;
185- }
186-
187- public static ICollection < T > TranslateToGenericICollection (
188- ICollection < T > fromList , Type toInstanceOfType )
189- {
190- var to = ( ICollection < T > ) CreateInstance ( toInstanceOfType ) ;
191- foreach ( var item in fromList )
192- {
193- to . Add ( item ) ;
194- }
195- return to ;
196- }
197- }
198-
199- public class TranslateListWithConvertibleElements < TFrom , TTo >
200- {
201- private static readonly Func < TFrom , TTo > ConvertFn ;
202-
203- static TranslateListWithConvertibleElements ( )
204- {
205- ConvertFn = GetConvertFn ( ) ;
206- }
207-
208- public static object LateBoundTranslateToGenericICollection (
209- object fromList , Type toInstanceOfType )
210- {
211- return TranslateToGenericICollection (
212- ( ICollection < TFrom > ) fromList , toInstanceOfType ) ;
213- }
214-
215- public static ICollection < TTo > TranslateToGenericICollection (
216- ICollection < TFrom > fromList , Type toInstanceOfType )
217- {
218- if ( fromList == null ) return null ; //AOT
219-
220- var to = ( ICollection < TTo > ) TranslateListWithElements < TTo > . CreateInstance ( toInstanceOfType ) ;
221-
222- foreach ( var item in fromList )
223- {
224- var toItem = ConvertFn ( item ) ;
225- to . Add ( toItem ) ;
226- }
227- return to ;
228- }
229-
230- private static Func < TFrom , TTo > GetConvertFn ( )
231- {
232- if ( typeof ( TTo ) == typeof ( string ) )
233- {
234- return x => ( TTo ) ( object ) TypeSerializer . SerializeToString ( x ) ;
235- }
236- if ( typeof ( TFrom ) == typeof ( string ) )
237- {
238- return x => TypeSerializer . DeserializeFromString < TTo > ( ( string ) ( object ) x ) ;
239- }
240- return x => TypeSerializer . DeserializeFromString < TTo > ( TypeSerializer . SerializeToString ( x ) ) ;
241- }
242- }
183+ return TranslateToGenericICollection (
184+ ( ICollection < T > ) fromList , toInstanceOfType ) ;
185+ }
186+
187+ public static ICollection < T > TranslateToGenericICollection (
188+ ICollection < T > fromList , Type toInstanceOfType )
189+ {
190+ var to = ( ICollection < T > ) CreateInstance ( toInstanceOfType ) ;
191+ foreach ( var item in fromList )
192+ {
193+ to . Add ( item ) ;
194+ }
195+ return to ;
196+ }
197+ }
198+
199+ public class TranslateListWithConvertibleElements < TFrom , TTo >
200+ {
201+ private static readonly Func < TFrom , TTo > ConvertFn ;
202+
203+ static TranslateListWithConvertibleElements ( )
204+ {
205+ ConvertFn = GetConvertFn ( ) ;
206+ }
207+
208+ public static object LateBoundTranslateToGenericICollection (
209+ object fromList , Type toInstanceOfType )
210+ {
211+ return TranslateToGenericICollection (
212+ ( ICollection < TFrom > ) fromList , toInstanceOfType ) ;
213+ }
214+
215+ public static ICollection < TTo > TranslateToGenericICollection (
216+ ICollection < TFrom > fromList , Type toInstanceOfType )
217+ {
218+ if ( fromList == null ) return null ; //AOT
219+
220+ var to = ( ICollection < TTo > ) TranslateListWithElements < TTo > . CreateInstance ( toInstanceOfType ) ;
221+
222+ foreach ( var item in fromList )
223+ {
224+ var toItem = ConvertFn ( item ) ;
225+ to . Add ( toItem ) ;
226+ }
227+ return to ;
228+ }
229+
230+ private static Func < TFrom , TTo > GetConvertFn ( )
231+ {
232+ if ( typeof ( TTo ) == typeof ( string ) )
233+ {
234+ return x => ( TTo ) ( object ) TypeSerializer . SerializeToString ( x ) ;
235+ }
236+ if ( typeof ( TFrom ) == typeof ( string ) )
237+ {
238+ return x => TypeSerializer . DeserializeFromString < TTo > ( ( string ) ( object ) x ) ;
239+ }
240+ return x => TypeSerializer . DeserializeFromString < TTo > ( TypeSerializer . SerializeToString ( x ) ) ;
241+ }
242+ }
243243}
0 commit comments