~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json/Utilities/TypeExtensions.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
ļ»æ#region License
 
2
// Copyright (c) 2007 James Newton-King
 
3
//
 
4
// Permission is hereby granted, free of charge, to any person
 
5
// obtaining a copy of this software and associated documentation
 
6
// files (the "Software"), to deal in the Software without
 
7
// restriction, including without limitation the rights to use,
 
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
 
9
// copies of the Software, and to permit persons to whom the
 
10
// Software is furnished to do so, subject to the following
 
11
// conditions:
 
12
//
 
13
// The above copyright notice and this permission notice shall be
 
14
// included in all copies or substantial portions of the Software.
 
15
//
 
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 
23
// OTHER DEALINGS IN THE SOFTWARE.
 
24
#endregion
 
25
 
 
26
using System;
 
27
using System.Collections.Generic;
 
28
using System.Reflection;
 
29
#if NET20
 
30
using Newtonsoft.Json.Utilities.LinqBridge;
 
31
#else
 
32
using System.Linq;
 
33
#endif
 
34
 
 
35
namespace Newtonsoft.Json.Utilities
 
36
{
 
37
  internal static class TypeExtensions
 
38
  {
 
39
#if NETFX_CORE
 
40
    private static BindingFlags DefaultFlags = BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
 
41
 
 
42
    public static MethodInfo GetGetMethod(this PropertyInfo propertyInfo)
 
43
    {
 
44
      return propertyInfo.GetGetMethod(false);
 
45
    }
 
46
 
 
47
    public static MethodInfo GetGetMethod(this PropertyInfo propertyInfo, bool nonPublic)
 
48
    {
 
49
      MethodInfo getMethod = propertyInfo.GetMethod;
 
50
      if (getMethod != null && (getMethod.IsPublic || nonPublic))
 
51
        return getMethod;
 
52
 
 
53
      return null;
 
54
    }
 
55
 
 
56
    public static MethodInfo GetSetMethod(this PropertyInfo propertyInfo)
 
57
    {
 
58
      return propertyInfo.GetSetMethod(false);
 
59
    }
 
60
 
 
61
    public static MethodInfo GetSetMethod(this PropertyInfo propertyInfo, bool nonPublic)
 
62
    {
 
63
      MethodInfo setMethod = propertyInfo.SetMethod;
 
64
      if (setMethod != null && (setMethod.IsPublic || nonPublic))
 
65
        return setMethod;
 
66
 
 
67
      return null;
 
68
    }
 
69
 
 
70
    public static bool IsSubclassOf(this Type type, Type c)
 
71
    {
 
72
      return type.GetTypeInfo().IsSubclassOf(c);
 
73
    }
 
74
 
 
75
    public static bool IsAssignableFrom(this Type type, Type c)
 
76
    {
 
77
      return type.GetTypeInfo().IsAssignableFrom(c.GetTypeInfo());
 
78
    }
 
79
#endif
 
80
 
 
81
    public static MemberTypes MemberType(this MemberInfo memberInfo)
 
82
    {
 
83
#if !(NETFX_CORE || PORTABLE)
 
84
      return memberInfo.MemberType;
 
85
#else
 
86
      if (memberInfo is PropertyInfo)
 
87
        return MemberTypes.Property;
 
88
      else if (memberInfo is FieldInfo)
 
89
        return MemberTypes.Field;
 
90
      else if (memberInfo is EventInfo)
 
91
        return MemberTypes.Event;
 
92
      else if (memberInfo is MethodInfo)
 
93
        return MemberTypes.Method;
 
94
      else
 
95
        return MemberTypes.Other;
 
96
#endif
 
97
    }
 
98
 
 
99
    public static bool ContainsGenericParameters(this Type type)
 
100
    {
 
101
#if !NETFX_CORE
 
102
      return type.ContainsGenericParameters;
 
103
#else
 
104
      return type.GetTypeInfo().ContainsGenericParameters;
 
105
#endif
 
106
    }
 
107
 
 
108
    public static bool IsInterface(this Type type)
 
109
    {
 
110
#if !NETFX_CORE
 
111
      return type.IsInterface;
 
112
#else
 
113
      return type.GetTypeInfo().IsInterface;
 
114
#endif
 
115
    }
 
116
 
 
117
    public static bool IsGenericType(this Type type)
 
118
    {
 
119
#if !NETFX_CORE
 
120
      return type.IsGenericType;
 
121
#else
 
122
      return type.GetTypeInfo().IsGenericType;
 
123
#endif
 
124
    }
 
125
 
 
126
    public static bool IsGenericTypeDefinition(this Type type)
 
127
    {
 
128
#if !NETFX_CORE
 
129
      return type.IsGenericTypeDefinition;
 
130
#else
 
131
      return type.GetTypeInfo().IsGenericTypeDefinition;
 
132
#endif
 
133
    }
 
134
 
 
135
    public static Type BaseType(this Type type)
 
136
    {
 
137
#if !NETFX_CORE
 
138
      return type.BaseType;
 
139
#else
 
140
      return type.GetTypeInfo().BaseType;
 
141
#endif
 
142
    }
 
143
 
 
144
    public static bool IsEnum(this Type type)
 
145
    {
 
146
#if !NETFX_CORE
 
147
      return type.IsEnum;
 
148
#else
 
149
      return type.GetTypeInfo().IsEnum;
 
150
#endif
 
151
    }
 
152
 
 
153
    public static bool IsClass(this Type type)
 
154
    {
 
155
#if !NETFX_CORE
 
156
      return type.IsClass;
 
157
#else
 
158
      return type.GetTypeInfo().IsClass;
 
159
#endif
 
160
    }
 
161
 
 
162
    public static bool IsSealed(this Type type)
 
163
    {
 
164
#if !NETFX_CORE
 
165
      return type.IsSealed;
 
166
#else
 
167
      return type.GetTypeInfo().IsSealed;
 
168
#endif
 
169
    }
 
170
 
 
171
#if PORTABLE
 
172
    public static PropertyInfo GetProperty(this Type type, string name, BindingFlags bindingFlags, object placeholder1, Type propertyType, IList<Type> indexParameters, object placeholder2)
 
173
    {
 
174
      IList<PropertyInfo> propertyInfos = type.GetProperties(bindingFlags);
 
175
 
 
176
      return propertyInfos.Where(p =>
 
177
      {
 
178
        if (name != null && name != p.Name)
 
179
          return false;
 
180
        if (propertyType != null && propertyType != p.PropertyType)
 
181
          return false;
 
182
        if (indexParameters != null)
 
183
        {
 
184
          if (!p.GetIndexParameters().Select(ip => ip.ParameterType).SequenceEqual(indexParameters))
 
185
            return false;
 
186
        }
 
187
 
 
188
        return true;
 
189
      }).SingleOrDefault();
 
190
    }
 
191
 
 
192
    public static IEnumerable<MemberInfo> GetMember(this Type type, string name, MemberTypes memberType, BindingFlags bindingFlags)
 
193
    {
 
194
      return type.GetMembers(bindingFlags).Where(m =>
 
195
        {
 
196
          if (name != null && name != m.Name)
 
197
            return false;
 
198
          if (m.MemberType() != memberType)
 
199
            return false;
 
200
 
 
201
          return true;
 
202
        });
 
203
    }
 
204
#endif
 
205
 
 
206
#if NETFX_CORE
 
207
    public static bool IsDefined(this Type type, Type attributeType, bool inherit)
 
208
    {
 
209
      return type.GetTypeInfo().CustomAttributes.Any(a => a.AttributeType == attributeType);
 
210
    }
 
211
 
 
212
    public static MethodInfo GetMethod(this Type type, string name)
 
213
    {
 
214
      return type.GetMethod(name, DefaultFlags);
 
215
    }
 
216
 
 
217
    public static MethodInfo GetMethod(this Type type, string name, BindingFlags bindingFlags)
 
218
    {
 
219
      return type.GetTypeInfo().GetDeclaredMethod(name);
 
220
    }
 
221
 
 
222
    public static MethodInfo GetMethod(this Type type, IList<Type> parameterTypes)
 
223
    {
 
224
      return type.GetMethod(null, parameterTypes);
 
225
    }
 
226
 
 
227
    public static MethodInfo GetMethod(this Type type, string name, IList<Type> parameterTypes)
 
228
    {
 
229
      return type.GetMethod(name, DefaultFlags, null, parameterTypes, null);
 
230
    }
 
231
 
 
232
    public static MethodInfo GetMethod(this Type type, string name, BindingFlags bindingFlags, object placeHolder1, IList<Type> parameterTypes, object placeHolder2)
 
233
    {
 
234
      return type.GetTypeInfo().DeclaredMethods.Where(m =>
 
235
      {
 
236
        if (name != null && m.Name != name)
 
237
          return false;
 
238
 
 
239
        if (!TestAccessibility(m, bindingFlags))
 
240
          return false;
 
241
 
 
242
        return m.GetParameters().Select(p => p.ParameterType).SequenceEqual(parameterTypes);
 
243
      }).SingleOrDefault();
 
244
    }
 
245
 
 
246
    public static PropertyInfo GetProperty(this Type type, string name, BindingFlags bindingFlags, object placeholder1, Type propertyType, IList<Type> indexParameters, object placeholder2)
 
247
    {
 
248
      return type.GetTypeInfo().DeclaredProperties.Where(p =>
 
249
      {
 
250
        if (name != null && name != p.Name)
 
251
          return false;
 
252
        if (propertyType != null && propertyType != p.PropertyType)
 
253
          return false;
 
254
        if (indexParameters != null)
 
255
        {
 
256
          if (!p.GetIndexParameters().Select(ip => ip.ParameterType).SequenceEqual(indexParameters))
 
257
            return false;
 
258
        }
 
259
 
 
260
        return true;
 
261
      }).SingleOrDefault();
 
262
    }
 
263
 
 
264
    public static IEnumerable<MemberInfo> GetMember(this Type type, string name, MemberTypes memberType, BindingFlags bindingFlags)
 
265
    {
 
266
      return type.GetTypeInfo().GetMembersRecursive().Where(m =>
 
267
      {
 
268
        if (name != null && name != m.Name)
 
269
          return false;
 
270
        if (m.MemberType() != memberType)
 
271
          return false;
 
272
        if (!TestAccessibility(m, bindingFlags))
 
273
          return false;
 
274
 
 
275
        return true;
 
276
      });
 
277
    }
 
278
 
 
279
    public static IEnumerable<ConstructorInfo> GetConstructors(this Type type)
 
280
    {
 
281
      return type.GetConstructors(DefaultFlags);
 
282
    }
 
283
 
 
284
    public static IEnumerable<ConstructorInfo> GetConstructors(this Type type, BindingFlags bindingFlags)
 
285
    {
 
286
      return type.GetConstructors(bindingFlags, null);
 
287
    }
 
288
 
 
289
    private static IEnumerable<ConstructorInfo> GetConstructors(this Type type, BindingFlags bindingFlags, IList<Type> parameterTypes)
 
290
    {
 
291
      return type.GetTypeInfo().DeclaredConstructors.Where(c =>
 
292
      {
 
293
        if (!TestAccessibility(c, bindingFlags))
 
294
          return false;
 
295
 
 
296
        if (parameterTypes != null && !c.GetParameters().Select(p => p.ParameterType).SequenceEqual(parameterTypes))
 
297
          return false;
 
298
 
 
299
        return true;
 
300
      });
 
301
    }
 
302
 
 
303
    public static ConstructorInfo GetConstructor(this Type type, IList<Type> parameterTypes)
 
304
    {
 
305
      return type.GetConstructor(DefaultFlags, null, parameterTypes, null);
 
306
    }
 
307
 
 
308
    public static ConstructorInfo GetConstructor(this Type type, BindingFlags bindingFlags, object placeholder1, IList<Type> parameterTypes, object placeholder2)
 
309
    {
 
310
      return type.GetConstructors(bindingFlags, parameterTypes).SingleOrDefault();
 
311
    }
 
312
 
 
313
    public static MemberInfo[] GetMember(this Type type, string member)
 
314
    {
 
315
      return type.GetMember(member, DefaultFlags);
 
316
    }
 
317
 
 
318
    public static MemberInfo[] GetMember(this Type type, string member, BindingFlags bindingFlags)
 
319
    {
 
320
      return type.GetTypeInfo().GetMembersRecursive().Where(m => m.Name == member && TestAccessibility(m, bindingFlags)).ToArray();
 
321
    }
 
322
 
 
323
    public static MemberInfo GetField(this Type type, string member)
 
324
    {
 
325
      return type.GetField(member, DefaultFlags);
 
326
    }
 
327
 
 
328
    public static MemberInfo GetField(this Type type, string member, BindingFlags bindingFlags)
 
329
    {
 
330
      return type.GetTypeInfo().GetDeclaredField(member);
 
331
    }
 
332
 
 
333
    public static IEnumerable<PropertyInfo> GetProperties(this Type type, BindingFlags bindingFlags)
 
334
    {
 
335
      return type.GetTypeInfo().GetPropertiesRecursive().Where(p => TestAccessibility(p, bindingFlags));
 
336
    }
 
337
 
 
338
    private static IList<MemberInfo> GetMembersRecursive(this TypeInfo type)
 
339
    {
 
340
      TypeInfo t = type;
 
341
      IList<MemberInfo> members = new List<MemberInfo>();
 
342
      while (t != null)
 
343
      {
 
344
        foreach (var member in t.DeclaredMembers)
 
345
        {
 
346
          if (!members.Any(p => p.Name == member.Name))
 
347
            members.Add(member);
 
348
        }
 
349
        t = (t.BaseType != null) ? t.BaseType.GetTypeInfo() : null;
 
350
      }
 
351
 
 
352
      return members;
 
353
    }
 
354
 
 
355
    private static IList<PropertyInfo> GetPropertiesRecursive(this TypeInfo type)
 
356
    {
 
357
      TypeInfo t = type;
 
358
      IList<PropertyInfo> properties = new List<PropertyInfo>();
 
359
      while (t != null)
 
360
      {
 
361
        foreach (var member in t.DeclaredProperties)
 
362
        {
 
363
          if (!properties.Any(p => p.Name == member.Name))
 
364
            properties.Add(member);
 
365
        }
 
366
        t = (t.BaseType != null) ? t.BaseType.GetTypeInfo() : null;
 
367
      }
 
368
 
 
369
      return properties;
 
370
    }
 
371
 
 
372
    private static IList<FieldInfo> GetFieldsRecursive(this TypeInfo type)
 
373
    {
 
374
      TypeInfo t = type;
 
375
      IList<FieldInfo> fields = new List<FieldInfo>();
 
376
      while (t != null)
 
377
      {
 
378
        foreach (var member in t.DeclaredFields)
 
379
        {
 
380
          if (!fields.Any(p => p.Name == member.Name))
 
381
            fields.Add(member);
 
382
        }
 
383
        t = (t.BaseType != null) ? t.BaseType.GetTypeInfo() : null;
 
384
      }
 
385
 
 
386
      return fields;
 
387
    }
 
388
 
 
389
    public static IEnumerable<MethodInfo> GetMethods(this Type type, BindingFlags bindingFlags)
 
390
    {
 
391
      return type.GetTypeInfo().DeclaredMethods;
 
392
    }
 
393
 
 
394
    public static PropertyInfo GetProperty(this Type type, string name)
 
395
    {
 
396
      return type.GetProperty(name, DefaultFlags);
 
397
    }
 
398
 
 
399
    public static PropertyInfo GetProperty(this Type type, string name, BindingFlags bindingFlags)
 
400
    {
 
401
      return type.GetTypeInfo().GetDeclaredProperty(name);
 
402
    }
 
403
 
 
404
    public static IEnumerable<FieldInfo> GetFields(this Type type)
 
405
    {
 
406
      return type.GetFields(DefaultFlags);
 
407
    }
 
408
 
 
409
    public static IEnumerable<FieldInfo> GetFields(this Type type, BindingFlags bindingFlags)
 
410
    {
 
411
      return type.GetTypeInfo().GetFieldsRecursive().Where(f => TestAccessibility(f, bindingFlags)).ToList();
 
412
    }
 
413
 
 
414
    private static bool TestAccessibility(PropertyInfo member, BindingFlags bindingFlags)
 
415
    {
 
416
      if (member.GetMethod != null && TestAccessibility(member.GetMethod, bindingFlags))
 
417
        return true;
 
418
 
 
419
      if (member.SetMethod != null && TestAccessibility(member.SetMethod, bindingFlags))
 
420
        return true;
 
421
 
 
422
      return false;
 
423
    }
 
424
 
 
425
    private static bool TestAccessibility(MemberInfo member, BindingFlags bindingFlags)
 
426
    {
 
427
      if (member is FieldInfo)
 
428
      {
 
429
        return TestAccessibility((FieldInfo)member, bindingFlags);
 
430
      }
 
431
      else if (member is MethodBase)
 
432
      {
 
433
        return TestAccessibility((MethodBase)member, bindingFlags);
 
434
      }
 
435
      else if (member is PropertyInfo)
 
436
      {
 
437
        return TestAccessibility((PropertyInfo)member, bindingFlags);
 
438
      }
 
439
 
 
440
      throw new Exception("Unexpected member type.");
 
441
    }
 
442
 
 
443
    private static bool TestAccessibility(FieldInfo member, BindingFlags bindingFlags)
 
444
    {
 
445
      bool visibility = (member.IsPublic && bindingFlags.HasFlag(BindingFlags.Public)) ||
 
446
        (!member.IsPublic && bindingFlags.HasFlag(BindingFlags.NonPublic));
 
447
 
 
448
      bool instance = (member.IsStatic && bindingFlags.HasFlag(BindingFlags.Static)) ||
 
449
        (!member.IsStatic && bindingFlags.HasFlag(BindingFlags.Instance));
 
450
 
 
451
      return visibility && instance;
 
452
    }
 
453
 
 
454
    private static bool TestAccessibility(MethodBase member, BindingFlags bindingFlags)
 
455
    {
 
456
      bool visibility = (member.IsPublic && bindingFlags.HasFlag(BindingFlags.Public)) ||
 
457
        (!member.IsPublic && bindingFlags.HasFlag(BindingFlags.NonPublic));
 
458
 
 
459
      bool instance = (member.IsStatic && bindingFlags.HasFlag(BindingFlags.Static)) ||
 
460
        (!member.IsStatic && bindingFlags.HasFlag(BindingFlags.Instance));
 
461
 
 
462
      return visibility && instance;
 
463
    }
 
464
 
 
465
    public static Type[] GetGenericArguments(this Type type)
 
466
    {
 
467
      return type.GetTypeInfo().GenericTypeArguments;
 
468
    }
 
469
 
 
470
    public static IEnumerable<Type> GetInterfaces(this Type type)
 
471
    {
 
472
      return type.GetTypeInfo().ImplementedInterfaces;
 
473
    }
 
474
 
 
475
    public static IEnumerable<MethodInfo> GetMethods(this Type type)
 
476
    {
 
477
      return type.GetTypeInfo().DeclaredMethods;
 
478
    }
 
479
#endif
 
480
 
 
481
    public static bool IsAbstract(this Type type)
 
482
    {
 
483
#if !NETFX_CORE
 
484
      return type.IsAbstract;
 
485
#else
 
486
      return type.GetTypeInfo().IsAbstract;
 
487
#endif
 
488
    }
 
489
 
 
490
    public static bool IsVisible(this Type type)
 
491
    {
 
492
#if !NETFX_CORE
 
493
      return type.IsVisible;
 
494
#else
 
495
      return type.GetTypeInfo().IsVisible;
 
496
#endif
 
497
    }
 
498
 
 
499
    public static bool IsValueType(this Type type)
 
500
    {
 
501
#if !NETFX_CORE
 
502
      return type.IsValueType;
 
503
#else
 
504
      return type.GetTypeInfo().IsValueType;
 
505
#endif
 
506
    }
 
507
 
 
508
    public static bool AssignableToTypeName(this Type type, string fullTypeName, out Type match)
 
509
    {
 
510
      Type current = type;
 
511
 
 
512
      while (current != null)
 
513
      {
 
514
        if (string.Equals(current.FullName, fullTypeName, StringComparison.Ordinal))
 
515
        {
 
516
          match = current;
 
517
          return true;
 
518
        }
 
519
 
 
520
        current = current.BaseType();
 
521
      }
 
522
 
 
523
      foreach (Type i in type.GetInterfaces())
 
524
      {
 
525
        if (string.Equals(i.Name, fullTypeName, StringComparison.Ordinal))
 
526
        {
 
527
          match = type;
 
528
          return true;
 
529
        }
 
530
      }
 
531
 
 
532
      match = null;
 
533
      return false;
 
534
    }
 
535
 
 
536
    public static bool AssignableToTypeName(this Type type, string fullTypeName)
 
537
    {
 
538
      Type match;
 
539
      return type.AssignableToTypeName(fullTypeName, out match);
 
540
    }
 
541
 
 
542
    public static MethodInfo GetGenericMethod(this Type type, string name, params Type[] parameterTypes)
 
543
    {
 
544
      var methods = type.GetMethods().Where(method => method.Name == name);
 
545
 
 
546
      foreach (var method in methods)
 
547
      {
 
548
        if (method.HasParameters(parameterTypes))
 
549
          return method;
 
550
      }
 
551
 
 
552
      return null;
 
553
    }
 
554
 
 
555
    public static bool HasParameters(this MethodInfo method, params Type[] parameterTypes)
 
556
    {
 
557
      var methodParameters = method.GetParameters().Select(parameter => parameter.ParameterType).ToArray();
 
558
 
 
559
      if (methodParameters.Length != parameterTypes.Length)
 
560
        return false;
 
561
 
 
562
      for (int i = 0; i < methodParameters.Length; i++)
 
563
        if (methodParameters[i].ToString() != parameterTypes[i].ToString())
 
564
          return false;
 
565
 
 
566
      return true;
 
567
    }
 
568
 
 
569
    public static IEnumerable<Type> GetAllInterfaces(this Type target)
 
570
    {
 
571
      foreach (var i in target.GetInterfaces())
 
572
      {
 
573
        yield return i;
 
574
        foreach (var ci in i.GetInterfaces())
 
575
        {
 
576
          yield return ci;
 
577
        }
 
578
      }
 
579
    }
 
580
 
 
581
    public static IEnumerable<MethodInfo> GetAllMethods(this Type target)
 
582
    {
 
583
      var allTypes = target.GetAllInterfaces().ToList();
 
584
      allTypes.Add(target);
 
585
 
 
586
      return from type in allTypes
 
587
             from method in type.GetMethods()
 
588
             select method;
 
589
    }
 
590
  }
 
591
}
 
 
b'\\ No newline at end of file'