forked from msgpack/msgpack-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionAbstractions.cs
More file actions
573 lines (499 loc) · 16.4 KB
/
ReflectionAbstractions.cs
File metadata and controls
573 lines (499 loc) · 16.4 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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#region -- License Terms --
//
// MessagePack for CLI
//
// Copyright (C) 2010-2018 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 --
#if UNITY_5 || UNITY_STANDALONE || UNITY_WEBPLAYER || UNITY_WII || UNITY_IPHONE || UNITY_ANDROID || UNITY_PS3 || UNITY_XBOX360 || UNITY_FLASH || UNITY_BKACKBERRY || UNITY_WINRT
#define UNITY
#endif
using System;
using System.Collections.Generic;
using System.Diagnostics;
#if FEATURE_MPCONTRACT
using Contract = MsgPack.MPContract;
#else
using System.Diagnostics.Contracts;
#endif // FEATURE_MPCONTRACT
using System.Linq;
using System.Reflection;
namespace MsgPack
{
#if UNITY && DEBUG
public
#else
internal
#endif
static class ReflectionAbstractions
{
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Performance", "CA1802:UseLiteralsWhereAppropriate", Justification = "Same as FCL" )]
public static readonly char TypeDelimiter = '.';
public static readonly Type[] EmptyTypes = new Type[ 0 ];
public static bool GetIsValueType( this Type source )
{
#if NETSTANDARD1_1 || NETSTANDARD1_3
return source.GetTypeInfo().IsValueType;
#else
return source.IsValueType;
#endif // NETSTANDARD1_1 || NETSTANDARD1_3
}
public static bool GetIsEnum( this Type source )
{
#if NETSTANDARD1_1 || NETSTANDARD1_3
return source.GetTypeInfo().IsEnum;
#else
return source.IsEnum;
#endif // NETSTANDARD1_1 || NETSTANDARD1_3
}
public static bool GetIsInterface( this Type source )
{
#if NETSTANDARD1_1 || NETSTANDARD1_3
return source.GetTypeInfo().IsInterface;
#else
return source.IsInterface;
#endif // NETSTANDARD1_1 || NETSTANDARD1_3
}
public static bool GetIsAbstract( this Type source )
{
#if NETSTANDARD1_1 || NETSTANDARD1_3
return source.GetTypeInfo().IsAbstract;
#else
return source.IsAbstract;
#endif // NETSTANDARD1_1 || NETSTANDARD1_3
}
public static bool GetIsGenericType( this Type source )
{
#if NETSTANDARD1_1 || NETSTANDARD1_3
return source.GetTypeInfo().IsGenericType;
#else
return source.IsGenericType;
#endif // NETSTANDARD1_1 || NETSTANDARD1_3
}
public static bool GetIsGenericTypeDefinition( this Type source )
{
#if NETSTANDARD1_1 || NETSTANDARD1_3
return source.GetTypeInfo().IsGenericTypeDefinition;
#else
return source.IsGenericTypeDefinition;
#endif // NETSTANDARD1_1 || NETSTANDARD1_3
}
#if DEBUG
public static bool GetContainsGenericParameters( this Type source )
{
#if NETSTANDARD1_1 || NETSTANDARD1_3
return source.GetTypeInfo().ContainsGenericParameters;
#else
return source.ContainsGenericParameters;
#endif // NETSTANDARD1_1 || NETSTANDARD1_3
}
#endif // DEBUG
public static Assembly GetAssembly( this Type source )
{
#if NETSTANDARD1_1 || NETSTANDARD1_3
return source.GetTypeInfo().Assembly;
#else
return source.Assembly;
#endif // NETSTANDARD1_1 || NETSTANDARD1_3
}
public static bool GetIsVisible( this Type source )
{
#if NETSTANDARD1_1 || NETSTANDARD1_3
return source.GetTypeInfo().IsVisible;
#else
return source.IsVisible;
#endif // NETSTANDARD1_1 || NETSTANDARD1_3
}
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Wrong detection" )]
public static bool GetIsPublic( this Type source )
{
#if NETSTANDARD1_1 || NETSTANDARD1_3
return source.GetTypeInfo().IsPublic;
#else
return source.IsPublic;
#endif // NETSTANDARD1_1 || NETSTANDARD1_3
}
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Wrong detection" )]
public static bool GetIsNestedPublic( this Type source )
{
#if NETSTANDARD1_1 || NETSTANDARD1_3
return source.GetTypeInfo().IsNestedPublic;
#else
return source.IsNestedPublic;
#endif // NETSTANDARD1_1 || NETSTANDARD1_3
}
#if DEBUG
public static bool GetIsPrimitive( this Type source )
{
#if NETSTANDARD1_1 || NETSTANDARD1_3
return source.GetTypeInfo().IsPrimitive;
#else
return source.IsPrimitive;
#endif // NETSTANDARD1_1 || NETSTANDARD1_3
}
#endif // DEBUG
public static Type GetBaseType( this Type source )
{
#if NETSTANDARD1_1 || NETSTANDARD1_3
return source.GetTypeInfo().BaseType;
#else
return source.BaseType;
#endif // NETSTANDARD1_1 || NETSTANDARD1_3
}
public static MethodBase GetDeclaringMethod( this Type source )
{
#if NETSTANDARD1_1 || NETSTANDARD1_3
return source.GetTypeInfo().DeclaringMethod;
#else
return source.DeclaringMethod;
#endif // NETSTANDARD1_1 || NETSTANDARD1_3
}
public static Type[] GetGenericTypeParameters( this Type source )
{
#if NETSTANDARD1_1 || NETSTANDARD1_3
return source.GetTypeInfo().GenericTypeParameters;
#else
return source.GetGenericArguments().Where( t => t.IsGenericParameter ).ToArray();
#endif // NETSTANDARD1_1 || NETSTANDARD1_3
}
public static MethodInfo GetRuntimeMethod( this Type source, string name )
{
var candidates = source.GetRuntimeMethods().Where( m => m.Name == name ).ToArray();
switch ( candidates.Length )
{
case 0:
{
return null;
}
case 1:
{
return candidates[ 0 ];
}
default:
{
throw new AmbiguousMatchException();
}
}
}
#if NETSTANDARD1_1 || NETSTANDARD1_3
public static MethodInfo GetRuntimeMethod( this Type source, string name, Type[] parameters )
{
return
source.GetRuntimeMethods()
.SingleOrDefault(
m => m.IsPublic && m.Name == name && m.GetParameters().Select( p => p.ParameterType ).SequenceEqual( parameters )
);
}
#else // NETSTANDARD1_1 || NETSTANDARD1_3
public static MethodInfo GetRuntimeMethod( this Type source, string name, Type[] parameters )
{
return
source.GetMethod(
name,
BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public,
null,
parameters,
null
);
}
public static IEnumerable<MethodInfo> GetRuntimeMethods( this Type source )
{
return
source.GetMethods(
BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic
);
}
public static PropertyInfo GetRuntimeProperty( this Type source, string name )
{
return
source.GetProperty(
name,
BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic
);
}
#if NET35 || SILVERLIGHT
public static IEnumerable<PropertyInfo> GetRuntimeProperties( this Type source )
{
return source.GetProperties( BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic );
}
#endif // NET35 || SILVERLIGHT
#if DEBUG
public static FieldInfo GetRuntimeField( this Type source, string name )
{
return
source.GetField(
name,
BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic
);
}
#endif // DEBUG
#endif // NETSTANDARD1_1 || NETSTANDARD1_3
public static ConstructorInfo GetRuntimeConstructor( this Type source, Type[] parameters )
{
#if NETSTANDARD1_1 || NETSTANDARD1_3
return source.GetTypeInfo().DeclaredConstructors.SingleOrDefault( c => c.GetParameters().Select( p => p.ParameterType ).SequenceEqual( parameters ) );
#else
return source.GetConstructor( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameters, null );
#endif // NETSTANDARD1_1 || NETSTANDARD1_3
}
#if NET35 || NET40 || SILVERLIGHT || UNITY
public static Delegate CreateDelegate( this MethodInfo source, Type delegateType )
{
return Delegate.CreateDelegate( delegateType, source );
}
public static Delegate CreateDelegate( this MethodInfo source, Type delegateType, object target )
{
return Delegate.CreateDelegate( delegateType, target, source );
}
#endif // NET35 || NET40 || SILVERLIGHT || UNITY
#if NETSTANDARD1_1 || NETSTANDARD1_3
public static MethodInfo GetMethod( this Type source, string name )
{
return source.GetRuntimeMethods().SingleOrDefault( m => m.IsPublic && m.Name == name && m.DeclaringType == source );
}
public static MethodInfo GetMethod( this Type source, string name, Type[] parameters )
{
return source.GetRuntimeMethod( name, parameters );
}
public static IEnumerable<MethodInfo> GetMethods( this Type source )
{
return source.GetRuntimeMethods();
}
public static PropertyInfo GetProperty( this Type source, string name )
{
return source.GetRuntimeProperty( name );
}
public static PropertyInfo GetProperty( this Type source, string name, Type[] keyTypes )
{
return
source.GetRuntimeProperties()
.SingleOrDefault(
p => p.Name == name && p.GetMethod.GetParameters().Select( r => r.ParameterType ).SequenceEqual( keyTypes )
);
}
public static IEnumerable<PropertyInfo> GetProperties( this Type source )
{
return source.GetRuntimeProperties();
}
public static FieldInfo GetField( this Type source, string name )
{
return source.GetRuntimeField( name );
}
public static ConstructorInfo GetConstructor( this Type source, Type[] parameteres )
{
return source.GetTypeInfo().GetConstructor( parameteres );
}
public static ConstructorInfo GetConstructor( this TypeInfo source, Type[] parameteres )
{
return source.DeclaredConstructors.SingleOrDefault( c => !c.IsStatic && c.GetParameters().Select( p => p.ParameterType ).SequenceEqual( parameteres ) );
}
public static IEnumerable<ConstructorInfo> GetConstructors( this Type source )
{
return source.GetTypeInfo().DeclaredConstructors.Where( c => c.IsPublic );
}
public static Type[] GetGenericArguments( this Type source )
{
return source.GenericTypeArguments;
}
public static bool IsAssignableFrom( this Type source, Type target )
{
return source.GetTypeInfo().IsAssignableFrom( target.GetTypeInfo() );
}
public static IEnumerable<Type> GetInterfaces( this Type source )
{
return source.GetTypeInfo().ImplementedInterfaces;
}
public static MethodInfo GetGetMethod( this PropertyInfo source )
{
return GetGetMethod( source, false );
}
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "containsNonPublic", Justification = "For API compabitility" )]
public static MethodInfo GetGetMethod( this PropertyInfo source, bool containsNonPublic )
{
var getter = source.GetMethod;
return ( containsNonPublic || getter.IsPublic ) ? getter : null;
}
public static MethodInfo GetSetMethod( this PropertyInfo source )
{
return GetSetMethod( source, false );
}
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "containsNonPublic", Justification = "For API compabitility" )]
public static MethodInfo GetSetMethod( this PropertyInfo source, bool containsNonPublic )
{
var setter = source.SetMethod;
return ( containsNonPublic || setter.IsPublic ) ? setter : null;
}
public static IEnumerable<Type> FindInterfaces( this Type source, Func<Type, object, bool> filter, object filterCriteria )
{
return source.GetTypeInfo().ImplementedInterfaces.Where( t => filter( t, filterCriteria ) );
}
public static InterfaceMapping GetInterfaceMap( this Type source, Type interfaceType )
{
return source.GetTypeInfo().GetRuntimeInterfaceMap( interfaceType );
}
public static IEnumerable<CustomAttributeData> GetCustomAttributesData( this Type source )
{
return source.GetTypeInfo().CustomAttributes;
}
public static IEnumerable<CustomAttributeData> GetCustomAttributesData( this MemberInfo source )
{
return source.CustomAttributes;
}
public static Type GetAttributeType( this CustomAttributeData source )
{
return source.AttributeType;
}
public static string GetMemberName( this CustomAttributeNamedArgument source )
{
// This is hack to check null because .NET Standard 1.1 does not expose CustomAttributeNamedArgument.MemberInfo
// but it still throws NullReferenceException when its private MemberInfo type field is null.
// This is caused by default instance of CustomAttributeNamedArgument, so it also should have default CustomAttributeTypedArgument
// which has null ArgumentType.
if ( source.TypedValue.ArgumentType == null )
{
return null;
}
return source.MemberName;
}
#else
public static T GetCustomAttribute<T>( this MemberInfo source )
where T : Attribute
{
return Attribute.GetCustomAttribute( source, typeof( T ) ) as T;
}
#if NET35 || NET40 || SILVERLIGHT || UNITY
public static bool IsDefined( this MemberInfo source, Type attributeType )
{
return Attribute.IsDefined( source, attributeType );
}
#endif // NET35 || NET40 || SILVERLIGHT || UNITY
#if !SILVERLIGHT
public static Type GetAttributeType( this CustomAttributeData source )
{
return source.Constructor.DeclaringType;
}
public static string GetMemberName( this CustomAttributeNamedArgument source )
{
if ( source.MemberInfo == null )
{
return null;
}
return source.MemberInfo.Name;
}
#else
public static Type GetAttributeType( this Attribute source )
{
return source.GetType();
}
#endif // !SILVERLIGHT
#endif // NETSTANDARD1_1 || NETSTANDARD1_3
public static string GetCultureName( this AssemblyName source )
{
#if NET35 || NET40 || SILVERLIGHT || UNITY
return source.CultureInfo == null ? null : source.CultureInfo.Name;
#else
return source.CultureName;
#endif
}
#if NET35 || UNITY
public static IEnumerable<CustomAttributeData> GetCustomAttributesData( this MemberInfo source )
{
return CustomAttributeData.GetCustomAttributes( source );
}
public static IEnumerable<CustomAttributeData> GetCustomAttributesData( this ParameterInfo source )
{
return CustomAttributeData.GetCustomAttributes( source );
}
#endif // NET35 || UNITY
#if NETSTANDARD1_1 || NETSTANDARD1_3
public static IEnumerable<CustomAttributeData> GetCustomAttributesData( this ParameterInfo source )
{
return source.CustomAttributes;
}
#endif // NETSTANDARD1_1 || NETSTANDARD1_3
#if SILVERLIGHT
public static IEnumerable<Attribute> GetCustomAttributesData( this MemberInfo source )
{
return source.GetCustomAttributes( false ).OfType<Attribute>();
}
public static IEnumerable<NamedArgument> GetNamedArguments( this Attribute attribute )
{
return
attribute.GetType()
.GetMembers( BindingFlags.Public | BindingFlags.Instance )
.Where( m => m.MemberType == MemberTypes.Field || m.MemberType == MemberTypes.Property )
.Select( m => new NamedArgument( attribute, m ) );
}
#else
public static IList<CustomAttributeTypedArgument> GetConstructorArguments( this CustomAttributeData source )
{
return source.ConstructorArguments;
}
public static IEnumerable<CustomAttributeNamedArgument> GetNamedArguments( this CustomAttributeData source )
{
return source.NamedArguments;
}
public static CustomAttributeTypedArgument GetTypedValue( this CustomAttributeNamedArgument source )
{
return source.TypedValue;
}
#endif // SILVERLIGHT
#if SILVERLIGHT
public struct NamedArgument
{
private object _instance;
private MemberInfo _memberInfo;
public NamedArgument( object instance, MemberInfo memberInfo )
{
this._instance = instance;
this._memberInfo = memberInfo;
}
public string GetMemberName()
{
return this._memberInfo.Name;
}
public KeyValuePair<Type, object> GetTypedValue()
{
Type type;
object value;
PropertyInfo asProperty;
if ( ( asProperty = this._memberInfo as PropertyInfo ) != null )
{
type = asProperty.PropertyType;
value = asProperty.GetValue( this._instance, null );
}
else
{
var asField = this._memberInfo as FieldInfo;
#if DEBUG
Contract.Assert( asField != null );
#endif
type = asField.FieldType;
value = asField.GetValue( this._instance );
}
return new KeyValuePair<Type, object>( type, value );
}
}
#endif // SILVERLIGHT
public static bool GetHasDefaultValue( this ParameterInfo source )
{
#if NET35 || NET40 || SILVERLIGHT || UNITY
return source.DefaultValue != DBNull.Value;
#else
return source.HasDefaultValue;
#endif
}
}
}