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

« back to all changes in this revision

Viewing changes to external/ikvm/reflect/Type.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
/*
 
2
  Copyright (C) 2009-2012 Jeroen Frijters
 
3
 
 
4
  This software is provided 'as-is', without any express or implied
 
5
  warranty.  In no event will the authors be held liable for any damages
 
6
  arising from the use of this software.
 
7
 
 
8
  Permission is granted to anyone to use this software for any purpose,
 
9
  including commercial applications, and to alter it and redistribute it
 
10
  freely, subject to the following restrictions:
 
11
 
 
12
  1. The origin of this software must not be misrepresented; you must not
 
13
     claim that you wrote the original software. If you use this software
 
14
     in a product, an acknowledgment in the product documentation would be
 
15
     appreciated but is not required.
 
16
  2. Altered source versions must be plainly marked as such, and must not be
 
17
     misrepresented as being the original software.
 
18
  3. This notice may not be removed or altered from any source distribution.
 
19
 
 
20
  Jeroen Frijters
 
21
  jeroen@frijters.net
 
22
  
 
23
*/
 
24
using System;
 
25
using System.Runtime.InteropServices;
 
26
using System.Text;
 
27
using System.Collections.Generic;
 
28
using IKVM.Reflection.Emit;
 
29
 
 
30
namespace IKVM.Reflection
 
31
{
 
32
        interface IGenericContext
 
33
        {
 
34
                Type GetGenericTypeArgument(int index);
 
35
                Type GetGenericMethodArgument(int index);
 
36
        }
 
37
 
 
38
        interface IGenericBinder
 
39
        {
 
40
                Type BindTypeParameter(Type type);
 
41
                Type BindMethodParameter(Type type);
 
42
        }
 
43
 
 
44
        public abstract class Type : MemberInfo, IGenericContext, IGenericBinder
 
45
        {
 
46
                public static readonly Type[] EmptyTypes = Empty<Type>.Array;
 
47
                protected readonly Type underlyingType;
 
48
                protected TypeFlags typeFlags;
 
49
 
 
50
                [Flags]
 
51
                protected enum TypeFlags
 
52
                {
 
53
                        // for use by TypeBuilder or TypeDefImpl
 
54
                        IsGenericTypeDefinition = 1,
 
55
 
 
56
                        // for use by TypeBuilder
 
57
                        HasNestedTypes = 2,
 
58
                        Baked = 4,
 
59
 
 
60
                        // for use by MissingType
 
61
                        ValueType = 8,
 
62
                        NotValueType = 16,
 
63
 
 
64
                        // for use by TypeDefImpl, TypeBuilder or MissingType
 
65
                        PotentialEnumOrValueType = 32,
 
66
                        EnumOrValueType = 64,
 
67
 
 
68
                        // for use by TypeDefImpl
 
69
                        NotGenericTypeDefinition = 128,
 
70
                }
 
71
 
 
72
                // prevent subclassing by outsiders
 
73
                internal Type()
 
74
                {
 
75
                        this.underlyingType = this;
 
76
                }
 
77
 
 
78
                internal Type(Type underlyingType)
 
79
                {
 
80
                        System.Diagnostics.Debug.Assert(underlyingType.underlyingType == underlyingType);
 
81
                        this.underlyingType = underlyingType;
 
82
                        this.typeFlags = underlyingType.typeFlags;
 
83
                }
 
84
 
 
85
                public static Binder DefaultBinder
 
86
                {
 
87
                        get { return new DefaultBinder(); }
 
88
                }
 
89
 
 
90
                public sealed override MemberTypes MemberType
 
91
                {
 
92
                        get { return IsNested ? MemberTypes.NestedType : MemberTypes.TypeInfo; }
 
93
                }
 
94
 
 
95
                public virtual string AssemblyQualifiedName
 
96
                {
 
97
                        // NOTE the assembly name is not escaped here, only when used in a generic type instantiation
 
98
                        get { return this.FullName + ", " + this.Assembly.FullName; }
 
99
                }
 
100
 
 
101
                public abstract Type BaseType
 
102
                {
 
103
                        get;
 
104
                }
 
105
 
 
106
                public abstract TypeAttributes Attributes
 
107
                {
 
108
                        get;
 
109
                }
 
110
 
 
111
                public virtual Type GetElementType()
 
112
                {
 
113
                        return null;
 
114
                }
 
115
 
 
116
                internal virtual void CheckBaked()
 
117
                {
 
118
                }
 
119
 
 
120
                public virtual Type[] __GetDeclaredTypes()
 
121
                {
 
122
                        return Type.EmptyTypes;
 
123
                }
 
124
 
 
125
                public virtual Type[] __GetDeclaredInterfaces()
 
126
                {
 
127
                        return Type.EmptyTypes;
 
128
                }
 
129
 
 
130
                public virtual MethodBase[] __GetDeclaredMethods()
 
131
                {
 
132
                        return Empty<MethodBase>.Array;
 
133
                }
 
134
 
 
135
                public virtual __MethodImplMap __GetMethodImplMap()
 
136
                {
 
137
                        throw new NotSupportedException();
 
138
                }
 
139
 
 
140
                public virtual FieldInfo[] __GetDeclaredFields()
 
141
                {
 
142
                        return Empty<FieldInfo>.Array;
 
143
                }
 
144
 
 
145
                public virtual EventInfo[] __GetDeclaredEvents()
 
146
                {
 
147
                        return Empty<EventInfo>.Array;
 
148
                }
 
149
 
 
150
                public virtual PropertyInfo[] __GetDeclaredProperties()
 
151
                {
 
152
                        return Empty<PropertyInfo>.Array;
 
153
                }
 
154
 
 
155
                public virtual CustomModifiers __GetCustomModifiers()
 
156
                {
 
157
                        return new CustomModifiers();
 
158
                }
 
159
 
 
160
                [Obsolete("Please use __GetCustomModifiers() instead.")]
 
161
                public Type[] __GetRequiredCustomModifiers()
 
162
                {
 
163
                        return __GetCustomModifiers().GetRequired();
 
164
                }
 
165
 
 
166
                [Obsolete("Please use __GetCustomModifiers() instead.")]
 
167
                public Type[] __GetOptionalCustomModifiers()
 
168
                {
 
169
                        return __GetCustomModifiers().GetOptional();
 
170
                }
 
171
 
 
172
                public virtual __StandAloneMethodSig __MethodSignature
 
173
                {
 
174
                        get { throw new InvalidOperationException(); }
 
175
                }
 
176
 
 
177
                public virtual bool HasElementType
 
178
                {
 
179
                        get { return false; }
 
180
                }
 
181
 
 
182
                public virtual bool IsArray
 
183
                {
 
184
                        get { return false; }
 
185
                }
 
186
 
 
187
                public virtual bool __IsVector
 
188
                {
 
189
                        get { return false; }
 
190
                }
 
191
 
 
192
                public virtual bool IsByRef
 
193
                {
 
194
                        get { return false; }
 
195
                }
 
196
 
 
197
                public virtual bool IsPointer
 
198
                {
 
199
                        get { return false; }
 
200
                }
 
201
 
 
202
                public virtual bool __IsFunctionPointer
 
203
                {
 
204
                        get { return false; }
 
205
                }
 
206
 
 
207
                public virtual bool IsValueType
 
208
                {
 
209
                        get
 
210
                        {
 
211
                                Type baseType = this.BaseType;
 
212
                                return baseType != null
 
213
                                        && baseType.IsEnumOrValueType
 
214
                                        && !this.IsEnumOrValueType;
 
215
                        }
 
216
                }
 
217
 
 
218
                public virtual bool IsGenericParameter
 
219
                {
 
220
                        get { return false; }
 
221
                }
 
222
 
 
223
                public virtual int GenericParameterPosition
 
224
                {
 
225
                        get { throw new NotSupportedException(); }
 
226
                }
 
227
 
 
228
                public virtual MethodBase DeclaringMethod
 
229
                {
 
230
                        get { return null; }
 
231
                }
 
232
 
 
233
                public Type UnderlyingSystemType
 
234
                {
 
235
                        get { return underlyingType; }
 
236
                }
 
237
 
 
238
                public override Type DeclaringType
 
239
                {
 
240
                        get { return null; }
 
241
                }
 
242
 
 
243
                public virtual string __Name
 
244
                {
 
245
                        get { throw new InvalidOperationException(); }
 
246
                }
 
247
 
 
248
                public virtual string __Namespace
 
249
                {
 
250
                        get { throw new InvalidOperationException(); }
 
251
                }
 
252
 
 
253
                public abstract override string Name
 
254
                {
 
255
                        get;
 
256
                }
 
257
 
 
258
                public virtual string Namespace
 
259
                {
 
260
                        get
 
261
                        {
 
262
                                if (IsNested)
 
263
                                {
 
264
                                        return DeclaringType.Namespace;
 
265
                                }
 
266
                                return __Namespace;
 
267
                        }
 
268
                }
 
269
 
 
270
                internal virtual int GetModuleBuilderToken()
 
271
                {
 
272
                        throw new InvalidOperationException();
 
273
                }
 
274
 
 
275
                public static bool operator ==(Type t1, Type t2)
 
276
                {
 
277
                        // Casting to object results in smaller code than calling ReferenceEquals and makes
 
278
                        // this method more likely to be inlined.
 
279
                        // On CLR v2 x86, microbenchmarks show this to be faster than calling ReferenceEquals.
 
280
                        return (object)t1 == (object)t2
 
281
                                || ((object)t1 != null && (object)t2 != null && (object)t1.underlyingType == (object)t2.underlyingType);
 
282
                }
 
283
 
 
284
                public static bool operator !=(Type t1, Type t2)
 
285
                {
 
286
                        return !(t1 == t2);
 
287
                }
 
288
 
 
289
                public bool Equals(Type type)
 
290
                {
 
291
                        return this == type;
 
292
                }
 
293
 
 
294
                public override bool Equals(object obj)
 
295
                {
 
296
                        return Equals(obj as Type);
 
297
                }
 
298
 
 
299
                public override int GetHashCode()
 
300
                {
 
301
                        Type type = this.UnderlyingSystemType;
 
302
                        return ReferenceEquals(type, this) ? base.GetHashCode() : type.GetHashCode();
 
303
                }
 
304
 
 
305
                public Type[] GenericTypeArguments
 
306
                {
 
307
                        get { return IsConstructedGenericType ? GetGenericArguments() : Type.EmptyTypes; }
 
308
                }
 
309
 
 
310
                public virtual Type[] GetGenericArguments()
 
311
                {
 
312
                        return Type.EmptyTypes;
 
313
                }
 
314
 
 
315
                public virtual CustomModifiers[] __GetGenericArgumentsCustomModifiers()
 
316
                {
 
317
                        return Empty<CustomModifiers>.Array;
 
318
                }
 
319
 
 
320
                [Obsolete("Please use __GetGenericArgumentsCustomModifiers() instead")]
 
321
                public Type[][] __GetGenericArgumentsRequiredCustomModifiers()
 
322
                {
 
323
                        CustomModifiers[] customModifiers = __GetGenericArgumentsCustomModifiers();
 
324
                        Type[][] array = new Type[customModifiers.Length][];
 
325
                        for (int i = 0; i < array.Length; i++)
 
326
                        {
 
327
                                array[i] = customModifiers[i].GetRequired();
 
328
                        }
 
329
                        return array;
 
330
                }
 
331
 
 
332
                [Obsolete("Please use __GetGenericArgumentsCustomModifiers() instead")]
 
333
                public Type[][] __GetGenericArgumentsOptionalCustomModifiers()
 
334
                {
 
335
                        CustomModifiers[] customModifiers = __GetGenericArgumentsCustomModifiers();
 
336
                        Type[][] array = new Type[customModifiers.Length][];
 
337
                        for (int i = 0; i < array.Length; i++)
 
338
                        {
 
339
                                array[i] = customModifiers[i].GetOptional();
 
340
                        }
 
341
                        return array;
 
342
                }
 
343
 
 
344
                public virtual Type GetGenericTypeDefinition()
 
345
                {
 
346
                        throw new InvalidOperationException();
 
347
                }
 
348
 
 
349
                public virtual StructLayoutAttribute StructLayoutAttribute
 
350
                {
 
351
                        get { return null; }
 
352
                }
 
353
 
 
354
                public virtual bool __GetLayout(out int packingSize, out int typeSize)
 
355
                {
 
356
                        packingSize = 0;
 
357
                        typeSize = 0;
 
358
                        return false;
 
359
                }
 
360
 
 
361
                public virtual bool IsGenericType
 
362
                {
 
363
                        get { return false; }
 
364
                }
 
365
 
 
366
                public virtual bool IsGenericTypeDefinition
 
367
                {
 
368
                        get { return false; }
 
369
                }
 
370
 
 
371
                // .NET 4.5 API
 
372
                public virtual bool IsConstructedGenericType
 
373
                {
 
374
                        get { return false; }
 
375
                }
 
376
 
 
377
                public virtual bool ContainsGenericParameters
 
378
                {
 
379
                        get
 
380
                        {
 
381
                                if (this.IsGenericParameter)
 
382
                                {
 
383
                                        return true;
 
384
                                }
 
385
                                foreach (Type arg in this.GetGenericArguments())
 
386
                                {
 
387
                                        if (arg.ContainsGenericParameters)
 
388
                                        {
 
389
                                                return true;
 
390
                                        }
 
391
                                }
 
392
                                return false;
 
393
                        }
 
394
                }
 
395
 
 
396
                public virtual Type[] GetGenericParameterConstraints()
 
397
                {
 
398
                        throw new InvalidOperationException();
 
399
                }
 
400
 
 
401
                public virtual GenericParameterAttributes GenericParameterAttributes
 
402
                {
 
403
                        get { throw new InvalidOperationException(); }
 
404
                }
 
405
 
 
406
                public virtual int GetArrayRank()
 
407
                {
 
408
                        throw new NotSupportedException();
 
409
                }
 
410
 
 
411
                public virtual int[] __GetArraySizes()
 
412
                {
 
413
                        throw new NotSupportedException();
 
414
                }
 
415
 
 
416
                public virtual int[] __GetArrayLowerBounds()
 
417
                {
 
418
                        throw new NotSupportedException();
 
419
                }
 
420
 
 
421
                // .NET 4.0 API
 
422
                public virtual Type GetEnumUnderlyingType()
 
423
                {
 
424
                        if (!this.IsEnum)
 
425
                        {
 
426
                                throw new ArgumentException();
 
427
                        }
 
428
                        CheckBaked();
 
429
                        return GetEnumUnderlyingTypeImpl();
 
430
                }
 
431
 
 
432
                internal Type GetEnumUnderlyingTypeImpl()
 
433
                {
 
434
                        foreach (FieldInfo field in __GetDeclaredFields())
 
435
                        {
 
436
                                if (!field.IsStatic)
 
437
                                {
 
438
                                        // the CLR assumes that an enum has only one instance field, so we can do the same
 
439
                                        return field.FieldType;
 
440
                                }
 
441
                        }
 
442
                        throw new InvalidOperationException();
 
443
                }
 
444
 
 
445
                public string[] GetEnumNames()
 
446
                {
 
447
                        if (!IsEnum)
 
448
                        {
 
449
                                throw new ArgumentException();
 
450
                        }
 
451
                        List<string> names = new List<string>();
 
452
                        foreach (FieldInfo field in __GetDeclaredFields())
 
453
                        {
 
454
                                if (field.IsLiteral)
 
455
                                {
 
456
                                        names.Add(field.Name);
 
457
                                }
 
458
                        }
 
459
                        return names.ToArray();
 
460
                }
 
461
 
 
462
                public string GetEnumName(object value)
 
463
                {
 
464
                        if (!IsEnum)
 
465
                        {
 
466
                                throw new ArgumentException();
 
467
                        }
 
468
                        if (value == null)
 
469
                        {
 
470
                                throw new ArgumentNullException();
 
471
                        }
 
472
                        try
 
473
                        {
 
474
                                value = Convert.ChangeType(value, GetTypeCode(GetEnumUnderlyingType()));
 
475
                        }
 
476
                        catch (FormatException)
 
477
                        {
 
478
                                throw new ArgumentException();
 
479
                        }
 
480
                        catch (OverflowException)
 
481
                        {
 
482
                                return null;
 
483
                        }
 
484
                        catch (InvalidCastException)
 
485
                        {
 
486
                                return null;
 
487
                        }
 
488
                        foreach (FieldInfo field in __GetDeclaredFields())
 
489
                        {
 
490
                                if (field.IsLiteral && field.GetRawConstantValue().Equals(value))
 
491
                                {
 
492
                                        return field.Name;
 
493
                                }
 
494
                        }
 
495
                        return null;
 
496
                }
 
497
 
 
498
                public bool IsEnumDefined(object value)
 
499
                {
 
500
                        if (value is string)
 
501
                        {
 
502
                                return Array.IndexOf(GetEnumNames(), value) != -1;
 
503
                        }
 
504
                        if (!IsEnum)
 
505
                        {
 
506
                                throw new ArgumentException();
 
507
                        }
 
508
                        if (value == null)
 
509
                        {
 
510
                                throw new ArgumentNullException();
 
511
                        }
 
512
                        if (System.Type.GetTypeCode(value.GetType()) != GetTypeCode(GetEnumUnderlyingType()))
 
513
                        {
 
514
                                throw new ArgumentException();
 
515
                        }
 
516
                        foreach (FieldInfo field in __GetDeclaredFields())
 
517
                        {
 
518
                                if (field.IsLiteral && field.GetRawConstantValue().Equals(value))
 
519
                                {
 
520
                                        return true;
 
521
                                }
 
522
                        }
 
523
                        return false;
 
524
                }
 
525
 
 
526
                public override string ToString()
 
527
                {
 
528
                        return FullName;
 
529
                }
 
530
 
 
531
                public abstract string FullName
 
532
                {
 
533
                        get;
 
534
                }
 
535
 
 
536
                protected string GetFullName()
 
537
                {
 
538
                        string ns = TypeNameParser.Escape(this.__Namespace);
 
539
                        Type decl = this.DeclaringType;
 
540
                        if (decl == null)
 
541
                        {
 
542
                                if (ns == null)
 
543
                                {
 
544
                                        return this.Name;
 
545
                                }
 
546
                                else
 
547
                                {
 
548
                                        return ns + "." + this.Name;
 
549
                                }
 
550
                        }
 
551
                        else
 
552
                        {
 
553
                                if (ns == null)
 
554
                                {
 
555
                                        return decl.FullName + "+" + this.Name;
 
556
                                }
 
557
                                else
 
558
                                {
 
559
                                        return decl.FullName + "+" + ns + "." + this.Name;
 
560
                                }
 
561
                        }
 
562
                }
 
563
 
 
564
                internal virtual bool IsModulePseudoType
 
565
                {
 
566
                        get { return false; }
 
567
                }
 
568
 
 
569
                internal virtual Type GetGenericTypeArgument(int index)
 
570
                {
 
571
                        throw new InvalidOperationException();
 
572
                }
 
573
 
 
574
                public MemberInfo[] GetDefaultMembers()
 
575
                {
 
576
                        Type defaultMemberAttribute = this.Module.universe.Import(typeof(System.Reflection.DefaultMemberAttribute));
 
577
                        foreach (CustomAttributeData cad in CustomAttributeData.GetCustomAttributes(this))
 
578
                        {
 
579
                                if (cad.Constructor.DeclaringType.Equals(defaultMemberAttribute))
 
580
                                {
 
581
                                        return GetMember((string)cad.ConstructorArguments[0].Value);
 
582
                                }
 
583
                        }
 
584
                        return Empty<MemberInfo>.Array;
 
585
                }
 
586
 
 
587
                public MemberInfo[] GetMember(string name)
 
588
                {
 
589
                        return GetMember(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
 
590
                }
 
591
 
 
592
                public MemberInfo[] GetMember(string name, BindingFlags bindingAttr)
 
593
                {
 
594
                        return GetMember(name, MemberTypes.All, bindingAttr);
 
595
                }
 
596
 
 
597
                public MemberInfo[] GetMembers()
 
598
                {
 
599
                        return GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
 
600
                }
 
601
 
 
602
                public MemberInfo[] GetMembers(BindingFlags bindingAttr)
 
603
                {
 
604
                        List<MemberInfo> members = new List<MemberInfo>();
 
605
                        members.AddRange(GetConstructors(bindingAttr));
 
606
                        members.AddRange(GetMethods(bindingAttr));
 
607
                        members.AddRange(GetFields(bindingAttr));
 
608
                        members.AddRange(GetProperties(bindingAttr));
 
609
                        members.AddRange(GetEvents(bindingAttr));
 
610
                        members.AddRange(GetNestedTypes(bindingAttr));
 
611
                        return members.ToArray();
 
612
                }
 
613
 
 
614
                public MemberInfo[] GetMember(string name, MemberTypes type, BindingFlags bindingAttr)
 
615
                {
 
616
                        MemberFilter filter;
 
617
                        if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
 
618
                        {
 
619
                                name = name.ToLowerInvariant();
 
620
                                filter = delegate(MemberInfo member, object filterCriteria) { return member.Name.ToLowerInvariant().Equals(filterCriteria); };
 
621
                        }
 
622
                        else
 
623
                        {
 
624
                                filter = delegate(MemberInfo member, object filterCriteria) { return member.Name.Equals(filterCriteria); };
 
625
                        }
 
626
                        return FindMembers(type, bindingAttr, filter, name);
 
627
                }
 
628
 
 
629
                private static void AddMembers(List<MemberInfo> list, MemberFilter filter, object filterCriteria, MemberInfo[] members)
 
630
                {
 
631
                        foreach (MemberInfo member in members)
 
632
                        {
 
633
                                if (filter == null || filter(member, filterCriteria))
 
634
                                {
 
635
                                        list.Add(member);
 
636
                                }
 
637
                        }
 
638
                }
 
639
 
 
640
                public MemberInfo[] FindMembers(MemberTypes memberType, BindingFlags bindingAttr, MemberFilter filter, object filterCriteria)
 
641
                {
 
642
                        List<MemberInfo> members = new List<MemberInfo>();
 
643
                        if ((memberType & MemberTypes.Constructor) != 0)
 
644
                        {
 
645
                                AddMembers(members, filter, filterCriteria, GetConstructors(bindingAttr));
 
646
                        }
 
647
                        if ((memberType & MemberTypes.Method) != 0)
 
648
                        {
 
649
                                AddMembers(members, filter, filterCriteria, GetMethods(bindingAttr));
 
650
                        }
 
651
                        if ((memberType & MemberTypes.Field) != 0)
 
652
                        {
 
653
                                AddMembers(members, filter, filterCriteria, GetFields(bindingAttr));
 
654
                        }
 
655
                        if ((memberType & MemberTypes.Property) != 0)
 
656
                        {
 
657
                                AddMembers(members, filter, filterCriteria, GetProperties(bindingAttr));
 
658
                        }
 
659
                        if ((memberType & MemberTypes.Event) != 0)
 
660
                        {
 
661
                                AddMembers(members, filter, filterCriteria, GetEvents(bindingAttr));
 
662
                        }
 
663
                        if ((memberType & MemberTypes.NestedType) != 0)
 
664
                        {
 
665
                                AddMembers(members, filter, filterCriteria, GetNestedTypes(bindingAttr));
 
666
                        }
 
667
                        return members.ToArray();
 
668
                }
 
669
 
 
670
                private MemberInfo[] GetMembers<T>()
 
671
                {
 
672
                        if (typeof(T) == typeof(ConstructorInfo) || typeof(T) == typeof(MethodInfo))
 
673
                        {
 
674
                                return __GetDeclaredMethods();
 
675
                        }
 
676
                        else if (typeof(T) == typeof(FieldInfo))
 
677
                        {
 
678
                                return __GetDeclaredFields();
 
679
                        }
 
680
                        else if (typeof(T) == typeof(PropertyInfo))
 
681
                        {
 
682
                                return __GetDeclaredProperties();
 
683
                        }
 
684
                        else if (typeof(T) == typeof(EventInfo))
 
685
                        {
 
686
                                return __GetDeclaredEvents();
 
687
                        }
 
688
                        else if (typeof(T) == typeof(Type))
 
689
                        {
 
690
                                return __GetDeclaredTypes();
 
691
                        }
 
692
                        else
 
693
                        {
 
694
                                throw new InvalidOperationException();
 
695
                        }
 
696
                }
 
697
 
 
698
                private T[] GetMembers<T>(BindingFlags flags)
 
699
                        where T : MemberInfo
 
700
                {
 
701
                        CheckBaked();
 
702
                        List<T> list = new List<T>();
 
703
                        foreach (MemberInfo member in GetMembers<T>())
 
704
                        {
 
705
                                if (member is T && member.BindingFlagsMatch(flags))
 
706
                                {
 
707
                                        list.Add((T)member);
 
708
                                }
 
709
                        }
 
710
                        if ((flags & BindingFlags.DeclaredOnly) == 0)
 
711
                        {
 
712
                                for (Type type = this.BaseType; type != null; type = type.BaseType)
 
713
                                {
 
714
                                        type.CheckBaked();
 
715
                                        foreach (MemberInfo member in type.GetMembers<T>())
 
716
                                        {
 
717
                                                if (member is T && member.BindingFlagsMatchInherited(flags))
 
718
                                                {
 
719
                                                        list.Add((T)member.SetReflectedType(this));
 
720
                                                }
 
721
                                        }
 
722
                                }
 
723
                        }
 
724
                        return list.ToArray();
 
725
                }
 
726
 
 
727
                private T GetMemberByName<T>(string name, BindingFlags flags, Predicate<T> filter)
 
728
                        where T : MemberInfo
 
729
                {
 
730
                        CheckBaked();
 
731
                        if ((flags & BindingFlags.IgnoreCase) != 0)
 
732
                        {
 
733
                                name = name.ToLowerInvariant();
 
734
                        }
 
735
                        T found = null;
 
736
                        foreach (MemberInfo member in GetMembers<T>())
 
737
                        {
 
738
                                if (member is T && member.BindingFlagsMatch(flags))
 
739
                                {
 
740
                                        string memberName = member.Name;
 
741
                                        if ((flags & BindingFlags.IgnoreCase) != 0)
 
742
                                        {
 
743
                                                memberName = memberName.ToLowerInvariant();
 
744
                                        }
 
745
                                        if (memberName == name && (filter == null || filter((T)member)))
 
746
                                        {
 
747
                                                if (found != null)
 
748
                                                {
 
749
                                                        throw new AmbiguousMatchException();
 
750
                                                }
 
751
                                                found = (T)member;
 
752
                                        }
 
753
                                }
 
754
                        }
 
755
                        if ((flags & BindingFlags.DeclaredOnly) == 0)
 
756
                        {
 
757
                                for (Type type = this.BaseType; (found == null || typeof(T) == typeof(MethodInfo)) && type != null; type = type.BaseType)
 
758
                                {
 
759
                                        type.CheckBaked();
 
760
                                        foreach (MemberInfo member in type.GetMembers<T>())
 
761
                                        {
 
762
                                                if (member is T && member.BindingFlagsMatchInherited(flags))
 
763
                                                {
 
764
                                                        string memberName = member.Name;
 
765
                                                        if ((flags & BindingFlags.IgnoreCase) != 0)
 
766
                                                        {
 
767
                                                                memberName = memberName.ToLowerInvariant();
 
768
                                                        }
 
769
                                                        if (memberName == name && (filter == null || filter((T)member)))
 
770
                                                        {
 
771
                                                                if (found != null)
 
772
                                                                {
 
773
                                                                        MethodInfo mi;
 
774
                                                                        // TODO does this depend on HideBySig vs HideByName?
 
775
                                                                        if ((mi = found as MethodInfo) != null
 
776
                                                                                && mi.MethodSignature.MatchParameterTypes(((MethodBase)member).MethodSignature))
 
777
                                                                        {
 
778
                                                                                continue;
 
779
                                                                        }
 
780
                                                                        throw new AmbiguousMatchException();
 
781
                                                                }
 
782
                                                                found = (T)member.SetReflectedType(this);
 
783
                                                        }
 
784
                                                }
 
785
                                        }
 
786
                                }
 
787
                        }
 
788
                        return found;
 
789
                }
 
790
 
 
791
                private T GetMemberByName<T>(string name, BindingFlags flags)
 
792
                        where T : MemberInfo
 
793
                {
 
794
                        return GetMemberByName<T>(name, flags, null);
 
795
                }
 
796
 
 
797
                public EventInfo GetEvent(string name)
 
798
                {
 
799
                        return GetEvent(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
 
800
                }
 
801
 
 
802
                public EventInfo GetEvent(string name, BindingFlags bindingAttr)
 
803
                {
 
804
                        return GetMemberByName<EventInfo>(name, bindingAttr);
 
805
                }
 
806
 
 
807
                public EventInfo[] GetEvents()
 
808
                {
 
809
                        return GetEvents(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
 
810
                }
 
811
 
 
812
                public EventInfo[] GetEvents(BindingFlags bindingAttr)
 
813
                {
 
814
                        return GetMembers<EventInfo>(bindingAttr);
 
815
                }
 
816
 
 
817
                public FieldInfo GetField(string name)
 
818
                {
 
819
                        return GetField(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
 
820
                }
 
821
 
 
822
                public FieldInfo GetField(string name, BindingFlags bindingAttr)
 
823
                {
 
824
                        return GetMemberByName<FieldInfo>(name, bindingAttr);
 
825
                }
 
826
 
 
827
                public FieldInfo[] GetFields()
 
828
                {
 
829
                        return GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
 
830
                }
 
831
 
 
832
                public FieldInfo[] GetFields(BindingFlags bindingAttr)
 
833
                {
 
834
                        return GetMembers<FieldInfo>(bindingAttr);
 
835
                }
 
836
 
 
837
                public Type[] GetInterfaces()
 
838
                {
 
839
                        List<Type> list = new List<Type>();
 
840
                        for (Type type = this; type != null; type = type.BaseType)
 
841
                        {
 
842
                                AddInterfaces(list, type);
 
843
                        }
 
844
                        return list.ToArray();
 
845
                }
 
846
 
 
847
                private static void AddInterfaces(List<Type> list, Type type)
 
848
                {
 
849
                        foreach (Type iface in type.__GetDeclaredInterfaces())
 
850
                        {
 
851
                                if (!list.Contains(iface))
 
852
                                {
 
853
                                        list.Add(iface);
 
854
                                        AddInterfaces(list, iface);
 
855
                                }
 
856
                        }
 
857
                }
 
858
 
 
859
                public MethodInfo[] GetMethods(BindingFlags bindingAttr)
 
860
                {
 
861
                        CheckBaked();
 
862
                        List<MethodInfo> list = new List<MethodInfo>();
 
863
                        foreach (MethodBase mb in __GetDeclaredMethods())
 
864
                        {
 
865
                                MethodInfo mi = mb as MethodInfo;
 
866
                                if (mi != null && mi.BindingFlagsMatch(bindingAttr))
 
867
                                {
 
868
                                        list.Add(mi);
 
869
                                }
 
870
                        }
 
871
                        if ((bindingAttr & BindingFlags.DeclaredOnly) == 0)
 
872
                        {
 
873
                                List<MethodInfo> baseMethods = new List<MethodInfo>();
 
874
                                foreach (MethodInfo mi in list)
 
875
                                {
 
876
                                        if (mi.IsVirtual)
 
877
                                        {
 
878
                                                baseMethods.Add(mi.GetBaseDefinition());
 
879
                                        }
 
880
                                }
 
881
                                for (Type type = this.BaseType; type != null; type = type.BaseType)
 
882
                                {
 
883
                                        type.CheckBaked();
 
884
                                        foreach (MethodBase mb in type.__GetDeclaredMethods())
 
885
                                        {
 
886
                                                MethodInfo mi = mb as MethodInfo;
 
887
                                                if (mi != null && mi.BindingFlagsMatchInherited(bindingAttr))
 
888
                                                {
 
889
                                                        if (mi.IsVirtual)
 
890
                                                        {
 
891
                                                                if (baseMethods == null)
 
892
                                                                {
 
893
                                                                        baseMethods = new List<MethodInfo>();
 
894
                                                                }
 
895
                                                                else if (baseMethods.Contains(mi.GetBaseDefinition()))
 
896
                                                                {
 
897
                                                                        continue;
 
898
                                                                }
 
899
                                                                baseMethods.Add(mi.GetBaseDefinition());
 
900
                                                        }
 
901
                                                        list.Add((MethodInfo)mi.SetReflectedType(this));
 
902
                                                }
 
903
                                        }
 
904
                                }
 
905
                        }
 
906
                        return list.ToArray();
 
907
                }
 
908
 
 
909
                public MethodInfo[] GetMethods()
 
910
                {
 
911
                        return GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
 
912
                }
 
913
 
 
914
                public MethodInfo GetMethod(string name)
 
915
                {
 
916
                        return GetMethod(name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
 
917
                }
 
918
 
 
919
                public MethodInfo GetMethod(string name, BindingFlags bindingAttr)
 
920
                {
 
921
                        return GetMemberByName<MethodInfo>(name, bindingAttr);
 
922
                }
 
923
 
 
924
                public MethodInfo GetMethod(string name, Type[] types)
 
925
                {
 
926
                        return GetMethod(name, types, null);
 
927
                }
 
928
 
 
929
                public MethodInfo GetMethod(string name, Type[] types, ParameterModifier[] modifiers)
 
930
                {
 
931
                        return GetMethod(name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public, null, types, modifiers);
 
932
                }
 
933
 
 
934
                public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
 
935
                {
 
936
                        // first we try an exact match and only if that fails we fall back to using the binder
 
937
                        return GetMemberByName<MethodInfo>(name, bindingAttr,
 
938
                                delegate(MethodInfo method) { return method.MethodSignature.MatchParameterTypes(types); })
 
939
                                ?? GetMethodWithBinder<MethodInfo>(name, bindingAttr, binder ?? DefaultBinder, types, modifiers);
 
940
                }
 
941
 
 
942
                private T GetMethodWithBinder<T>(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
 
943
                        where T : MethodBase
 
944
                {
 
945
                        List<MethodBase> list = new List<MethodBase>();
 
946
                        GetMemberByName<T>(name, bindingAttr, delegate(T method) {
 
947
                                list.Add(method);
 
948
                                return false;
 
949
                        });
 
950
                        return (T)binder.SelectMethod(bindingAttr, list.ToArray(), types, modifiers);
 
951
                }
 
952
 
 
953
                public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
 
954
                {
 
955
                        // FXBUG callConvention seems to be ignored
 
956
                        return GetMethod(name, bindingAttr, binder, types, modifiers);
 
957
                }
 
958
 
 
959
                public ConstructorInfo[] GetConstructors()
 
960
                {
 
961
                        return GetConstructors(BindingFlags.Public | BindingFlags.Instance);
 
962
                }
 
963
 
 
964
                public ConstructorInfo[] GetConstructors(BindingFlags bindingAttr)
 
965
                {
 
966
                        return GetMembers<ConstructorInfo>(bindingAttr | BindingFlags.DeclaredOnly);
 
967
                }
 
968
 
 
969
                public ConstructorInfo GetConstructor(Type[] types)
 
970
                {
 
971
                        return GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, CallingConventions.Standard, types, null);
 
972
                }
 
973
 
 
974
                public ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
 
975
                {
 
976
                        ConstructorInfo ci1 = null;
 
977
                        if ((bindingAttr & BindingFlags.Instance) != 0)
 
978
                        {
 
979
                                ci1 = GetConstructorImpl(ConstructorInfo.ConstructorName, bindingAttr, binder, types, modifiers);
 
980
                        }
 
981
                        if ((bindingAttr & BindingFlags.Static) != 0)
 
982
                        {
 
983
                                ConstructorInfo ci2 = GetConstructorImpl(ConstructorInfo.TypeConstructorName, bindingAttr, binder, types, modifiers);
 
984
                                if (ci2 != null)
 
985
                                {
 
986
                                        if (ci1 != null)
 
987
                                        {
 
988
                                                throw new AmbiguousMatchException();
 
989
                                        }
 
990
                                        return ci2;
 
991
                                }
 
992
                        }
 
993
                        return ci1;
 
994
                }
 
995
 
 
996
                private ConstructorInfo GetConstructorImpl(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
 
997
                {
 
998
                        // first we try an exact match and only if that fails we fall back to using the binder
 
999
                        return GetMemberByName<ConstructorInfo>(name, bindingAttr | BindingFlags.DeclaredOnly,
 
1000
                                delegate(ConstructorInfo ctor) { return ctor.MethodSignature.MatchParameterTypes(types); })
 
1001
                                ?? GetMethodWithBinder<ConstructorInfo>(name, bindingAttr, binder ?? DefaultBinder, types, modifiers);
 
1002
                }
 
1003
 
 
1004
                public ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, CallingConventions callingConvention, Type[] types, ParameterModifier[] modifiers)
 
1005
                {
 
1006
                        // FXBUG callConvention seems to be ignored
 
1007
                        return GetConstructor(bindingAttr, binder, types, modifiers);
 
1008
                }
 
1009
 
 
1010
                internal Type ResolveNestedType(TypeName typeName)
 
1011
                {
 
1012
                        return FindNestedType(typeName) ?? Module.universe.GetMissingTypeOrThrow(Module, this, typeName);
 
1013
                }
 
1014
 
 
1015
                // unlike the public API, this takes the namespace and name into account
 
1016
                internal virtual Type FindNestedType(TypeName name)
 
1017
                {
 
1018
                        foreach (Type type in __GetDeclaredTypes())
 
1019
                        {
 
1020
                                if (type.__Namespace == name.Namespace && type.__Name == name.Name)
 
1021
                                {
 
1022
                                        return type;
 
1023
                                }
 
1024
                        }
 
1025
                        return null;
 
1026
                }
 
1027
 
 
1028
                internal virtual Type FindNestedTypeIgnoreCase(TypeName lowerCaseName)
 
1029
                {
 
1030
                        foreach (Type type in __GetDeclaredTypes())
 
1031
                        {
 
1032
                                if (new TypeName(type.__Namespace, type.__Name).ToLowerInvariant() == lowerCaseName)
 
1033
                                {
 
1034
                                        return type;
 
1035
                                }
 
1036
                        }
 
1037
                        return null;
 
1038
                }
 
1039
 
 
1040
                public Type GetNestedType(string name)
 
1041
                {
 
1042
                        return GetNestedType(name, BindingFlags.Public);
 
1043
                }
 
1044
 
 
1045
                public Type GetNestedType(string name, BindingFlags bindingAttr)
 
1046
                {
 
1047
                        // FXBUG the namespace is ignored, so we can use GetMemberByName
 
1048
                        return GetMemberByName<Type>(name, bindingAttr | BindingFlags.DeclaredOnly);
 
1049
                }
 
1050
 
 
1051
                public Type[] GetNestedTypes()
 
1052
                {
 
1053
                        return GetNestedTypes(BindingFlags.Public);
 
1054
                }
 
1055
 
 
1056
                public Type[] GetNestedTypes(BindingFlags bindingAttr)
 
1057
                {
 
1058
                        // FXBUG the namespace is ignored, so we can use GetMember
 
1059
                        return GetMembers<Type>(bindingAttr | BindingFlags.DeclaredOnly);
 
1060
                }
 
1061
 
 
1062
                public PropertyInfo[] GetProperties()
 
1063
                {
 
1064
                        return GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
 
1065
                }
 
1066
 
 
1067
                public PropertyInfo[] GetProperties(BindingFlags bindingAttr)
 
1068
                {
 
1069
                        return GetMembers<PropertyInfo>(bindingAttr);
 
1070
                }
 
1071
 
 
1072
                public PropertyInfo GetProperty(string name)
 
1073
                {
 
1074
                        return GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
 
1075
                }
 
1076
 
 
1077
                public PropertyInfo GetProperty(string name, BindingFlags bindingAttr)
 
1078
                {
 
1079
                        return GetMemberByName<PropertyInfo>(name, bindingAttr);
 
1080
                }
 
1081
 
 
1082
                public PropertyInfo GetProperty(string name, Type returnType)
 
1083
                {
 
1084
                        const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;
 
1085
                        return GetMemberByName<PropertyInfo>(name, flags, delegate(PropertyInfo prop) { return prop.PropertyType.Equals(returnType); })
 
1086
                                ?? GetPropertyWithBinder(name, flags, DefaultBinder, returnType, null, null);
 
1087
                }
 
1088
 
 
1089
                public PropertyInfo GetProperty(string name, Type[] types)
 
1090
                {
 
1091
                        const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;
 
1092
                        return GetMemberByName<PropertyInfo>(name, flags, delegate(PropertyInfo prop) { return prop.PropertySignature.MatchParameterTypes(types); })
 
1093
                                ?? GetPropertyWithBinder(name, flags, DefaultBinder, null, types, null);
 
1094
                }
 
1095
 
 
1096
                public PropertyInfo GetProperty(string name, Type returnType, Type[] types)
 
1097
                {
 
1098
                        return GetProperty(name, returnType, types, null);
 
1099
                }
 
1100
 
 
1101
                public PropertyInfo GetProperty(string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
 
1102
                {
 
1103
                        return GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static, null, returnType, types, modifiers);
 
1104
                }
 
1105
 
 
1106
                public PropertyInfo GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
 
1107
                {
 
1108
                        return GetMemberByName<PropertyInfo>(name, bindingAttr,
 
1109
                                delegate(PropertyInfo prop) {
 
1110
                                        return prop.PropertyType.Equals(returnType) && prop.PropertySignature.MatchParameterTypes(types);
 
1111
                                })
 
1112
                                ?? GetPropertyWithBinder(name, bindingAttr, binder ?? DefaultBinder, returnType, types, modifiers);
 
1113
                }
 
1114
 
 
1115
                private PropertyInfo GetPropertyWithBinder(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
 
1116
                {
 
1117
                        List<PropertyInfo> list = new List<PropertyInfo>();
 
1118
                        GetMemberByName<PropertyInfo>(name, bindingAttr, delegate(PropertyInfo property) {
 
1119
                                list.Add(property);
 
1120
                                return false;
 
1121
                        });
 
1122
                        return binder.SelectProperty(bindingAttr, list.ToArray(), returnType, types, modifiers);
 
1123
                }
 
1124
 
 
1125
                public Type GetInterface(string name)
 
1126
                {
 
1127
                        return GetInterface(name, false);
 
1128
                }
 
1129
 
 
1130
                public Type GetInterface(string name, bool ignoreCase)
 
1131
                {
 
1132
                        if (ignoreCase)
 
1133
                        {
 
1134
                                name = name.ToLowerInvariant();
 
1135
                        }
 
1136
                        Type found = null;
 
1137
                        foreach (Type type in GetInterfaces())
 
1138
                        {
 
1139
                                string typeName = type.FullName;
 
1140
                                if (ignoreCase)
 
1141
                                {
 
1142
                                        typeName = typeName.ToLowerInvariant();
 
1143
                                }
 
1144
                                if (typeName == name)
 
1145
                                {
 
1146
                                        if (found != null)
 
1147
                                        {
 
1148
                                                throw new AmbiguousMatchException();
 
1149
                                        }
 
1150
                                        found = type;
 
1151
                                }
 
1152
                        }
 
1153
                        return found;
 
1154
                }
 
1155
 
 
1156
                public Type[] FindInterfaces(TypeFilter filter, object filterCriteria)
 
1157
                {
 
1158
                        List<Type> list = new List<Type>();
 
1159
                        foreach (Type type in GetInterfaces())
 
1160
                        {
 
1161
                                if (filter(type, filterCriteria))
 
1162
                                {
 
1163
                                        list.Add(type);
 
1164
                                }
 
1165
                        }
 
1166
                        return list.ToArray();
 
1167
                }
 
1168
 
 
1169
                public ConstructorInfo TypeInitializer
 
1170
                {
 
1171
                        get { return GetConstructor(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null); }
 
1172
                }
 
1173
 
 
1174
                public bool IsPrimitive
 
1175
                {
 
1176
                        get
 
1177
                        {
 
1178
                                Universe u = this.Universe;
 
1179
                                return this == u.System_Boolean
 
1180
                                        || this == u.System_Byte
 
1181
                                        || this == u.System_SByte
 
1182
                                        || this == u.System_Int16
 
1183
                                        || this == u.System_UInt16
 
1184
                                        || this == u.System_Int32
 
1185
                                        || this == u.System_UInt32
 
1186
                                        || this == u.System_Int64
 
1187
                                        || this == u.System_UInt64
 
1188
                                        || this == u.System_IntPtr
 
1189
                                        || this == u.System_UIntPtr
 
1190
                                        || this == u.System_Char
 
1191
                                        || this == u.System_Double
 
1192
                                        || this == u.System_Single
 
1193
                                        ;
 
1194
                        }
 
1195
                }
 
1196
 
 
1197
                public bool IsEnum
 
1198
                {
 
1199
                        get
 
1200
                        {
 
1201
                                Type baseType = this.BaseType;
 
1202
                                return baseType != null
 
1203
                                        && baseType.IsEnumOrValueType
 
1204
                                        && baseType.__Name[0] == 'E';
 
1205
                        }
 
1206
                }
 
1207
 
 
1208
                public bool IsSealed
 
1209
                {
 
1210
                        get { return (Attributes & TypeAttributes.Sealed) != 0; }
 
1211
                }
 
1212
 
 
1213
                public bool IsAbstract
 
1214
                {
 
1215
                        get { return (Attributes & TypeAttributes.Abstract) != 0; }
 
1216
                }
 
1217
 
 
1218
                private bool CheckVisibility(TypeAttributes access)
 
1219
                {
 
1220
                        return (Attributes & TypeAttributes.VisibilityMask) == access;
 
1221
                }
 
1222
 
 
1223
                public bool IsPublic
 
1224
                {
 
1225
                        get { return CheckVisibility(TypeAttributes.Public); }
 
1226
                }
 
1227
 
 
1228
                public bool IsNestedPublic
 
1229
                {
 
1230
                        get { return CheckVisibility(TypeAttributes.NestedPublic); }
 
1231
                }
 
1232
 
 
1233
                public bool IsNestedPrivate
 
1234
                {
 
1235
                        get { return CheckVisibility(TypeAttributes.NestedPrivate); }
 
1236
                }
 
1237
 
 
1238
                public bool IsNestedFamily
 
1239
                {
 
1240
                        get { return CheckVisibility(TypeAttributes.NestedFamily); }
 
1241
                }
 
1242
 
 
1243
                public bool IsNestedAssembly
 
1244
                {
 
1245
                        get { return CheckVisibility(TypeAttributes.NestedAssembly); }
 
1246
                }
 
1247
 
 
1248
                public bool IsNestedFamANDAssem
 
1249
                {
 
1250
                        get { return CheckVisibility(TypeAttributes.NestedFamANDAssem); }
 
1251
                }
 
1252
 
 
1253
                public bool IsNestedFamORAssem
 
1254
                {
 
1255
                        get { return CheckVisibility(TypeAttributes.NestedFamORAssem); }
 
1256
                }
 
1257
 
 
1258
                public bool IsNotPublic
 
1259
                {
 
1260
                        get { return CheckVisibility(TypeAttributes.NotPublic); }
 
1261
                }
 
1262
 
 
1263
                public bool IsImport
 
1264
                {
 
1265
                        get { return (Attributes & TypeAttributes.Import) != 0; }
 
1266
                }
 
1267
 
 
1268
                public bool IsCOMObject
 
1269
                {
 
1270
                        get { return IsClass && IsImport; }
 
1271
                }
 
1272
 
 
1273
                public bool IsContextful
 
1274
                {
 
1275
                        get { return IsSubclassOf(this.Module.universe.Import(typeof(ContextBoundObject))); }
 
1276
                }
 
1277
 
 
1278
                public bool IsMarshalByRef
 
1279
                {
 
1280
                        get { return IsSubclassOf(this.Module.universe.Import(typeof(MarshalByRefObject))); }
 
1281
                }
 
1282
 
 
1283
                public virtual bool IsVisible
 
1284
                {
 
1285
                        get { return IsPublic || (IsNestedPublic && this.DeclaringType.IsVisible); }
 
1286
                }
 
1287
 
 
1288
                public bool IsAnsiClass
 
1289
                {
 
1290
                        get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AnsiClass; }
 
1291
                }
 
1292
 
 
1293
                public bool IsUnicodeClass
 
1294
                {
 
1295
                        get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass; }
 
1296
                }
 
1297
 
 
1298
                public bool IsAutoClass
 
1299
                {
 
1300
                        get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass; }
 
1301
                }
 
1302
 
 
1303
                public bool IsAutoLayout
 
1304
                {
 
1305
                        get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout; }
 
1306
                }
 
1307
 
 
1308
                public bool IsLayoutSequential
 
1309
                {
 
1310
                        get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout; }
 
1311
                }
 
1312
 
 
1313
                public bool IsExplicitLayout
 
1314
                {
 
1315
                        get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout; }
 
1316
                }
 
1317
 
 
1318
                public bool IsSpecialName
 
1319
                {
 
1320
                        get { return (Attributes & TypeAttributes.SpecialName) != 0; }
 
1321
                }
 
1322
 
 
1323
                public bool IsSerializable
 
1324
                {
 
1325
                        get { return (Attributes & TypeAttributes.Serializable) != 0; }
 
1326
                }
 
1327
 
 
1328
                public bool IsClass
 
1329
                {
 
1330
                        get { return !IsInterface && !IsValueType; }
 
1331
                }
 
1332
 
 
1333
                public bool IsInterface
 
1334
                {
 
1335
                        get { return (Attributes & TypeAttributes.Interface) != 0; }
 
1336
                }
 
1337
 
 
1338
                public bool IsNested
 
1339
                {
 
1340
                        // FXBUG we check the declaring type (like .NET) and this results
 
1341
                        // in IsNested returning true for a generic type parameter
 
1342
                        get { return this.DeclaringType != null; }
 
1343
                }
 
1344
 
 
1345
                public virtual bool __ContainsMissingType
 
1346
                {
 
1347
                        get
 
1348
                        {
 
1349
                                if (this.__IsMissing)
 
1350
                                {
 
1351
                                        return true;
 
1352
                                }
 
1353
                                foreach (Type arg in this.GetGenericArguments())
 
1354
                                {
 
1355
                                        if (arg.__ContainsMissingType)
 
1356
                                        {
 
1357
                                                return true;
 
1358
                                        }
 
1359
                                }
 
1360
                                return false;
 
1361
                        }
 
1362
                }
 
1363
 
 
1364
                public Type MakeArrayType()
 
1365
                {
 
1366
                        return ArrayType.Make(this, new CustomModifiers());
 
1367
                }
 
1368
 
 
1369
                public Type __MakeArrayType(CustomModifiers customModifiers)
 
1370
                {
 
1371
                        return ArrayType.Make(this, customModifiers);
 
1372
                }
 
1373
 
 
1374
                [Obsolete("Please use __MakeArrayType(CustomModifiers) instead.")]
 
1375
                public Type __MakeArrayType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
 
1376
                {
 
1377
                        return __MakeArrayType(CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
 
1378
                }
 
1379
 
 
1380
                public Type MakeArrayType(int rank)
 
1381
                {
 
1382
                        return __MakeArrayType(rank, new CustomModifiers());
 
1383
                }
 
1384
 
 
1385
                public Type __MakeArrayType(int rank, CustomModifiers customModifiers)
 
1386
                {
 
1387
                        return MultiArrayType.Make(this, rank, Empty<int>.Array, new int[rank], customModifiers);
 
1388
                }
 
1389
 
 
1390
                [Obsolete("Please use __MakeArrayType(int, CustomModifiers) instead.")]
 
1391
                public Type __MakeArrayType(int rank, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
 
1392
                {
 
1393
                        return __MakeArrayType(rank, CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
 
1394
                }
 
1395
 
 
1396
                public Type __MakeArrayType(int rank, int[] sizes, int[] lobounds, CustomModifiers customModifiers)
 
1397
                {
 
1398
                        return MultiArrayType.Make(this, rank, sizes ?? Empty<int>.Array, lobounds ?? Empty<int>.Array, customModifiers);
 
1399
                }
 
1400
 
 
1401
                [Obsolete("Please use __MakeArrayType(int, int[], int[], CustomModifiers) instead.")]
 
1402
                public Type __MakeArrayType(int rank, int[] sizes, int[] lobounds, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
 
1403
                {
 
1404
                        return __MakeArrayType(rank, sizes, lobounds, CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
 
1405
                }
 
1406
 
 
1407
                public Type MakeByRefType()
 
1408
                {
 
1409
                        return ByRefType.Make(this, new CustomModifiers());
 
1410
                }
 
1411
 
 
1412
                public Type __MakeByRefType(CustomModifiers customModifiers)
 
1413
                {
 
1414
                        return ByRefType.Make(this, customModifiers);
 
1415
                }
 
1416
 
 
1417
                [Obsolete("Please use __MakeByRefType(CustomModifiers) instead.")]
 
1418
                public Type __MakeByRefType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
 
1419
                {
 
1420
                        return __MakeByRefType(CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
 
1421
                }
 
1422
 
 
1423
                public Type MakePointerType()
 
1424
                {
 
1425
                        return PointerType.Make(this, new CustomModifiers());
 
1426
                }
 
1427
 
 
1428
                public Type __MakePointerType(CustomModifiers customModifiers)
 
1429
                {
 
1430
                        return PointerType.Make(this, customModifiers);
 
1431
                }
 
1432
 
 
1433
                [Obsolete("Please use __MakeByRefType(CustomModifiers) instead.")]
 
1434
                public Type __MakePointerType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
 
1435
                {
 
1436
                        return __MakePointerType(CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
 
1437
                }
 
1438
 
 
1439
                public Type MakeGenericType(params Type[] typeArguments)
 
1440
                {
 
1441
                        return __MakeGenericType(typeArguments, null);
 
1442
                }
 
1443
 
 
1444
                public Type __MakeGenericType(Type[] typeArguments, CustomModifiers[] customModifiers)
 
1445
                {
 
1446
                        if (!this.__IsMissing && !this.IsGenericTypeDefinition)
 
1447
                        {
 
1448
                                throw new InvalidOperationException();
 
1449
                        }
 
1450
                        return GenericTypeInstance.Make(this, Util.Copy(typeArguments), customModifiers == null ? null : (CustomModifiers[])customModifiers.Clone());
 
1451
                }
 
1452
 
 
1453
                [Obsolete("Please use __MakeGenericType(Type[], CustomModifiers[]) instead.")]
 
1454
                public Type __MakeGenericType(Type[] typeArguments, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
 
1455
                {
 
1456
                        if (!this.__IsMissing && !this.IsGenericTypeDefinition)
 
1457
                        {
 
1458
                                throw new InvalidOperationException();
 
1459
                        }
 
1460
                        CustomModifiers[] mods = null;
 
1461
                        if (requiredCustomModifiers != null || optionalCustomModifiers != null)
 
1462
                        {
 
1463
                                mods = new CustomModifiers[typeArguments.Length];
 
1464
                                for (int i = 0; i < mods.Length; i++)
 
1465
                                {
 
1466
                                        mods[i] = CustomModifiers.FromReqOpt(Util.NullSafeElementAt(requiredCustomModifiers, i), Util.NullSafeElementAt(optionalCustomModifiers, i));
 
1467
                                }
 
1468
                        }
 
1469
                        return GenericTypeInstance.Make(this, Util.Copy(typeArguments), mods);
 
1470
                }
 
1471
 
 
1472
                public static System.Type __GetSystemType(TypeCode typeCode)
 
1473
                {
 
1474
                        switch (typeCode)
 
1475
                        {
 
1476
                                case TypeCode.Boolean:
 
1477
                                        return typeof(System.Boolean);
 
1478
                                case TypeCode.Byte:
 
1479
                                        return typeof(System.Byte);
 
1480
                                case TypeCode.Char:
 
1481
                                        return typeof(System.Char);
 
1482
                                case TypeCode.DBNull:
 
1483
                                        return typeof(System.DBNull);
 
1484
                                case TypeCode.DateTime:
 
1485
                                        return typeof(System.DateTime);
 
1486
                                case TypeCode.Decimal:
 
1487
                                        return typeof(System.Decimal);
 
1488
                                case TypeCode.Double:
 
1489
                                        return typeof(System.Double);
 
1490
                                case TypeCode.Empty:
 
1491
                                        return null;
 
1492
                                case TypeCode.Int16:
 
1493
                                        return typeof(System.Int16);
 
1494
                                case TypeCode.Int32:
 
1495
                                        return typeof(System.Int32);
 
1496
                                case TypeCode.Int64:
 
1497
                                        return typeof(System.Int64);
 
1498
                                case TypeCode.Object:
 
1499
                                        return typeof(System.Object);
 
1500
                                case TypeCode.SByte:
 
1501
                                        return typeof(System.SByte);
 
1502
                                case TypeCode.Single:
 
1503
                                        return typeof(System.Single);
 
1504
                                case TypeCode.String:
 
1505
                                        return typeof(System.String);
 
1506
                                case TypeCode.UInt16:
 
1507
                                        return typeof(System.UInt16);
 
1508
                                case TypeCode.UInt32:
 
1509
                                        return typeof(System.UInt32);
 
1510
                                case TypeCode.UInt64:
 
1511
                                        return typeof(System.UInt64);
 
1512
                                default:
 
1513
                                        throw new ArgumentOutOfRangeException();
 
1514
                        }
 
1515
                }
 
1516
 
 
1517
                public static TypeCode GetTypeCode(Type type)
 
1518
                {
 
1519
                        if (type == null)
 
1520
                        {
 
1521
                                return TypeCode.Empty;
 
1522
                        }
 
1523
                        if (!type.__IsMissing && type.IsEnum)
 
1524
                        {
 
1525
                                type = type.GetEnumUnderlyingType();
 
1526
                        }
 
1527
                        Universe u = type.Module.universe;
 
1528
                        if (type == u.System_Boolean)
 
1529
                        {
 
1530
                                return TypeCode.Boolean;
 
1531
                        }
 
1532
                        else if (type == u.System_Char)
 
1533
                        {
 
1534
                                return TypeCode.Char;
 
1535
                        }
 
1536
                        else if (type == u.System_SByte)
 
1537
                        {
 
1538
                                return TypeCode.SByte;
 
1539
                        }
 
1540
                        else if (type == u.System_Byte)
 
1541
                        {
 
1542
                                return TypeCode.Byte;
 
1543
                        }
 
1544
                        else if (type == u.System_Int16)
 
1545
                        {
 
1546
                                return TypeCode.Int16;
 
1547
                        }
 
1548
                        else if (type == u.System_UInt16)
 
1549
                        {
 
1550
                                return TypeCode.UInt16;
 
1551
                        }
 
1552
                        else if (type == u.System_Int32)
 
1553
                        {
 
1554
                                return TypeCode.Int32;
 
1555
                        }
 
1556
                        else if (type == u.System_UInt32)
 
1557
                        {
 
1558
                                return TypeCode.UInt32;
 
1559
                        }
 
1560
                        else if (type == u.System_Int64)
 
1561
                        {
 
1562
                                return TypeCode.Int64;
 
1563
                        }
 
1564
                        else if (type == u.System_UInt64)
 
1565
                        {
 
1566
                                return TypeCode.UInt64;
 
1567
                        }
 
1568
                        else if (type == u.System_Single)
 
1569
                        {
 
1570
                                return TypeCode.Single;
 
1571
                        }
 
1572
                        else if (type == u.System_Double)
 
1573
                        {
 
1574
                                return TypeCode.Double;
 
1575
                        }
 
1576
                        else if (type == u.System_DateTime)
 
1577
                        {
 
1578
                                return TypeCode.DateTime;
 
1579
                        }
 
1580
                        else if (type == u.System_DBNull)
 
1581
                        {
 
1582
                                return TypeCode.DBNull;
 
1583
                        }
 
1584
                        else if (type == u.System_Decimal)
 
1585
                        {
 
1586
                                return TypeCode.Decimal;
 
1587
                        }
 
1588
                        else if (type == u.System_String)
 
1589
                        {
 
1590
                                return TypeCode.String;
 
1591
                        }
 
1592
                        else if (type.__IsMissing)
 
1593
                        {
 
1594
                                throw new MissingMemberException(type);
 
1595
                        }
 
1596
                        else
 
1597
                        {
 
1598
                                return TypeCode.Object;
 
1599
                        }
 
1600
                }
 
1601
 
 
1602
                public Assembly Assembly
 
1603
                {
 
1604
                        get { return Module.Assembly; }
 
1605
                }
 
1606
 
 
1607
                public bool IsAssignableFrom(Type type)
 
1608
                {
 
1609
                        if (this.Equals(type))
 
1610
                        {
 
1611
                                return true;
 
1612
                        }
 
1613
                        else if (type == null)
 
1614
                        {
 
1615
                                return false;
 
1616
                        }
 
1617
                        else if (this.IsArray && type.IsArray)
 
1618
                        {
 
1619
                                if (this.GetArrayRank() != type.GetArrayRank())
 
1620
                                {
 
1621
                                        return false;
 
1622
                                }
 
1623
                                else if (this.__IsVector && !type.__IsVector)
 
1624
                                {
 
1625
                                        return false;
 
1626
                                }
 
1627
                                Type e1 = this.GetElementType();
 
1628
                                Type e2 = type.GetElementType();
 
1629
                                return e1.IsValueType == e2.IsValueType && e1.IsAssignableFrom(e2);
 
1630
                        }
 
1631
                        else if (this.IsCovariant(type))
 
1632
                        {
 
1633
                                return true;
 
1634
                        }
 
1635
                        else if (this.IsSealed)
 
1636
                        {
 
1637
                                return false;
 
1638
                        }
 
1639
                        else if (this.IsInterface)
 
1640
                        {
 
1641
                                foreach (Type iface in type.GetInterfaces())
 
1642
                                {
 
1643
                                        if (this.Equals(iface) || this.IsCovariant(iface))
 
1644
                                        {
 
1645
                                                return true;
 
1646
                                        }
 
1647
                                }
 
1648
                                return false;
 
1649
                        }
 
1650
                        else if (type.IsInterface)
 
1651
                        {
 
1652
                                return this == this.Module.universe.System_Object;
 
1653
                        }
 
1654
                        else if (type.IsPointer)
 
1655
                        {
 
1656
                                return this == this.Module.universe.System_Object || this == this.Module.universe.System_ValueType;
 
1657
                        }
 
1658
                        else
 
1659
                        {
 
1660
                                return type.IsSubclassOf(this);
 
1661
                        }
 
1662
                }
 
1663
 
 
1664
                private bool IsCovariant(Type other)
 
1665
                {
 
1666
                        if (this.IsConstructedGenericType
 
1667
                                && other.IsConstructedGenericType
 
1668
                                && this.GetGenericTypeDefinition() == other.GetGenericTypeDefinition())
 
1669
                        {
 
1670
                                Type[] typeParameters = GetGenericTypeDefinition().GetGenericArguments();
 
1671
                                for (int i = 0; i < typeParameters.Length; i++)
 
1672
                                {
 
1673
                                        Type t1 = this.GetGenericTypeArgument(i);
 
1674
                                        Type t2 = other.GetGenericTypeArgument(i);
 
1675
                                        if (t1.IsValueType != t2.IsValueType)
 
1676
                                        {
 
1677
                                                return false;
 
1678
                                        }
 
1679
                                        switch (typeParameters[i].GenericParameterAttributes & GenericParameterAttributes.VarianceMask)
 
1680
                                        {
 
1681
                                                case GenericParameterAttributes.Covariant:
 
1682
                                                        if (!t1.IsAssignableFrom(t2))
 
1683
                                                        {
 
1684
                                                                return false;
 
1685
                                                        }
 
1686
                                                        break;
 
1687
                                                case GenericParameterAttributes.Contravariant:
 
1688
                                                        if (!t2.IsAssignableFrom(t1))
 
1689
                                                        {
 
1690
                                                                return false;
 
1691
                                                        }
 
1692
                                                        break;
 
1693
                                                case GenericParameterAttributes.None:
 
1694
                                                        if (t1 != t2)
 
1695
                                                        {
 
1696
                                                                return false;
 
1697
                                                        }
 
1698
                                                        break;
 
1699
                                        }
 
1700
                                }
 
1701
                                return true;
 
1702
                        }
 
1703
                        return false;
 
1704
                }
 
1705
 
 
1706
                public bool IsSubclassOf(Type type)
 
1707
                {
 
1708
                        Type thisType = this.BaseType;
 
1709
                        while (thisType != null)
 
1710
                        {
 
1711
                                if (thisType.Equals(type))
 
1712
                                {
 
1713
                                        return true;
 
1714
                                }
 
1715
                                thisType = thisType.BaseType;
 
1716
                        }
 
1717
                        return false;
 
1718
                }
 
1719
 
 
1720
                // This returns true if this type directly (i.e. not inherited from the base class) implements the interface.
 
1721
                // Note that a complicating factor is that the interface itself can be implemented by an interface that extends it.
 
1722
                private bool IsDirectlyImplementedInterface(Type interfaceType)
 
1723
                {
 
1724
                        foreach (Type iface in __GetDeclaredInterfaces())
 
1725
                        {
 
1726
                                if (interfaceType.IsAssignableFrom(iface))
 
1727
                                {
 
1728
                                        return true;
 
1729
                                }
 
1730
                        }
 
1731
                        return false;
 
1732
                }
 
1733
 
 
1734
                public InterfaceMapping GetInterfaceMap(Type interfaceType)
 
1735
                {
 
1736
                        CheckBaked();
 
1737
                        InterfaceMapping map = new InterfaceMapping();
 
1738
                        if (!IsDirectlyImplementedInterface(interfaceType))
 
1739
                        {
 
1740
                                Type baseType = this.BaseType;
 
1741
                                if (baseType == null)
 
1742
                                {
 
1743
                                        throw new ArgumentException();
 
1744
                                }
 
1745
                                else
 
1746
                                {
 
1747
                                        map = baseType.GetInterfaceMap(interfaceType);
 
1748
                                }
 
1749
                        }
 
1750
                        else
 
1751
                        {
 
1752
                                map.InterfaceMethods = interfaceType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
 
1753
                                map.InterfaceType = interfaceType;
 
1754
                                map.TargetMethods = new MethodInfo[map.InterfaceMethods.Length];
 
1755
                                FillInExplicitInterfaceMethods(map.InterfaceMethods, map.TargetMethods);
 
1756
                                MethodInfo[] methods = GetMethods(BindingFlags.Instance | BindingFlags.Public);
 
1757
                                for (int i = 0; i < map.TargetMethods.Length; i++)
 
1758
                                {
 
1759
                                        if (map.TargetMethods[i] == null)
 
1760
                                        {
 
1761
                                                // TODO use proper method resolution (also take into account that no implicit base class implementation is used across assembly boundaries)
 
1762
                                                for (int j = 0; j < methods.Length; j++)
 
1763
                                                {
 
1764
                                                        if (methods[j].Name == map.InterfaceMethods[i].Name
 
1765
                                                                && methods[j].MethodSignature.Equals(map.InterfaceMethods[i].MethodSignature))
 
1766
                                                        {
 
1767
                                                                map.TargetMethods[i] = methods[j];
 
1768
                                                        }
 
1769
                                                }
 
1770
                                        }
 
1771
                                }
 
1772
                                for (Type baseType = this.BaseType; baseType != null && interfaceType.IsAssignableFrom(baseType); baseType = baseType.BaseType)
 
1773
                                {
 
1774
                                        baseType.FillInExplicitInterfaceMethods(map.InterfaceMethods, map.TargetMethods);
 
1775
                                }
 
1776
                        }
 
1777
                        map.TargetType = this;
 
1778
                        return map;
 
1779
                }
 
1780
 
 
1781
                internal void FillInExplicitInterfaceMethods(MethodInfo[] interfaceMethods, MethodInfo[] targetMethods)
 
1782
                {
 
1783
                        __MethodImplMap impl = __GetMethodImplMap();
 
1784
                        for (int i = 0; i < impl.MethodDeclarations.Length; i++)
 
1785
                        {
 
1786
                                for (int j = 0; j < impl.MethodDeclarations[i].Length; j++)
 
1787
                                {
 
1788
                                        int index = Array.IndexOf(interfaceMethods, impl.MethodDeclarations[i][j]);
 
1789
                                        if (index != -1 && targetMethods[index] == null)
 
1790
                                        {
 
1791
                                                targetMethods[index] = impl.MethodBodies[i];
 
1792
                                        }
 
1793
                                }
 
1794
                        }
 
1795
                }
 
1796
 
 
1797
                Type IGenericContext.GetGenericTypeArgument(int index)
 
1798
                {
 
1799
                        return GetGenericTypeArgument(index);
 
1800
                }
 
1801
 
 
1802
                Type IGenericContext.GetGenericMethodArgument(int index)
 
1803
                {
 
1804
                        throw new BadImageFormatException();
 
1805
                }
 
1806
 
 
1807
                Type IGenericBinder.BindTypeParameter(Type type)
 
1808
                {
 
1809
                        return GetGenericTypeArgument(type.GenericParameterPosition);
 
1810
                }
 
1811
 
 
1812
                Type IGenericBinder.BindMethodParameter(Type type)
 
1813
                {
 
1814
                        throw new BadImageFormatException();
 
1815
                }
 
1816
 
 
1817
                internal virtual Type BindTypeParameters(IGenericBinder binder)
 
1818
                {
 
1819
                        if (IsGenericTypeDefinition)
 
1820
                        {
 
1821
                                Type[] args = GetGenericArguments();
 
1822
                                Type.InplaceBindTypeParameters(binder, args);
 
1823
                                return GenericTypeInstance.Make(this, args, null);
 
1824
                        }
 
1825
                        else
 
1826
                        {
 
1827
                                return this;
 
1828
                        }
 
1829
                }
 
1830
 
 
1831
                private static void InplaceBindTypeParameters(IGenericBinder binder, Type[] types)
 
1832
                {
 
1833
                        for (int i = 0; i < types.Length; i++)
 
1834
                        {
 
1835
                                types[i] = types[i].BindTypeParameters(binder);
 
1836
                        }
 
1837
                }
 
1838
 
 
1839
                internal virtual MethodBase FindMethod(string name, MethodSignature signature)
 
1840
                {
 
1841
                        foreach (MethodBase method in __GetDeclaredMethods())
 
1842
                        {
 
1843
                                if (method.Name == name && method.MethodSignature.Equals(signature))
 
1844
                                {
 
1845
                                        return method;
 
1846
                                }
 
1847
                        }
 
1848
                        return null;
 
1849
                }
 
1850
 
 
1851
                internal virtual FieldInfo FindField(string name, FieldSignature signature)
 
1852
                {
 
1853
                        foreach (FieldInfo field in __GetDeclaredFields())
 
1854
                        {
 
1855
                                if (field.Name == name && field.FieldSignature.Equals(signature))
 
1856
                                {
 
1857
                                        return field;
 
1858
                                }
 
1859
                        }
 
1860
                        return null;
 
1861
                }
 
1862
 
 
1863
                internal bool IsAllowMultipleCustomAttribute
 
1864
                {
 
1865
                        get
 
1866
                        {
 
1867
                                IList<CustomAttributeData> cad = CustomAttributeData.__GetCustomAttributes(this, this.Module.universe.System_AttributeUsageAttribute, false);
 
1868
                                if (cad.Count == 1)
 
1869
                                {
 
1870
                                        foreach (CustomAttributeNamedArgument arg in cad[0].NamedArguments)
 
1871
                                        {
 
1872
                                                if (arg.MemberInfo.Name == "AllowMultiple")
 
1873
                                                {
 
1874
                                                        return (bool)arg.TypedValue.Value;
 
1875
                                                }
 
1876
                                        }
 
1877
                                }
 
1878
                                return false;
 
1879
                        }
 
1880
                }
 
1881
 
 
1882
                internal bool IsPseudoCustomAttribute
 
1883
                {
 
1884
                        get
 
1885
                        {
 
1886
                                Universe u = this.Module.universe;
 
1887
                                return this == u.System_NonSerializedAttribute
 
1888
                                        || this == u.System_SerializableAttribute
 
1889
                                        || this == u.System_Runtime_InteropServices_DllImportAttribute
 
1890
                                        || this == u.System_Runtime_InteropServices_FieldOffsetAttribute
 
1891
                                        || this == u.System_Runtime_InteropServices_InAttribute
 
1892
                                        || this == u.System_Runtime_InteropServices_MarshalAsAttribute
 
1893
                                        || this == u.System_Runtime_InteropServices_OutAttribute
 
1894
                                        || this == u.System_Runtime_InteropServices_StructLayoutAttribute
 
1895
                                        || this == u.System_Runtime_InteropServices_OptionalAttribute
 
1896
                                        || this == u.System_Runtime_InteropServices_PreserveSigAttribute
 
1897
                                        || this == u.System_Runtime_InteropServices_ComImportAttribute
 
1898
                                        || this == u.System_Runtime_CompilerServices_SpecialNameAttribute
 
1899
                                        || this == u.System_Runtime_CompilerServices_MethodImplAttribute
 
1900
                                        ;
 
1901
                        }
 
1902
                }
 
1903
 
 
1904
                internal Type MarkNotValueType()
 
1905
                {
 
1906
                        typeFlags |= TypeFlags.NotValueType;
 
1907
                        return this;
 
1908
                }
 
1909
 
 
1910
                internal Type MarkValueType()
 
1911
                {
 
1912
                        typeFlags |= TypeFlags.ValueType;
 
1913
                        return this;
 
1914
                }
 
1915
 
 
1916
                internal ConstructorInfo GetPseudoCustomAttributeConstructor(params Type[] parameterTypes)
 
1917
                {
 
1918
                        Universe u = this.Module.universe;
 
1919
                        MethodSignature methodSig = MethodSignature.MakeFromBuilder(u.System_Void, parameterTypes, new PackedCustomModifiers(), CallingConventions.Standard | CallingConventions.HasThis, 0);
 
1920
                        MethodBase mb =
 
1921
                                FindMethod(".ctor", methodSig) ??
 
1922
                                u.GetMissingMethodOrThrow(this, ".ctor", methodSig);
 
1923
                        return (ConstructorInfo)mb;
 
1924
                }
 
1925
 
 
1926
                public MethodBase __CreateMissingMethod(string name, CallingConventions callingConvention, Type returnType, CustomModifiers returnTypeCustomModifiers, Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers)
 
1927
                {
 
1928
                        return CreateMissingMethod(name, callingConvention, returnType, parameterTypes, PackedCustomModifiers.CreateFromExternal(returnTypeCustomModifiers, parameterTypeCustomModifiers, parameterTypes.Length));
 
1929
                }
 
1930
 
 
1931
                private MethodBase CreateMissingMethod(string name, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, PackedCustomModifiers customModifiers)
 
1932
                {
 
1933
                        MethodSignature sig = new MethodSignature(
 
1934
                                returnType ?? this.Module.universe.System_Void,
 
1935
                                Util.Copy(parameterTypes),
 
1936
                                customModifiers,
 
1937
                                callingConvention,
 
1938
                                0);
 
1939
                        MethodInfo method = new MissingMethod(this, name, sig);
 
1940
                        if (name == ".ctor" || name == ".cctor")
 
1941
                        {
 
1942
                                return new ConstructorInfoImpl(method);
 
1943
                        }
 
1944
                        return method;
 
1945
                }
 
1946
 
 
1947
                [Obsolete("Please use __CreateMissingMethod(string, CallingConventions, Type, CustomModifiers, Type[], CustomModifiers[]) instead")]
 
1948
                public MethodBase __CreateMissingMethod(string name, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
 
1949
                {
 
1950
                        return CreateMissingMethod(name, callingConvention, returnType, parameterTypes, PackedCustomModifiers.CreateFromExternal(returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers, parameterTypeRequiredCustomModifiers, parameterTypes.Length));
 
1951
                }
 
1952
 
 
1953
                public FieldInfo __CreateMissingField(string name, Type fieldType, CustomModifiers customModifiers)
 
1954
                {
 
1955
                        return new MissingField(this, name, FieldSignature.Create(fieldType, customModifiers));
 
1956
                }
 
1957
 
 
1958
                [Obsolete("Please use __CreateMissingField(string, Type, CustomModifiers) instead")]
 
1959
                public FieldInfo __CreateMissingField(string name, Type fieldType, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
 
1960
                {
 
1961
                        return __CreateMissingField(name, fieldType, CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
 
1962
                }
 
1963
 
 
1964
                public PropertyInfo __CreateMissingProperty(string name, CallingConventions callingConvention, Type propertyType, CustomModifiers propertyTypeCustomModifiers, Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers)
 
1965
                {
 
1966
                        PropertySignature sig = PropertySignature.Create(callingConvention,
 
1967
                                propertyType,
 
1968
                                parameterTypes,
 
1969
                                PackedCustomModifiers.CreateFromExternal(propertyTypeCustomModifiers, parameterTypeCustomModifiers, Util.NullSafeLength(parameterTypes)));
 
1970
                        return new MissingProperty(this, name, sig);
 
1971
                }
 
1972
 
 
1973
                internal virtual Type SetMetadataTokenForMissing(int token, int flags)
 
1974
                {
 
1975
                        return this;
 
1976
                }
 
1977
 
 
1978
                protected void MarkEnumOrValueType(string typeNamespace, string typeName)
 
1979
                {
 
1980
                        // we assume that mscorlib won't have nested types with these names,
 
1981
                        // so we don't check that we're not a nested type
 
1982
                        if (typeNamespace == "System"
 
1983
                                && (typeName == "Enum" || typeName == "ValueType"))
 
1984
                        {
 
1985
                                typeFlags |= TypeFlags.PotentialEnumOrValueType;
 
1986
                        }
 
1987
                }
 
1988
 
 
1989
                private bool ResolvePotentialEnumOrValueType()
 
1990
                {
 
1991
                        if (this.Assembly == this.Universe.Mscorlib
 
1992
                                || this.Assembly.GetName().Name.Equals("mscorlib", StringComparison.OrdinalIgnoreCase)
 
1993
                                // check if mscorlib forwards the type (.NETCore profile reference mscorlib forwards System.Enum and System.ValueType to System.Runtime.dll)
 
1994
                                || this.Universe.Mscorlib.FindType(new TypeName(__Namespace, __Name)) == this)
 
1995
                        {
 
1996
                                typeFlags = (typeFlags & ~TypeFlags.PotentialEnumOrValueType) | TypeFlags.EnumOrValueType;
 
1997
                                return true;
 
1998
                        }
 
1999
                        else
 
2000
                        {
 
2001
                                typeFlags &= ~TypeFlags.PotentialEnumOrValueType;
 
2002
                                return false;
 
2003
                        }
 
2004
                }
 
2005
 
 
2006
                private bool IsEnumOrValueType
 
2007
                {
 
2008
                        get
 
2009
                        {
 
2010
                                return (typeFlags & (TypeFlags.EnumOrValueType | TypeFlags.PotentialEnumOrValueType)) != 0
 
2011
                                        && ((typeFlags & TypeFlags.EnumOrValueType) != 0 || ResolvePotentialEnumOrValueType());
 
2012
                        }
 
2013
                }
 
2014
 
 
2015
                internal virtual Universe Universe
 
2016
                {
 
2017
                        get { return Module.universe; }
 
2018
                }
 
2019
 
 
2020
                internal sealed override bool BindingFlagsMatch(BindingFlags flags)
 
2021
                {
 
2022
                        return BindingFlagsMatch(IsNestedPublic, flags, BindingFlags.Public, BindingFlags.NonPublic);
 
2023
                }
 
2024
 
 
2025
                internal sealed override MemberInfo SetReflectedType(Type type)
 
2026
                {
 
2027
                        throw new InvalidOperationException();
 
2028
                }
 
2029
 
 
2030
                internal override int GetCurrentToken()
 
2031
                {
 
2032
                        return this.MetadataToken;
 
2033
                }
 
2034
 
 
2035
                internal sealed override List<CustomAttributeData> GetPseudoCustomAttributes(Type attributeType)
 
2036
                {
 
2037
                        // types don't have pseudo custom attributes
 
2038
                        return null;
 
2039
                }
 
2040
 
 
2041
                // in .NET this is an extension method, but we target .NET 2.0, so we have an instance method
 
2042
                public TypeInfo GetTypeInfo()
 
2043
                {
 
2044
                        TypeInfo type = this as TypeInfo;
 
2045
                        if (type == null)
 
2046
                        {
 
2047
                                throw new MissingMemberException(this);
 
2048
                        }
 
2049
                        return type;
 
2050
                }
 
2051
 
 
2052
                public virtual bool __IsTypeForwarder
 
2053
                {
 
2054
                        get { return false; }
 
2055
                }
 
2056
        }
 
2057
 
 
2058
        abstract class ElementHolderType : TypeInfo
 
2059
        {
 
2060
                protected readonly Type elementType;
 
2061
                private int token;
 
2062
                private readonly CustomModifiers mods;
 
2063
 
 
2064
                protected ElementHolderType(Type elementType, CustomModifiers mods)
 
2065
                {
 
2066
                        this.elementType = elementType;
 
2067
                        this.mods = mods;
 
2068
                }
 
2069
 
 
2070
                protected bool EqualsHelper(ElementHolderType other)
 
2071
                {
 
2072
                        return other != null
 
2073
                                && other.elementType.Equals(elementType)
 
2074
                                && other.mods.Equals(mods);
 
2075
                }
 
2076
 
 
2077
                public override CustomModifiers __GetCustomModifiers()
 
2078
                {
 
2079
                        return mods;
 
2080
                }
 
2081
 
 
2082
                public sealed override string Name
 
2083
                {
 
2084
                        get { return elementType.Name + GetSuffix(); }
 
2085
                }
 
2086
 
 
2087
                public sealed override string Namespace
 
2088
                {
 
2089
                        get { return elementType.Namespace; }
 
2090
                }
 
2091
 
 
2092
                public sealed override string FullName
 
2093
                {
 
2094
                        get { return elementType.FullName + GetSuffix(); }
 
2095
                }
 
2096
 
 
2097
                public sealed override string ToString()
 
2098
                {
 
2099
                        return elementType.ToString() + GetSuffix();
 
2100
                }
 
2101
 
 
2102
                public sealed override Type GetElementType()
 
2103
                {
 
2104
                        return elementType;
 
2105
                }
 
2106
 
 
2107
                public sealed override bool HasElementType
 
2108
                {
 
2109
                        get { return true; }
 
2110
                }
 
2111
 
 
2112
                public sealed override Module Module
 
2113
                {
 
2114
                        get { return elementType.Module; }
 
2115
                }
 
2116
 
 
2117
                internal sealed override int GetModuleBuilderToken()
 
2118
                {
 
2119
                        if (token == 0)
 
2120
                        {
 
2121
                                token = ((ModuleBuilder)elementType.Module).ImportType(this);
 
2122
                        }
 
2123
                        return token;
 
2124
                }
 
2125
 
 
2126
                public sealed override bool ContainsGenericParameters
 
2127
                {
 
2128
                        get
 
2129
                        {
 
2130
                                Type type = elementType;
 
2131
                                while (type.HasElementType)
 
2132
                                {
 
2133
                                        type = type.GetElementType();
 
2134
                                }
 
2135
                                return type.ContainsGenericParameters;
 
2136
                        }
 
2137
                }
 
2138
 
 
2139
                public sealed override bool __ContainsMissingType
 
2140
                {
 
2141
                        get
 
2142
                        {
 
2143
                                Type type = elementType;
 
2144
                                while (type.HasElementType)
 
2145
                                {
 
2146
                                        type = type.GetElementType();
 
2147
                                }
 
2148
                                return type.__ContainsMissingType;
 
2149
                        }
 
2150
                }
 
2151
 
 
2152
                internal sealed override Type BindTypeParameters(IGenericBinder binder)
 
2153
                {
 
2154
                        Type type = elementType.BindTypeParameters(binder);
 
2155
                        CustomModifiers mods = this.mods.Bind(binder);
 
2156
                        if (ReferenceEquals(type, elementType)
 
2157
                                && mods.Equals(this.mods))
 
2158
                        {
 
2159
                                return this;
 
2160
                        }
 
2161
                        return Wrap(type, mods);
 
2162
                }
 
2163
 
 
2164
                internal override void CheckBaked()
 
2165
                {
 
2166
                        elementType.CheckBaked();
 
2167
                }
 
2168
 
 
2169
                internal sealed override Universe Universe
 
2170
                {
 
2171
                        get { return elementType.Universe; }
 
2172
                }
 
2173
 
 
2174
                internal sealed override bool IsBaked
 
2175
                {
 
2176
                        get { return elementType.IsBaked; }
 
2177
                }
 
2178
 
 
2179
                internal sealed override int GetCurrentToken()
 
2180
                {
 
2181
                        // we don't have a token, so we return 0 (which is never a valid token)
 
2182
                        return 0;
 
2183
                }
 
2184
 
 
2185
                internal abstract string GetSuffix();
 
2186
 
 
2187
                protected abstract Type Wrap(Type type, CustomModifiers mods);
 
2188
        }
 
2189
 
 
2190
        sealed class ArrayType : ElementHolderType
 
2191
        {
 
2192
                internal static Type Make(Type type, CustomModifiers mods)
 
2193
                {
 
2194
                        return type.Universe.CanonicalizeType(new ArrayType(type, mods));
 
2195
                }
 
2196
 
 
2197
                private ArrayType(Type type, CustomModifiers mods)
 
2198
                        : base(type, mods)
 
2199
                {
 
2200
                }
 
2201
 
 
2202
                public override Type BaseType
 
2203
                {
 
2204
                        get { return elementType.Module.universe.System_Array; }
 
2205
                }
 
2206
 
 
2207
                public override Type[] __GetDeclaredInterfaces()
 
2208
                {
 
2209
                        return new Type[] {
 
2210
                                this.Module.universe.Import(typeof(IList<>)).MakeGenericType(elementType),
 
2211
                                this.Module.universe.Import(typeof(ICollection<>)).MakeGenericType(elementType),
 
2212
                                this.Module.universe.Import(typeof(IEnumerable<>)).MakeGenericType(elementType)
 
2213
                        };
 
2214
                }
 
2215
 
 
2216
                public override MethodBase[] __GetDeclaredMethods()
 
2217
                {
 
2218
                        Type[] int32 = new Type[] { this.Module.universe.System_Int32 };
 
2219
                        List<MethodBase> list = new List<MethodBase>();
 
2220
                        list.Add(new BuiltinArrayMethod(this.Module, this, "Set", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, new Type[] { this.Module.universe.System_Int32, elementType }));
 
2221
                        list.Add(new BuiltinArrayMethod(this.Module, this, "Address", CallingConventions.Standard | CallingConventions.HasThis, elementType.MakeByRefType(), int32));
 
2222
                        list.Add(new BuiltinArrayMethod(this.Module, this, "Get", CallingConventions.Standard | CallingConventions.HasThis, elementType, int32));
 
2223
                        list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, int32)));
 
2224
                        for (Type type = elementType; type.__IsVector; type = type.GetElementType())
 
2225
                        {
 
2226
                                Array.Resize(ref int32, int32.Length + 1);
 
2227
                                int32[int32.Length - 1] = int32[0];
 
2228
                                list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, int32)));
 
2229
                        }
 
2230
                        return list.ToArray();
 
2231
                }
 
2232
 
 
2233
                public override TypeAttributes Attributes
 
2234
                {
 
2235
                        get { return TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Serializable; }
 
2236
                }
 
2237
 
 
2238
                public override bool IsArray
 
2239
                {
 
2240
                        get { return true; }
 
2241
                }
 
2242
 
 
2243
                public override bool __IsVector
 
2244
                {
 
2245
                        get { return true; }
 
2246
                }
 
2247
 
 
2248
                public override int GetArrayRank()
 
2249
                {
 
2250
                        return 1;
 
2251
                }
 
2252
 
 
2253
                public override bool Equals(object o)
 
2254
                {
 
2255
                        return EqualsHelper(o as ArrayType);
 
2256
                }
 
2257
 
 
2258
                public override int GetHashCode()
 
2259
                {
 
2260
                        return elementType.GetHashCode() * 5;
 
2261
                }
 
2262
 
 
2263
                internal override string GetSuffix()
 
2264
                {
 
2265
                        return "[]";
 
2266
                }
 
2267
 
 
2268
                protected override Type Wrap(Type type, CustomModifiers mods)
 
2269
                {
 
2270
                        return Make(type, mods);
 
2271
                }
 
2272
        }
 
2273
 
 
2274
        sealed class MultiArrayType : ElementHolderType
 
2275
        {
 
2276
                private readonly int rank;
 
2277
                private readonly int[] sizes;
 
2278
                private readonly int[] lobounds;
 
2279
 
 
2280
                internal static Type Make(Type type, int rank, int[] sizes, int[] lobounds, CustomModifiers mods)
 
2281
                {
 
2282
                        return type.Universe.CanonicalizeType(new MultiArrayType(type, rank, sizes, lobounds, mods));
 
2283
                }
 
2284
 
 
2285
                private MultiArrayType(Type type, int rank, int[] sizes, int[] lobounds, CustomModifiers mods)
 
2286
                        : base(type, mods)
 
2287
                {
 
2288
                        this.rank = rank;
 
2289
                        this.sizes = sizes;
 
2290
                        this.lobounds = lobounds;
 
2291
                }
 
2292
 
 
2293
                public override Type BaseType
 
2294
                {
 
2295
                        get { return elementType.Module.universe.System_Array; }
 
2296
                }
 
2297
 
 
2298
                public override MethodBase[] __GetDeclaredMethods()
 
2299
                {
 
2300
                        Type int32 = this.Module.universe.System_Int32;
 
2301
                        Type[] setArgs = new Type[rank + 1];
 
2302
                        Type[] getArgs = new Type[rank];
 
2303
                        Type[] ctorArgs = new Type[rank * 2];
 
2304
                        for (int i = 0; i < rank; i++)
 
2305
                        {
 
2306
                                setArgs[i] = int32;
 
2307
                                getArgs[i] = int32;
 
2308
                                ctorArgs[i * 2 + 0] = int32;
 
2309
                                ctorArgs[i * 2 + 1] = int32;
 
2310
                        }
 
2311
                        setArgs[rank] = elementType;
 
2312
                        return new MethodBase[] {
 
2313
                                new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, getArgs)),
 
2314
                                new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, ctorArgs)),
 
2315
                                new BuiltinArrayMethod(this.Module, this, "Set", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, setArgs),
 
2316
                                new BuiltinArrayMethod(this.Module, this, "Address", CallingConventions.Standard | CallingConventions.HasThis, elementType.MakeByRefType(), getArgs),
 
2317
                                new BuiltinArrayMethod(this.Module, this, "Get", CallingConventions.Standard | CallingConventions.HasThis, elementType, getArgs),
 
2318
                        };
 
2319
                }
 
2320
 
 
2321
                public override TypeAttributes Attributes
 
2322
                {
 
2323
                        get { return TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Serializable; }
 
2324
                }
 
2325
 
 
2326
                public override bool IsArray
 
2327
                {
 
2328
                        get { return true; }
 
2329
                }
 
2330
 
 
2331
                public override int GetArrayRank()
 
2332
                {
 
2333
                        return rank;
 
2334
                }
 
2335
 
 
2336
                public override int[] __GetArraySizes()
 
2337
                {
 
2338
                        return Util.Copy(sizes);
 
2339
                }
 
2340
 
 
2341
                public override int[] __GetArrayLowerBounds()
 
2342
                {
 
2343
                        return Util.Copy(lobounds);
 
2344
                }
 
2345
 
 
2346
                public override bool Equals(object o)
 
2347
                {
 
2348
                        MultiArrayType at = o as MultiArrayType;
 
2349
                        return EqualsHelper(at)
 
2350
                                && at.rank == rank
 
2351
                                && ArrayEquals(at.sizes, sizes)
 
2352
                                && ArrayEquals(at.lobounds, lobounds);
 
2353
                }
 
2354
 
 
2355
                private static bool ArrayEquals(int[] i1, int[] i2)
 
2356
                {
 
2357
                        if (i1.Length == i2.Length)
 
2358
                        {
 
2359
                                for (int i = 0; i < i1.Length; i++)
 
2360
                                {
 
2361
                                        if (i1[i] != i2[i])
 
2362
                                        {
 
2363
                                                return false;
 
2364
                                        }
 
2365
                                }
 
2366
                                return true;
 
2367
                        }
 
2368
                        return false;
 
2369
                }
 
2370
 
 
2371
                public override int GetHashCode()
 
2372
                {
 
2373
                        return elementType.GetHashCode() * 9 + rank;
 
2374
                }
 
2375
 
 
2376
                internal override string GetSuffix()
 
2377
                {
 
2378
                        if (rank == 1)
 
2379
                        {
 
2380
                                return "[*]";
 
2381
                        }
 
2382
                        else
 
2383
                        {
 
2384
                                return "[" + new String(',', rank - 1) + "]";
 
2385
                        }
 
2386
                }
 
2387
 
 
2388
                protected override Type Wrap(Type type, CustomModifiers mods)
 
2389
                {
 
2390
                        return Make(type, rank, sizes, lobounds, mods);
 
2391
                }
 
2392
        }
 
2393
 
 
2394
        sealed class BuiltinArrayMethod : ArrayMethod
 
2395
        {
 
2396
                internal BuiltinArrayMethod(Module module, Type arrayClass, string methodName, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
 
2397
                        : base(module, arrayClass, methodName, callingConvention, returnType, parameterTypes)
 
2398
                {
 
2399
                }
 
2400
 
 
2401
                public override MethodAttributes Attributes
 
2402
                {
 
2403
                        get { return this.Name == ".ctor" ? MethodAttributes.RTSpecialName | MethodAttributes.Public : MethodAttributes.Public; }
 
2404
                }
 
2405
 
 
2406
                public override MethodImplAttributes GetMethodImplementationFlags()
 
2407
                {
 
2408
                        return MethodImplAttributes.IL;
 
2409
                }
 
2410
 
 
2411
                public override int MetadataToken
 
2412
                {
 
2413
                        get { return 0x06000000; }
 
2414
                }
 
2415
 
 
2416
                public override MethodBody GetMethodBody()
 
2417
                {
 
2418
                        return null;
 
2419
                }
 
2420
 
 
2421
                public override ParameterInfo[] GetParameters()
 
2422
                {
 
2423
                        ParameterInfo[] parameterInfos = new ParameterInfo[parameterTypes.Length];
 
2424
                        for (int i = 0; i < parameterInfos.Length; i++)
 
2425
                        {
 
2426
                                parameterInfos[i] = new ParameterInfoImpl(this, parameterTypes[i], i);
 
2427
                        }
 
2428
                        return parameterInfos;
 
2429
                }
 
2430
 
 
2431
                public override ParameterInfo ReturnParameter
 
2432
                {
 
2433
                        get { return new ParameterInfoImpl(this, this.ReturnType, -1); }
 
2434
                }
 
2435
 
 
2436
                private sealed class ParameterInfoImpl : ParameterInfo
 
2437
                {
 
2438
                        private readonly MethodInfo method;
 
2439
                        private readonly Type type;
 
2440
                        private readonly int pos;
 
2441
 
 
2442
                        internal ParameterInfoImpl(MethodInfo method, Type type, int pos)
 
2443
                        {
 
2444
                                this.method = method;
 
2445
                                this.type = type;
 
2446
                                this.pos = pos;
 
2447
                        }
 
2448
 
 
2449
                        public override Type ParameterType
 
2450
                        {
 
2451
                                get { return type; }
 
2452
                        }
 
2453
 
 
2454
                        public override string Name
 
2455
                        {
 
2456
                                get { return null; }
 
2457
                        }
 
2458
 
 
2459
                        public override ParameterAttributes Attributes
 
2460
                        {
 
2461
                                get { return ParameterAttributes.None; }
 
2462
                        }
 
2463
 
 
2464
                        public override int Position
 
2465
                        {
 
2466
                                get { return pos; }
 
2467
                        }
 
2468
 
 
2469
                        public override object RawDefaultValue
 
2470
                        {
 
2471
                                get { return null; }
 
2472
                        }
 
2473
 
 
2474
                        public override CustomModifiers __GetCustomModifiers()
 
2475
                        {
 
2476
                                return new CustomModifiers();
 
2477
                        }
 
2478
 
 
2479
                        public override bool __TryGetFieldMarshal(out FieldMarshal fieldMarshal)
 
2480
                        {
 
2481
                                fieldMarshal = new FieldMarshal();
 
2482
                                return false;
 
2483
                        }
 
2484
 
 
2485
                        public override MemberInfo Member
 
2486
                        {
 
2487
                                get { return method.IsConstructor ? (MethodBase)new ConstructorInfoImpl(method) : method; }
 
2488
                        }
 
2489
 
 
2490
                        public override int MetadataToken
 
2491
                        {
 
2492
                                get { return 0x08000000; }
 
2493
                        }
 
2494
 
 
2495
                        internal override Module Module
 
2496
                        {
 
2497
                                get { return method.Module; }
 
2498
                        }
 
2499
                }
 
2500
        }
 
2501
 
 
2502
        sealed class ByRefType : ElementHolderType
 
2503
        {
 
2504
                internal static Type Make(Type type, CustomModifiers mods)
 
2505
                {
 
2506
                        return type.Universe.CanonicalizeType(new ByRefType(type, mods));
 
2507
                }
 
2508
 
 
2509
                private ByRefType(Type type, CustomModifiers mods)
 
2510
                        : base(type, mods)
 
2511
                {
 
2512
                }
 
2513
 
 
2514
                public override bool Equals(object o)
 
2515
                {
 
2516
                        return EqualsHelper(o as ByRefType);
 
2517
                }
 
2518
 
 
2519
                public override int GetHashCode()
 
2520
                {
 
2521
                        return elementType.GetHashCode() * 3;
 
2522
                }
 
2523
 
 
2524
                public override Type BaseType
 
2525
                {
 
2526
                        get { return null; }
 
2527
                }
 
2528
 
 
2529
                public override TypeAttributes Attributes
 
2530
                {
 
2531
                        get { return 0; }
 
2532
                }
 
2533
 
 
2534
                public override bool IsByRef
 
2535
                {
 
2536
                        get { return true; }
 
2537
                }
 
2538
 
 
2539
                internal override string GetSuffix()
 
2540
                {
 
2541
                        return "&";
 
2542
                }
 
2543
 
 
2544
                protected override Type Wrap(Type type, CustomModifiers mods)
 
2545
                {
 
2546
                        return Make(type, mods);
 
2547
                }
 
2548
        }
 
2549
 
 
2550
        sealed class PointerType : ElementHolderType
 
2551
        {
 
2552
                internal static Type Make(Type type, CustomModifiers mods)
 
2553
                {
 
2554
                        return type.Universe.CanonicalizeType(new PointerType(type, mods));
 
2555
                }
 
2556
 
 
2557
                private PointerType(Type type, CustomModifiers mods)
 
2558
                        : base(type, mods)
 
2559
                {
 
2560
                }
 
2561
 
 
2562
                public override bool Equals(object o)
 
2563
                {
 
2564
                        return EqualsHelper(o as PointerType);
 
2565
                }
 
2566
 
 
2567
                public override int GetHashCode()
 
2568
                {
 
2569
                        return elementType.GetHashCode() * 7;
 
2570
                }
 
2571
 
 
2572
                public override Type BaseType
 
2573
                {
 
2574
                        get { return null; }
 
2575
                }
 
2576
 
 
2577
                public override TypeAttributes Attributes
 
2578
                {
 
2579
                        get { return 0; }
 
2580
                }
 
2581
 
 
2582
                public override bool IsPointer
 
2583
                {
 
2584
                        get { return true; }
 
2585
                }
 
2586
 
 
2587
                internal override string GetSuffix()
 
2588
                {
 
2589
                        return "*";
 
2590
                }
 
2591
 
 
2592
                protected override Type Wrap(Type type, CustomModifiers mods)
 
2593
                {
 
2594
                        return Make(type, mods);
 
2595
                }
 
2596
        }
 
2597
 
 
2598
        sealed class GenericTypeInstance : TypeInfo
 
2599
        {
 
2600
                private readonly Type type;
 
2601
                private readonly Type[] args;
 
2602
                private readonly CustomModifiers[] mods;
 
2603
                private Type baseType;
 
2604
                private int token;
 
2605
 
 
2606
                internal static Type Make(Type type, Type[] typeArguments, CustomModifiers[] mods)
 
2607
                {
 
2608
                        bool identity = true;
 
2609
                        if (type is TypeBuilder || type is BakedType || type.__IsMissing)
 
2610
                        {
 
2611
                                // a TypeBuiler identity must be instantiated
 
2612
                                identity = false;
 
2613
                        }
 
2614
                        else
 
2615
                        {
 
2616
                                // we must not instantiate the identity instance, because typeof(Foo<>).MakeGenericType(typeof(Foo<>).GetGenericArguments()) == typeof(Foo<>)
 
2617
                                for (int i = 0; i < typeArguments.Length; i++)
 
2618
                                {
 
2619
                                        if (typeArguments[i] != type.GetGenericTypeArgument(i)
 
2620
                                                || !IsEmpty(mods, i))
 
2621
                                        {
 
2622
                                                identity = false;
 
2623
                                                break;
 
2624
                                        }
 
2625
                                }
 
2626
                        }
 
2627
                        if (identity)
 
2628
                        {
 
2629
                                return type;
 
2630
                        }
 
2631
                        else
 
2632
                        {
 
2633
                                return type.Universe.CanonicalizeType(new GenericTypeInstance(type, typeArguments, mods));
 
2634
                        }
 
2635
                }
 
2636
 
 
2637
                private static bool IsEmpty(CustomModifiers[] mods, int i)
 
2638
                {
 
2639
                        // we need to be extra careful, because mods doesn't not need to be in canonical format
 
2640
                        // (Signature.ReadGenericInst() calls Make() directly, without copying the modifier arrays)
 
2641
                        return mods == null || mods[i].IsEmpty;
 
2642
                }
 
2643
 
 
2644
                private GenericTypeInstance(Type type, Type[] args, CustomModifiers[] mods)
 
2645
                {
 
2646
                        this.type = type;
 
2647
                        this.args = args;
 
2648
                        this.mods = mods;
 
2649
                }
 
2650
 
 
2651
                public override bool Equals(object o)
 
2652
                {
 
2653
                        GenericTypeInstance gt = o as GenericTypeInstance;
 
2654
                        return gt != null && gt.type.Equals(type) && Util.ArrayEquals(gt.args, args)
 
2655
                                && Util.ArrayEquals(gt.mods, mods);
 
2656
                }
 
2657
 
 
2658
                public override int GetHashCode()
 
2659
                {
 
2660
                        return type.GetHashCode() * 3 ^ Util.GetHashCode(args);
 
2661
                }
 
2662
 
 
2663
                public override string AssemblyQualifiedName
 
2664
                {
 
2665
                        get
 
2666
                        {
 
2667
                                string fn = FullName;
 
2668
                                return fn == null ? null : fn + ", " + type.Assembly.FullName;
 
2669
                        }
 
2670
                }
 
2671
 
 
2672
                public override Type BaseType
 
2673
                {
 
2674
                        get
 
2675
                        {
 
2676
                                if (baseType == null)
 
2677
                                {
 
2678
                                        Type rawBaseType = type.BaseType;
 
2679
                                        if (rawBaseType == null)
 
2680
                                        {
 
2681
                                                baseType = rawBaseType;
 
2682
                                        }
 
2683
                                        else
 
2684
                                        {
 
2685
                                                baseType = rawBaseType.BindTypeParameters(this);
 
2686
                                        }
 
2687
                                }
 
2688
                                return baseType;
 
2689
                        }
 
2690
                }
 
2691
 
 
2692
                public override bool IsValueType
 
2693
                {
 
2694
                        get { return type.IsValueType; }
 
2695
                }
 
2696
 
 
2697
                public override bool IsVisible
 
2698
                {
 
2699
                        get
 
2700
                        {
 
2701
                                if (base.IsVisible)
 
2702
                                {
 
2703
                                        foreach (Type arg in args)
 
2704
                                        {
 
2705
                                                if (!arg.IsVisible)
 
2706
                                                {
 
2707
                                                        return false;
 
2708
                                                }
 
2709
                                        }
 
2710
                                        return true;
 
2711
                                }
 
2712
                                return false;
 
2713
                        }
 
2714
                }
 
2715
 
 
2716
                public override Type DeclaringType
 
2717
                {
 
2718
                        get { return type.DeclaringType; }
 
2719
                }
 
2720
 
 
2721
                public override TypeAttributes Attributes
 
2722
                {
 
2723
                        get { return type.Attributes; }
 
2724
                }
 
2725
 
 
2726
                internal override void CheckBaked()
 
2727
                {
 
2728
                        type.CheckBaked();
 
2729
                }
 
2730
 
 
2731
                public override FieldInfo[] __GetDeclaredFields()
 
2732
                {
 
2733
                        FieldInfo[] fields = type.__GetDeclaredFields();
 
2734
                        for (int i = 0; i < fields.Length; i++)
 
2735
                        {
 
2736
                                fields[i] = fields[i].BindTypeParameters(this);
 
2737
                        }
 
2738
                        return fields;
 
2739
                }
 
2740
 
 
2741
                public override Type[] __GetDeclaredInterfaces()
 
2742
                {
 
2743
                        Type[] interfaces = type.__GetDeclaredInterfaces();
 
2744
                        for (int i = 0; i < interfaces.Length; i++)
 
2745
                        {
 
2746
                                interfaces[i] = interfaces[i].BindTypeParameters(this);
 
2747
                        }
 
2748
                        return interfaces;
 
2749
                }
 
2750
 
 
2751
                public override MethodBase[] __GetDeclaredMethods()
 
2752
                {
 
2753
                        MethodBase[] methods = type.__GetDeclaredMethods();
 
2754
                        for (int i = 0; i < methods.Length; i++)
 
2755
                        {
 
2756
                                methods[i] = methods[i].BindTypeParameters(this);
 
2757
                        }
 
2758
                        return methods;
 
2759
                }
 
2760
 
 
2761
                public override Type[] __GetDeclaredTypes()
 
2762
                {
 
2763
                        return type.__GetDeclaredTypes();
 
2764
                }
 
2765
 
 
2766
                public override EventInfo[] __GetDeclaredEvents()
 
2767
                {
 
2768
                        EventInfo[] events = type.__GetDeclaredEvents();
 
2769
                        for (int i = 0; i < events.Length; i++)
 
2770
                        {
 
2771
                                events[i] = events[i].BindTypeParameters(this);
 
2772
                        }
 
2773
                        return events;
 
2774
                }
 
2775
 
 
2776
                public override PropertyInfo[] __GetDeclaredProperties()
 
2777
                {
 
2778
                        PropertyInfo[] properties = type.__GetDeclaredProperties();
 
2779
                        for (int i = 0; i < properties.Length; i++)
 
2780
                        {
 
2781
                                properties[i] = properties[i].BindTypeParameters(this);
 
2782
                        }
 
2783
                        return properties;
 
2784
                }
 
2785
 
 
2786
                public override __MethodImplMap __GetMethodImplMap()
 
2787
                {
 
2788
                        __MethodImplMap map = type.__GetMethodImplMap();
 
2789
                        map.TargetType = this;
 
2790
                        for (int i = 0; i < map.MethodBodies.Length; i++)
 
2791
                        {
 
2792
                                map.MethodBodies[i] = (MethodInfo)map.MethodBodies[i].BindTypeParameters(this);
 
2793
                                for (int j = 0; j < map.MethodDeclarations[i].Length; j++)
 
2794
                                {
 
2795
                                        Type interfaceType = map.MethodDeclarations[i][j].DeclaringType;
 
2796
                                        if (interfaceType.IsGenericType)
 
2797
                                        {
 
2798
                                                map.MethodDeclarations[i][j] = (MethodInfo)map.MethodDeclarations[i][j].BindTypeParameters(this);
 
2799
                                        }
 
2800
                                }
 
2801
                        }
 
2802
                        return map;
 
2803
                }
 
2804
 
 
2805
                public override string Namespace
 
2806
                {
 
2807
                        get { return type.Namespace; }
 
2808
                }
 
2809
 
 
2810
                public override string Name
 
2811
                {
 
2812
                        get { return type.Name; }
 
2813
                }
 
2814
 
 
2815
                public override string FullName
 
2816
                {
 
2817
                        get
 
2818
                        {
 
2819
                                if (!this.__ContainsMissingType && this.ContainsGenericParameters)
 
2820
                                {
 
2821
                                        return null;
 
2822
                                }
 
2823
                                StringBuilder sb = new StringBuilder(this.type.FullName);
 
2824
                                sb.Append('[');
 
2825
                                string sep = "";
 
2826
                                foreach (Type type in args)
 
2827
                                {
 
2828
                                        sb.Append(sep).Append('[').Append(type.FullName).Append(", ").Append(type.Assembly.FullName.Replace("]", "\\]")).Append(']');
 
2829
                                        sep = ",";
 
2830
                                }
 
2831
                                sb.Append(']');
 
2832
                                return sb.ToString();
 
2833
                        }
 
2834
                }
 
2835
 
 
2836
                public override string ToString()
 
2837
                {
 
2838
                        StringBuilder sb = new StringBuilder(type.FullName);
 
2839
                        sb.Append('[');
 
2840
                        string sep = "";
 
2841
                        foreach (Type arg in args)
 
2842
                        {
 
2843
                                sb.Append(sep);
 
2844
                                sb.Append(arg);
 
2845
                                sep = ",";
 
2846
                        }
 
2847
                        sb.Append(']');
 
2848
                        return sb.ToString();
 
2849
                }
 
2850
 
 
2851
                public override Module Module
 
2852
                {
 
2853
                        get { return type.Module; }
 
2854
                }
 
2855
 
 
2856
                public override bool IsGenericType
 
2857
                {
 
2858
                        get { return true; }
 
2859
                }
 
2860
 
 
2861
                public override bool IsConstructedGenericType
 
2862
                {
 
2863
                        get { return true; }
 
2864
                }
 
2865
 
 
2866
                public override Type GetGenericTypeDefinition()
 
2867
                {
 
2868
                        return type;
 
2869
                }
 
2870
 
 
2871
                public override Type[] GetGenericArguments()
 
2872
                {
 
2873
                        return Util.Copy(args);
 
2874
                }
 
2875
 
 
2876
                public override CustomModifiers[] __GetGenericArgumentsCustomModifiers()
 
2877
                {
 
2878
                        return mods != null ? (CustomModifiers[])mods.Clone() : new CustomModifiers[args.Length];
 
2879
                }
 
2880
 
 
2881
                internal override Type GetGenericTypeArgument(int index)
 
2882
                {
 
2883
                        return args[index];
 
2884
                }
 
2885
 
 
2886
                public override bool ContainsGenericParameters
 
2887
                {
 
2888
                        get
 
2889
                        {
 
2890
                                foreach (Type type in args)
 
2891
                                {
 
2892
                                        if (type.ContainsGenericParameters)
 
2893
                                        {
 
2894
                                                return true;
 
2895
                                        }
 
2896
                                }
 
2897
                                return false;
 
2898
                        }
 
2899
                }
 
2900
 
 
2901
                public override bool __ContainsMissingType
 
2902
                {
 
2903
                        get
 
2904
                        {
 
2905
                                foreach (Type type in args)
 
2906
                                {
 
2907
                                        if (type.__ContainsMissingType)
 
2908
                                        {
 
2909
                                                return true;
 
2910
                                        }
 
2911
                                }
 
2912
                                return this.type.__IsMissing;
 
2913
                        }
 
2914
                }
 
2915
 
 
2916
                public override StructLayoutAttribute StructLayoutAttribute
 
2917
                {
 
2918
                        get { return type.StructLayoutAttribute; }
 
2919
                }
 
2920
 
 
2921
                internal override int GetModuleBuilderToken()
 
2922
                {
 
2923
                        if (token == 0)
 
2924
                        {
 
2925
                                token = ((ModuleBuilder)type.Module).ImportType(this);
 
2926
                        }
 
2927
                        return token;
 
2928
                }
 
2929
 
 
2930
                internal override Type BindTypeParameters(IGenericBinder binder)
 
2931
                {
 
2932
                        for (int i = 0; i < args.Length; i++)
 
2933
                        {
 
2934
                                Type xarg = args[i].BindTypeParameters(binder);
 
2935
                                if (!ReferenceEquals(xarg, args[i]))
 
2936
                                {
 
2937
                                        Type[] xargs = new Type[args.Length];
 
2938
                                        Array.Copy(args, xargs, i);
 
2939
                                        xargs[i++] = xarg;
 
2940
                                        for (; i < args.Length; i++)
 
2941
                                        {
 
2942
                                                xargs[i] = args[i].BindTypeParameters(binder);
 
2943
                                        }
 
2944
                                        return Make(type, xargs, null);
 
2945
                                }
 
2946
                        }
 
2947
                        return this;
 
2948
                }
 
2949
 
 
2950
                internal override int GetCurrentToken()
 
2951
                {
 
2952
                        return type.GetCurrentToken();
 
2953
                }
 
2954
 
 
2955
                internal override bool IsBaked
 
2956
                {
 
2957
                        get { return type.IsBaked; }
 
2958
                }
 
2959
        }
 
2960
 
 
2961
        sealed class FunctionPointerType : TypeInfo
 
2962
        {
 
2963
                private readonly Universe universe;
 
2964
                private readonly __StandAloneMethodSig sig;
 
2965
 
 
2966
                internal static Type Make(Universe universe, __StandAloneMethodSig sig)
 
2967
                {
 
2968
                        return universe.CanonicalizeType(new FunctionPointerType(universe, sig));
 
2969
                }
 
2970
 
 
2971
                private FunctionPointerType(Universe universe, __StandAloneMethodSig sig)
 
2972
                {
 
2973
                        this.universe = universe;
 
2974
                        this.sig = sig;
 
2975
                }
 
2976
 
 
2977
                public override bool Equals(object obj)
 
2978
                {
 
2979
                        FunctionPointerType other = obj as FunctionPointerType;
 
2980
                        return other != null
 
2981
                                && other.universe == universe
 
2982
                                && other.sig.Equals(sig);
 
2983
                }
 
2984
 
 
2985
                public override int GetHashCode()
 
2986
                {
 
2987
                        return sig.GetHashCode();
 
2988
                }
 
2989
 
 
2990
                public override bool __IsFunctionPointer
 
2991
                {
 
2992
                        get { return true; }
 
2993
                }
 
2994
 
 
2995
                public override __StandAloneMethodSig __MethodSignature
 
2996
                {
 
2997
                        get { return sig; }
 
2998
                }
 
2999
 
 
3000
                public override Type BaseType
 
3001
                {
 
3002
                        get { return null; }
 
3003
                }
 
3004
 
 
3005
                public override TypeAttributes Attributes
 
3006
                {
 
3007
                        get { return 0; }
 
3008
                }
 
3009
 
 
3010
                public override string Name
 
3011
                {
 
3012
                        get { throw new InvalidOperationException(); }
 
3013
                }
 
3014
 
 
3015
                public override string FullName
 
3016
                {
 
3017
                        get { throw new InvalidOperationException(); }
 
3018
                }
 
3019
 
 
3020
                public override Module Module
 
3021
                {
 
3022
                        get { throw new InvalidOperationException(); }
 
3023
                }
 
3024
 
 
3025
                internal override Universe Universe
 
3026
                {
 
3027
                        get { return universe; }
 
3028
                }
 
3029
 
 
3030
                public override string ToString()
 
3031
                {
 
3032
                        return "<FunctionPtr>";
 
3033
                }
 
3034
 
 
3035
                internal override bool IsBaked
 
3036
                {
 
3037
                        get { return true; }
 
3038
                }
 
3039
        }
 
3040
 
 
3041
        sealed class MarkerType : Type
 
3042
        {
 
3043
                // used by CustomModifiers and SignatureHelper
 
3044
                internal static readonly Type ModOpt = new MarkerType();
 
3045
                internal static readonly Type ModReq = new MarkerType();
 
3046
                // used by SignatureHelper
 
3047
                internal static readonly Type Sentinel = new MarkerType();
 
3048
                internal static readonly Type Pinned = new MarkerType();
 
3049
 
 
3050
                private MarkerType() { }
 
3051
 
 
3052
                public override Type BaseType
 
3053
                {
 
3054
                        get { throw new InvalidOperationException(); }
 
3055
                }
 
3056
 
 
3057
                public override TypeAttributes Attributes
 
3058
                {
 
3059
                        get { throw new InvalidOperationException(); }
 
3060
                }
 
3061
 
 
3062
                public override string Name
 
3063
                {
 
3064
                        get { throw new InvalidOperationException(); }
 
3065
                }
 
3066
 
 
3067
                public override string FullName
 
3068
                {
 
3069
                        get { throw new InvalidOperationException(); }
 
3070
                }
 
3071
 
 
3072
                public override Module Module
 
3073
                {
 
3074
                        get { throw new InvalidOperationException(); }
 
3075
                }
 
3076
 
 
3077
                internal override bool IsBaked
 
3078
                {
 
3079
                        get { throw new InvalidOperationException(); }
 
3080
                }
 
3081
 
 
3082
                public override bool __IsMissing
 
3083
                {
 
3084
                        get { throw new InvalidOperationException(); }
 
3085
                }
 
3086
        }
 
3087
}