forked from msgpack/msgpack-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionExtensions.cs
More file actions
408 lines (369 loc) · 13.9 KB
/
ReflectionExtensions.cs
File metadata and controls
408 lines (369 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#region -- License Terms --
//
// MessagePack for CLI
//
// Copyright (C) 2010-2013 FUJIWARA, Yusuke
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion -- License Terms --
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Reflection;
using MsgPack.Serialization.Reflection;
namespace MsgPack.Serialization
{
internal static class ReflectionExtensions
{
// TODO: Metadata
private static readonly PropertyInfo _icollectionCount = FromExpression.ToProperty( ( ICollection value ) => value.Count );
public static Type GetMemberValueType( this MemberInfo source )
{
Contract.Requires( source != null );
PropertyInfo asProperty = source as PropertyInfo;
FieldInfo asField = source as FieldInfo;
return asProperty != null ? asProperty.PropertyType : asField.FieldType;
}
public static bool CanSetValue( this MemberInfo source )
{
PropertyInfo asProperty = source as PropertyInfo;
FieldInfo asField = source as FieldInfo;
// GetSetMethod() on WinRT is not compabitle with CLR. CLR returns null but WinRT returns non-null for non-public setter.
return asProperty != null ? ( asProperty.CanWrite && asProperty.GetSetMethod() != null && asProperty.GetSetMethod().IsPublic ) : !asField.IsInitOnly;
}
public static CollectionTraits GetCollectionTraits( this Type source )
{
Contract.Assert( !source.GetContainsGenericParameters() );
/*
* SPEC
* If the object has single public method TEnumerator GetEnumerator() ( where TEnumerator implements IEnumerator<TItem>),
* then the object is considered as the collection of TItem.
* When the object is considered as the collection of TItem, TItem is KeyValuePair<TKey,TValue>,
* and the object implements IDictionary<TKey,TValue>, then the object is considered as dictionary of TKey and TValue.
* Else, if the object has single public method IEnumerator GetEnumerator(), then the object is considered as the collection of Object.
* When it also implements IDictionary, however, it is considered dictionary of Object and Object.
* Otherwise, that means it implements multiple collection interface, is following.
* First, if the object implements IDictionary<MessagePackObject,MessagePackObject>, then it is considered as MPO dictionary.
* Second, if the object implements IEnumerable<MPO>, then it is considered as MPO dictionary.
* Third, if the object implement SINGLE IDictionary<TKey,TValue> and multiple IEnumerable<T>, then it is considered as dictionary of TKey and TValue.
* Fourth, the object is considered as UNSERIALIZABLE member. This behavior similer to DataContract serialization behavor
* (see http://msdn.microsoft.com/en-us/library/aa347850.aspx ).
*/
if ( !source.IsAssignableTo( typeof( IEnumerable ) ) )
{
return CollectionTraits.NotCollection;
}
if ( source.IsArray )
{
return
new CollectionTraits(
CollectionKind.Array,
null, // Add() : Never used for array.
null, // GetEnumerator() : Never used for array.
null, // Count : Never used for array.
source.GetElementType()
);
}
var getEnumerator = source.GetMethod( "GetEnumerator", ReflectionAbstractions.EmptyTypes );
if ( getEnumerator != null && getEnumerator.ReturnType.IsAssignableTo( typeof( IEnumerator ) ) )
{
// If public 'GetEnumerator' is found, it is primary collection traits.
if ( source.Implements( typeof( IDictionary<,> ) ) )
{
var ienumetaorT = getEnumerator.ReturnType.GetInterfaces().FirstOrDefault( @interface => @interface.GetIsGenericType() && @interface.GetGenericTypeDefinition() == typeof( IEnumerator<> ) );
if ( ienumetaorT != null )
{
var elementType = ienumetaorT.GetGenericArguments()[ 0 ];
return
new CollectionTraits(
CollectionKind.Map,
GetAddMethod( source, elementType.GetGenericArguments()[ 0 ], elementType.GetGenericArguments()[ 1 ] ),
getEnumerator,
GetDictionaryCountProperty( source, elementType.GetGenericArguments()[ 0 ], elementType.GetGenericArguments()[ 1 ] ),
elementType
);
}
}
if ( source.IsAssignableTo( typeof( IDictionary ) ) )
{
return
new CollectionTraits(
CollectionKind.Map,
GetAddMethod( source, typeof( object ), typeof( object ) ),
getEnumerator,
_icollectionCount,
typeof( DictionaryEntry )
);
}
// Block to limit variable scope
{
var ienumetaorT =
IsIEnumeratorT(getEnumerator.ReturnType)
? getEnumerator.ReturnType
: getEnumerator.ReturnType.GetInterfaces().FirstOrDefault( IsIEnumeratorT );
if ( ienumetaorT != null )
{
var elementType = ienumetaorT.GetGenericArguments()[ 0 ];
return
new CollectionTraits(
CollectionKind.Array,
GetAddMethod( source, elementType ),
getEnumerator,
GetCollectionTCountProperty( source, elementType ),
elementType
);
}
}
}
Type ienumerableT = null;
Type idictionaryT = null;
Type ienumerable = null;
Type idictionary = null;
var sourceInterfaces = source.FindInterfaces( FilterCollectionType, null );
if ( source.GetIsInterface() && FilterCollectionType( source, null ) )
{
var originalSourceInterfaces = sourceInterfaces.ToArray();
var concatenatedSourceInterface = new Type[ originalSourceInterfaces.Length + 1 ];
concatenatedSourceInterface[ 0 ] = source;
for ( int i = 0; i < originalSourceInterfaces.Length; i++ )
{
concatenatedSourceInterface[ i + 1 ] = originalSourceInterfaces[ i ];
}
sourceInterfaces = concatenatedSourceInterface;
}
foreach ( var type in sourceInterfaces )
{
if ( type == typeof( IDictionary<MessagePackObject, MessagePackObject> ) )
{
return
new CollectionTraits(
CollectionKind.Map,
GetAddMethod( source, typeof( MessagePackObject ), typeof( MessagePackObject ) ),
FindInterfaceMethod( source, typeof( IEnumerable<KeyValuePair<MessagePackObject, MessagePackObject>> ), "GetEnumerator", ReflectionAbstractions.EmptyTypes ),
GetDictionaryCountProperty( source, typeof( MessagePackObject ), typeof( MessagePackObject ) ),
typeof( KeyValuePair<MessagePackObject, MessagePackObject> )
);
}
if ( type == typeof( IEnumerable<MessagePackObject> ) )
{
var addMethod = GetAddMethod( source, typeof( MessagePackObject ) );
if ( addMethod != null )
{
return
new CollectionTraits(
CollectionKind.Array,
addMethod,
FindInterfaceMethod( source, typeof( IEnumerable<MessagePackObject> ), "GetEnumerator", ReflectionAbstractions.EmptyTypes ),
GetCollectionTCountProperty( source, typeof( MessagePackObject ) ),
typeof( MessagePackObject )
);
}
}
if ( type.GetIsGenericType() )
{
var genericTypeDefinition = type.GetGenericTypeDefinition();
if ( genericTypeDefinition == typeof( IDictionary<,> ) )
{
if ( idictionaryT != null )
{
return CollectionTraits.Unserializable;
}
idictionaryT = type;
}
else if ( genericTypeDefinition == typeof( IEnumerable<> ) )
{
if ( ienumerableT != null )
{
return CollectionTraits.Unserializable;
}
ienumerableT = type;
}
}
else
{
if ( type == typeof( IDictionary ) )
{
idictionary = type;
}
else if ( type == typeof( IEnumerable ) )
{
ienumerable = type;
}
}
}
if ( idictionaryT != null )
{
var elementType = typeof( KeyValuePair<,> ).MakeGenericType( idictionaryT.GetGenericArguments() );
return
new CollectionTraits(
CollectionKind.Map,
GetAddMethod( source, idictionaryT.GetGenericArguments()[ 0 ], idictionaryT.GetGenericArguments()[ 1 ] ),
FindInterfaceMethod( source, typeof( IEnumerable<> ).MakeGenericType( elementType ), "GetEnumerator", ReflectionAbstractions.EmptyTypes ),
GetDictionaryCountProperty( source, idictionaryT.GetGenericArguments()[ 0 ], idictionaryT.GetGenericArguments()[ 1 ] ),
elementType
);
}
if ( ienumerableT != null )
{
var elementType = ienumerableT.GetGenericArguments()[ 0 ];
return
new CollectionTraits(
CollectionKind.Array,
GetAddMethod( source, elementType ),
FindInterfaceMethod( source, ienumerableT, "GetEnumerator", ReflectionAbstractions.EmptyTypes ),
GetCollectionTCountProperty( source, elementType ),
elementType
);
}
if ( idictionary != null )
{
return
new CollectionTraits(
CollectionKind.Map,
GetAddMethod( source, typeof( object ), typeof( object ) ),
FindInterfaceMethod( source, idictionary, "GetEnumerator", ReflectionAbstractions.EmptyTypes ),
_icollectionCount,
typeof( object )
);
}
if ( ienumerable != null )
{
var addMethod = GetAddMethod( source, typeof( object ) );
if ( addMethod != null )
{
return
new CollectionTraits(
CollectionKind.Array,
addMethod,
FindInterfaceMethod( source, ienumerable, "GetEnumerator", ReflectionAbstractions.EmptyTypes ),
_icollectionCount,
typeof( object )
);
}
}
return CollectionTraits.NotCollection;
}
private static MethodInfo FindInterfaceMethod( Type targetType, Type interfaceType, string name, Type[] parameterTypes )
{
if ( targetType.GetIsInterface() )
{
return targetType.FindInterfaces( ( type, _ ) => type == interfaceType, null ).Single().GetMethod( name, parameterTypes );
}
var map = targetType.GetInterfaceMap( interfaceType );
#if !SILVERLIGHT
int index = Array.FindIndex( map.InterfaceMethods, method => method.Name == name && method.GetParameters().Select( p => p.ParameterType ).SequenceEqual( parameterTypes ) );
#else
int index = map.InterfaceMethods.FindIndex( method => method.Name == name && method.GetParameters().Select( p => p.ParameterType ).SequenceEqual( parameterTypes ) );
#endif
if ( index < 0 )
{
#if DEBUG
#if !NETFX_35
Contract.Assert( false, interfaceType + "::" + name + "(" + String.Join<Type>( ", ", parameterTypes ) + ") is not found in " + targetType );
#else
Contract.Assert( false, interfaceType + "::" + name + "(" + String.Join( ", ", parameterTypes.Select( t => t.ToString() ).ToArray() ) + ") is not found in " + targetType );
#endif
#endif
return null;
}
else
{
return map.TargetMethods[ index ];
}
}
private static PropertyInfo GetCollectionTCountProperty( Type targetType, Type elementType )
{
if ( !targetType.GetIsValueType() && targetType.Implements( typeof( ICollection<> ) ) )
{
return typeof( ICollection<> ).MakeGenericType( elementType ).GetProperty( "Count" );
}
var property = targetType.GetProperty( "Count" );
if ( property != null && property.PropertyType == typeof( int ) && property.GetIndexParameters().Length == 0 )
{
return property;
}
return null;
}
private static PropertyInfo GetDictionaryCountProperty( Type targetType, Type keyType, Type valueType )
{
var property = targetType.GetProperty( "Count" );
if ( property != null && property.PropertyType == typeof( int ) && property.GetIndexParameters().Length == 0 )
{
return property;
}
return
typeof( ICollection<> ).MakeGenericType(
typeof( KeyValuePair<,> ).MakeGenericType( keyType, valueType )
).GetProperty( "Count" );
}
private static MethodInfo GetAddMethod( Type targetType, Type argumentType )
{
var argumentTypes = new[] { argumentType };
var result = targetType.GetMethod( "Add", argumentTypes );
if ( result != null )
{
return result;
}
var icollectionT = typeof( ICollection<> ).MakeGenericType( argumentType );
if ( targetType.IsAssignableTo( icollectionT ) )
{
return icollectionT.GetMethod( "Add", argumentTypes );
}
if ( targetType.IsAssignableTo( typeof( IList ) ) )
{
return typeof( IList ).GetMethod( "Add", new[] { typeof( object ) } );
}
return null;
}
private static MethodInfo GetAddMethod( Type targetType, Type keyType, Type valueType )
{
var argumentTypes = new[] { keyType, valueType };
var result = targetType.GetMethod( "Add", argumentTypes );
if ( result != null )
{
return result;
}
return typeof( IDictionary<,> ).MakeGenericType( argumentTypes ).GetMethod( "Add", argumentTypes );
}
private static bool FilterCollectionType( Type type, object filterCriteria )
{
#if !NETFX_CORE
Contract.Assert( type.IsInterface );
return type.Assembly == typeof( Array ).Assembly && ( type.Namespace == "System.Collections" || type.Namespace == "System.Collections.Generic" );
#else
var typeInfo = type.GetTypeInfo();
Contract.Assert( typeInfo.IsInterface );
return typeInfo.Assembly.Equals( typeof( Array ).GetTypeInfo().Assembly ) && ( type.Namespace == "System.Collections" || type.Namespace == "System.Collections.Generic" );
#endif
}
private static bool IsIEnumeratorT( Type @interface )
{
return @interface.GetIsGenericType() && @interface.GetGenericTypeDefinition() == typeof( IEnumerator<> );
}
#if WINDOWS_PHONE
public static IEnumerable<Type> FindInterfaces( this Type source, Func<Type, object, bool> filter, object criterion )
{
foreach ( var @interface in source.GetInterfaces() )
{
if ( filter( @interface, criterion ) )
{
yield return @interface;
}
}
}
#endif
}
}