20
20
21
21
namespace ServiceStack . Text
22
22
{
23
- public static class TranslateListWithElements
24
- {
23
+ public static class TranslateListWithElements
24
+ {
25
25
private static Dictionary < Type , ConvertInstanceDelegate > TranslateICollectionCache
26
26
= new Dictionary < Type , ConvertInstanceDelegate > ( ) ;
27
27
28
- public static object TranslateToGenericICollectionCache ( object from , Type toInstanceOfType , Type elementType )
29
- {
28
+ public static object TranslateToGenericICollectionCache ( object from , Type toInstanceOfType , Type elementType )
29
+ {
30
30
ConvertInstanceDelegate translateToFn ;
31
31
if ( TranslateICollectionCache . TryGetValue ( toInstanceOfType , out translateToFn ) )
32
32
return translateToFn ( from , toInstanceOfType ) ;
@@ -45,16 +45,16 @@ public static object TranslateToGenericICollectionCache(object from, Type toInst
45
45
} while ( ! ReferenceEquals (
46
46
Interlocked . CompareExchange ( ref TranslateICollectionCache , newCache , snapshot ) , snapshot ) ) ;
47
47
48
- return translateToFn ( from , toInstanceOfType ) ;
49
- }
48
+ return translateToFn ( from , toInstanceOfType ) ;
49
+ }
50
50
51
51
private static Dictionary < ConvertibleTypeKey , ConvertInstanceDelegate > TranslateConvertibleICollectionCache
52
52
= new Dictionary < ConvertibleTypeKey , ConvertInstanceDelegate > ( ) ;
53
53
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 ) ;
58
58
ConvertInstanceDelegate translateToFn ;
59
59
if ( TranslateConvertibleICollectionCache . TryGetValue ( typeKey , out translateToFn ) ) return translateToFn ( from , toInstanceOfType ) ;
60
60
@@ -72,105 +72,105 @@ public static object TranslateToConvertibleGenericICollectionCache(
72
72
73
73
} while ( ! ReferenceEquals (
74
74
Interlocked . CompareExchange ( ref TranslateConvertibleICollectionCache , newCache , snapshot ) , snapshot ) ) ;
75
-
75
+
76
76
return translateToFn ( from , toInstanceOfType ) ;
77
- }
77
+ }
78
78
79
79
public static object TryTranslateCollections ( Type fromPropertyType , Type toPropertyType , object fromValue )
80
- {
80
+ {
81
81
var args = typeof ( IEnumerable < > ) . GetGenericArgumentsIfBothHaveSameGenericDefinitionTypeAndArguments (
82
- fromPropertyType , toPropertyType ) ;
82
+ fromPropertyType , toPropertyType ) ;
83
83
84
84
if ( args != null )
85
- {
86
- return TranslateToGenericICollectionCache (
87
- fromValue , toPropertyType , args [ 0 ] ) ;
88
- }
85
+ {
86
+ return TranslateToGenericICollectionCache (
87
+ fromValue , toPropertyType , args [ 0 ] ) ;
88
+ }
89
89
90
90
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
+ {
148
148
if ( toInstanceOfType . IsGeneric ( ) )
149
149
{
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 )
174
174
return null ; //AOT
175
175
176
176
if ( toInstanceOfType . IsArray )
@@ -180,64 +180,64 @@ public static object LateBoundTranslateToGenericICollection(
180
180
return result . ToArray ( ) ;
181
181
}
182
182
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
+ }
243
243
}
0 commit comments