Merge pull request #271 from pruiz/xamarin-bug-4108
[mono.git] / mcs / class / IKVM.Reflection / Missing.cs
1 /*
2   Copyright (C) 2011-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.Collections.Generic;
26 using System.Runtime.InteropServices;
27
28 namespace IKVM.Reflection
29 {
30         [Serializable]
31         public sealed class MissingAssemblyException : InvalidOperationException
32         {
33                 [NonSerialized]
34                 private readonly MissingAssembly assembly;
35
36                 internal MissingAssemblyException(MissingAssembly assembly)
37                         : base("Assembly '" + assembly.FullName + "' is a missing assembly and does not support the requested operation.")
38                 {
39                         this.assembly = assembly;
40                 }
41
42                 private MissingAssemblyException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
43                         : base(info, context)
44                 {
45                 }
46
47                 public Assembly Assembly
48                 {
49                         get { return assembly; }
50                 }
51         }
52
53         [Serializable]
54         public sealed class MissingModuleException : InvalidOperationException
55         {
56                 [NonSerialized]
57                 private readonly MissingModule module;
58
59                 internal MissingModuleException(MissingModule module)
60                         : base("Module from missing assembly '" + module.Assembly.FullName + "' does not support the requested operation.")
61                 {
62                         this.module = module;
63                 }
64
65                 private MissingModuleException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
66                         : base(info, context)
67                 {
68                 }
69
70                 public Module Module
71                 {
72                         get { return module; }
73                 }
74         }
75
76         [Serializable]
77         public sealed class MissingMemberException : InvalidOperationException
78         {
79                 [NonSerialized]
80                 private readonly MemberInfo member;
81
82                 internal MissingMemberException(MemberInfo member)
83                         : base("Member '" + member + "' is a missing member and does not support the requested operation.")
84                 {
85                         this.member = member;
86                 }
87
88                 private MissingMemberException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
89                         : base(info, context)
90                 {
91                 }
92
93                 public MemberInfo MemberInfo
94                 {
95                         get { return member; }
96                 }
97         }
98
99         public struct MissingGenericMethodBuilder
100         {
101                 private readonly MissingMethod method;
102
103                 public MissingGenericMethodBuilder(Type declaringType, CallingConventions callingConvention, string name, int genericParameterCount)
104                 {
105                         method = new MissingMethod(declaringType, name, new MethodSignature(null, null, new PackedCustomModifiers(), callingConvention, genericParameterCount));
106                 }
107
108                 public Type[] GetGenericArguments()
109                 {
110                         return method.GetGenericArguments();
111                 }
112
113                 public void SetSignature(Type returnType, CustomModifiers returnTypeCustomModifiers, Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers)
114                 {
115                         method.signature = new MethodSignature(
116                                 returnType ?? method.Module.universe.System_Void,
117                                 Util.Copy(parameterTypes),
118                                 PackedCustomModifiers.CreateFromExternal(returnTypeCustomModifiers, parameterTypeCustomModifiers, parameterTypes.Length),
119                                 method.signature.CallingConvention,
120                                 method.signature.GenericParameterCount);
121                 }
122
123                 [Obsolete("Please use SetSignature(Type, CustomModifiers, Type[], CustomModifiers[]) instead.")]
124                 public void SetSignature(Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
125                 {
126                         method.signature = new MethodSignature(
127                                 returnType ?? method.Module.universe.System_Void,
128                                 Util.Copy(parameterTypes),
129                                 PackedCustomModifiers.CreateFromExternal(returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers, parameterTypeRequiredCustomModifiers, parameterTypes.Length),
130                                 method.signature.CallingConvention,
131                                 method.signature.GenericParameterCount);
132                 }
133
134                 public MethodInfo Finish()
135                 {
136                         return method;
137                 }
138         }
139
140         sealed class MissingAssembly : Assembly
141         {
142                 private readonly MissingModule module;
143
144                 internal MissingAssembly(Universe universe, string name)
145                         : base(universe)
146                 {
147                         module = new MissingModule(this);
148                         this.fullName = name;
149                 }
150
151                 public override Type[] GetTypes()
152                 {
153                         throw new MissingAssemblyException(this);
154                 }
155
156                 public override AssemblyName GetName()
157                 {
158                         return new AssemblyName(fullName);
159                 }
160
161                 public override string ImageRuntimeVersion
162                 {
163                         get { throw new MissingAssemblyException(this); }
164                 }
165
166                 public override Module ManifestModule
167                 {
168                         get { return module; }
169                 }
170
171                 public override MethodInfo EntryPoint
172                 {
173                         get { throw new MissingAssemblyException(this); }
174                 }
175
176                 public override string Location
177                 {
178                         get { throw new MissingAssemblyException(this); }
179                 }
180
181                 public override AssemblyName[] GetReferencedAssemblies()
182                 {
183                         throw new MissingAssemblyException(this);
184                 }
185
186                 public override Module[] GetModules(bool getResourceModules)
187                 {
188                         throw new MissingAssemblyException(this);
189                 }
190
191                 public override Module[] GetLoadedModules(bool getResourceModules)
192                 {
193                         throw new MissingAssemblyException(this);
194                 }
195
196                 public override Module GetModule(string name)
197                 {
198                         throw new MissingAssemblyException(this);
199                 }
200
201                 public override string[] GetManifestResourceNames()
202                 {
203                         throw new MissingAssemblyException(this);
204                 }
205
206                 public override ManifestResourceInfo GetManifestResourceInfo(string resourceName)
207                 {
208                         throw new MissingAssemblyException(this);
209                 }
210
211                 public override System.IO.Stream GetManifestResourceStream(string resourceName)
212                 {
213                         throw new MissingAssemblyException(this);
214                 }
215
216                 public override bool __IsMissing
217                 {
218                         get { return true; }
219                 }
220
221                 internal override Type FindType(TypeName typeName)
222                 {
223                         return null;
224                 }
225
226                 internal override Type FindTypeIgnoreCase(TypeName lowerCaseName)
227                 {
228                         return null;
229                 }
230
231                 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
232                 {
233                         throw new MissingAssemblyException(this);
234                 }
235         }
236
237         sealed class MissingModule : NonPEModule
238         {
239                 private readonly MissingAssembly assembly;
240
241                 internal MissingModule(MissingAssembly assembly)
242                         : base(assembly.universe)
243                 {
244                         this.assembly = assembly;
245                 }
246
247                 public override int MDStreamVersion
248                 {
249                         get { throw new MissingModuleException(this); }
250                 }
251
252                 public override Assembly Assembly
253                 {
254                         get { return assembly; }
255                 }
256
257                 public override string FullyQualifiedName
258                 {
259                         get { throw new MissingModuleException(this); }
260                 }
261
262                 public override string Name
263                 {
264                         get { throw new MissingModuleException(this); }
265                 }
266
267                 public override Guid ModuleVersionId
268                 {
269                         get { throw new MissingModuleException(this); }
270                 }
271
272                 public override string ScopeName
273                 {
274                         get { throw new MissingModuleException(this); }
275                 }
276
277                 internal override Type FindType(TypeName typeName)
278                 {
279                         return null;
280                 }
281
282                 internal override Type FindTypeIgnoreCase(TypeName lowerCaseName)
283                 {
284                         return null;
285                 }
286
287                 internal override void GetTypesImpl(System.Collections.Generic.List<Type> list)
288                 {
289                         throw new MissingModuleException(this);
290                 }
291
292                 public override void __GetDataDirectoryEntry(int index, out int rva, out int length)
293                 {
294                         throw new MissingModuleException(this);
295                 }
296
297                 public override IList<CustomAttributeData> __GetPlaceholderAssemblyCustomAttributes(bool multiple, bool security)
298                 {
299                         throw new MissingModuleException(this);
300                 }
301
302                 public override long __RelativeVirtualAddressToFileOffset(int rva)
303                 {
304                         throw new MissingModuleException(this);
305                 }
306
307                 public override __StandAloneMethodSig __ResolveStandAloneMethodSig(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
308                 {
309                         throw new MissingModuleException(this);
310                 }
311
312                 public override int __Subsystem
313                 {
314                         get { throw new MissingModuleException(this); }
315                 }
316
317                 internal override void ExportTypes(int fileToken, IKVM.Reflection.Emit.ModuleBuilder manifestModule)
318                 {
319                         throw new MissingModuleException(this);
320                 }
321
322                 public override void GetPEKind(out PortableExecutableKinds peKind, out ImageFileMachine machine)
323                 {
324                         throw new MissingModuleException(this);
325                 }
326
327                 public override bool __IsMissing
328                 {
329                         get { return true; }
330                 }
331
332                 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
333                 {
334                         throw new MissingModuleException(this);
335                 }
336
337                 protected override Exception InvalidOperationException()
338                 {
339                         return new MissingModuleException(this);
340                 }
341
342                 protected override Exception NotSupportedException()
343                 {
344                         return new MissingModuleException(this);
345                 }
346
347                 protected override Exception ArgumentOutOfRangeException()
348                 {
349                         return new MissingModuleException(this);
350                 }
351         }
352
353         sealed class MissingType : Type
354         {
355                 private readonly Module module;
356                 private readonly Type declaringType;
357                 private readonly string ns;
358                 private readonly string name;
359                 private Type[] typeArgs;
360                 private int token;
361
362                 internal MissingType(Module module, Type declaringType, string ns, string name)
363                 {
364                         this.module = module;
365                         this.declaringType = declaringType;
366                         this.ns = ns;
367                         this.name = name;
368                         MarkEnumOrValueType(ns, name);
369                 }
370
371                 internal override MethodBase FindMethod(string name, MethodSignature signature)
372                 {
373                         MethodInfo method = new MissingMethod(this, name, signature);
374                         if (name == ".ctor")
375                         {
376                                 return new ConstructorInfoImpl(method);
377                         }
378                         return method;
379                 }
380
381                 internal override FieldInfo FindField(string name, FieldSignature signature)
382                 {
383                         return new MissingField(this, name, signature);
384                 }
385
386                 internal override Type FindNestedType(TypeName name)
387                 {
388                         return null;
389                 }
390
391                 internal override Type FindNestedTypeIgnoreCase(TypeName lowerCaseName)
392                 {
393                         return null;
394                 }
395
396                 public override bool __IsMissing
397                 {
398                         get { return true; }
399                 }
400
401                 public override Type DeclaringType
402                 {
403                         get { return declaringType; }
404                 }
405
406                 public override string __Name
407                 {
408                         get { return name; }
409                 }
410
411                 public override string __Namespace
412                 {
413                         get { return ns; }
414                 }
415
416                 public override string Name
417                 {
418                         get { return TypeNameParser.Escape(name); }
419                 }
420
421                 public override string FullName
422                 {
423                         get { return GetFullName(); }
424                 }
425
426                 public override Module Module
427                 {
428                         get { return module; }
429                 }
430
431                 public override int MetadataToken
432                 {
433                         get { return token; }
434                 }
435
436                 public override bool IsValueType
437                 {
438                         get
439                         {
440                                 switch (typeFlags & (TypeFlags.ValueType | TypeFlags.NotValueType))
441                                 {
442                                         case TypeFlags.ValueType:
443                                                 return true;
444                                         case TypeFlags.NotValueType:
445                                                 return false;
446                                         default:
447                                                 if (module.universe.ResolveMissingTypeIsValueType(this))
448                                                 {
449                                                         typeFlags |= TypeFlags.ValueType;
450                                                 }
451                                                 else
452                                                 {
453                                                         typeFlags |= TypeFlags.NotValueType;
454                                                 }
455                                                 return (typeFlags & TypeFlags.ValueType) != 0;
456                                 }
457                         }
458                 }
459
460                 public override Type BaseType
461                 {
462                         get { throw new MissingMemberException(this); }
463                 }
464
465                 public override TypeAttributes Attributes
466                 {
467                         get { throw new MissingMemberException(this); }
468                 }
469
470                 public override Type[] __GetDeclaredTypes()
471                 {
472                         throw new MissingMemberException(this);
473                 }
474
475                 public override Type[] __GetDeclaredInterfaces()
476                 {
477                         throw new MissingMemberException(this);
478                 }
479
480                 public override MethodBase[] __GetDeclaredMethods()
481                 {
482                         throw new MissingMemberException(this);
483                 }
484
485                 public override __MethodImplMap __GetMethodImplMap()
486                 {
487                         throw new MissingMemberException(this);
488                 }
489
490                 public override FieldInfo[] __GetDeclaredFields()
491                 {
492                         throw new MissingMemberException(this);
493                 }
494
495                 public override EventInfo[] __GetDeclaredEvents()
496                 {
497                         throw new MissingMemberException(this);
498                 }
499
500                 public override PropertyInfo[] __GetDeclaredProperties()
501                 {
502                         throw new MissingMemberException(this);
503                 }
504
505                 public override CustomModifiers __GetCustomModifiers()
506                 {
507                         throw new MissingMemberException(this);
508                 }
509
510                 public override Type[] GetGenericArguments()
511                 {
512                         throw new MissingMemberException(this);
513                 }
514
515                 public override CustomModifiers[] __GetGenericArgumentsCustomModifiers()
516                 {
517                         throw new MissingMemberException(this);
518                 }
519
520                 public override StructLayoutAttribute StructLayoutAttribute
521                 {
522                         get { throw new MissingMemberException(this); }
523                 }
524
525                 public override bool IsGenericType
526                 {
527                         get { throw new MissingMemberException(this); }
528                 }
529
530                 public override bool IsGenericTypeDefinition
531                 {
532                         get { throw new MissingMemberException(this); }
533                 }
534
535                 internal override Type GetGenericTypeArgument(int index)
536                 {
537                         if (typeArgs == null)
538                         {
539                                 typeArgs = new Type[index + 1];
540                         }
541                         else if (typeArgs.Length <= index)
542                         {
543                                 Array.Resize(ref typeArgs, index + 1);
544                         }
545                         return typeArgs[index] ?? (typeArgs[index] = new MissingTypeParameter(this, index));
546                 }
547
548                 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
549                 {
550                         throw new MissingMemberException(this);
551                 }
552
553                 internal override Type BindTypeParameters(IGenericBinder binder)
554                 {
555                         return this;
556                 }
557
558                 internal override Type SetMetadataTokenForMissing(int token)
559                 {
560                         this.token = token;
561                         return this;
562                 }
563         }
564
565         sealed class MissingTypeParameter : IKVM.Reflection.Reader.TypeParameterType
566         {
567                 private readonly MemberInfo owner;
568                 private readonly int index;
569
570                 internal MissingTypeParameter(MemberInfo owner, int index)
571                 {
572                         this.owner = owner;
573                         this.index = index;
574                 }
575
576                 public override Module Module
577                 {
578                         get { return owner.Module; }
579                 }
580
581                 public override string Name
582                 {
583                         get { return null; }
584                 }
585
586                 public override int GenericParameterPosition
587                 {
588                         get { return index; }
589                 }
590
591                 public override MethodBase DeclaringMethod
592                 {
593                         get { return owner as MethodBase; }
594                 }
595
596                 public override Type DeclaringType
597                 {
598                         get { return owner as Type; }
599                 }
600
601                 internal override Type BindTypeParameters(IGenericBinder binder)
602                 {
603                         if (owner is MethodBase)
604                         {
605                                 return binder.BindMethodParameter(this);
606                         }
607                         else
608                         {
609                                 return binder.BindTypeParameter(this);
610                         }
611                 }
612         }
613
614         sealed class MissingMethod : MethodInfo
615         {
616                 private readonly Type declaringType;
617                 private readonly string name;
618                 internal MethodSignature signature;
619                 private MethodInfo forwarder;
620                 private Type[] typeArgs;
621
622                 internal MissingMethod(Type declaringType, string name, MethodSignature signature)
623                 {
624                         this.declaringType = declaringType;
625                         this.name = name;
626                         this.signature = signature;
627                 }
628
629                 private MethodInfo Forwarder
630                 {
631                         get
632                         {
633                                 MethodInfo method = TryGetForwarder();
634                                 if (method == null)
635                                 {
636                                         throw new MissingMemberException(this);
637                                 }
638                                 return method;
639                         }
640                 }
641
642                 private MethodInfo TryGetForwarder()
643                 {
644                         if (forwarder == null && !declaringType.__IsMissing)
645                         {
646                                 MethodBase mb = declaringType.FindMethod(name, signature);
647                                 ConstructorInfo ci = mb as ConstructorInfo;
648                                 if (ci != null)
649                                 {
650                                         forwarder = ci.GetMethodInfo();
651                                 }
652                                 else
653                                 {
654                                         forwarder = (MethodInfo)mb;
655                                 }
656                         }
657                         return forwarder;
658                 }
659
660                 public override bool __IsMissing
661                 {
662                         get { return TryGetForwarder() == null; }
663                 }
664
665                 public override Type ReturnType
666                 {
667                         get { return signature.GetReturnType(this); }
668                 }
669
670                 public override ParameterInfo ReturnParameter
671                 {
672                         get { return new ParameterInfoImpl(this, -1); }
673                 }
674
675                 internal override MethodSignature MethodSignature
676                 {
677                         get { return signature; }
678                 }
679
680                 internal override int ParameterCount
681                 {
682                         get { return signature.GetParameterCount(); }
683                 }
684
685                 private sealed class ParameterInfoImpl : ParameterInfo
686                 {
687                         private readonly MissingMethod method;
688                         private readonly int index;
689
690                         internal ParameterInfoImpl(MissingMethod method, int index)
691                         {
692                                 this.method = method;
693                                 this.index = index;
694                         }
695
696                         private ParameterInfo Forwarder
697                         {
698                                 get { return index == -1 ? method.Forwarder.ReturnParameter : method.Forwarder.GetParameters()[index]; }
699                         }
700
701                         public override string Name
702                         {
703                                 get { return Forwarder.Name; }
704                         }
705
706                         public override Type ParameterType
707                         {
708                                 get { return index == -1 ? method.signature.GetReturnType(method) : method.signature.GetParameterType(method, index); }
709                         }
710
711                         public override ParameterAttributes Attributes
712                         {
713                                 get { return Forwarder.Attributes; }
714                         }
715
716                         public override int Position
717                         {
718                                 get { return index; }
719                         }
720
721                         public override object RawDefaultValue
722                         {
723                                 get { return Forwarder.RawDefaultValue; }
724                         }
725
726                         public override CustomModifiers __GetCustomModifiers()
727                         {
728                                 return index == -1
729                                         ? method.signature.GetReturnTypeCustomModifiers(method)
730                                         : method.signature.GetParameterCustomModifiers(method, index);
731                         }
732
733                         public override MemberInfo Member
734                         {
735                                 get { return method; }
736                         }
737
738                         public override int MetadataToken
739                         {
740                                 get { return Forwarder.MetadataToken; }
741                         }
742
743                         internal override Module Module
744                         {
745                                 get { return method.Module; }
746                         }
747
748                         internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
749                         {
750                                 return Forwarder.GetCustomAttributesData(attributeType);
751                         }
752
753                         public override string ToString()
754                         {
755                                 return Forwarder.ToString();
756                         }
757                 }
758
759                 public override ParameterInfo[] GetParameters()
760                 {
761                         ParameterInfo[] parameters = new ParameterInfo[signature.GetParameterCount()];
762                         for (int i = 0; i < parameters.Length; i++)
763                         {
764                                 parameters[i] = new ParameterInfoImpl(this, i);
765                         }
766                         return parameters;
767                 }
768
769                 public override MethodAttributes Attributes
770                 {
771                         get { return Forwarder.Attributes; }
772                 }
773
774                 public override MethodImplAttributes GetMethodImplementationFlags()
775                 {
776                         return Forwarder.GetMethodImplementationFlags();
777                 }
778
779                 public override MethodBody GetMethodBody()
780                 {
781                         return Forwarder.GetMethodBody();
782                 }
783
784                 public override int __MethodRVA
785                 {
786                         get { return Forwarder.__MethodRVA; }
787                 }
788
789                 public override CallingConventions CallingConvention
790                 {
791                         get { return signature.CallingConvention; }
792                 }
793
794                 internal override int ImportTo(IKVM.Reflection.Emit.ModuleBuilder module)
795                 {
796                         MethodInfo method = TryGetForwarder();
797                         if (method != null)
798                         {
799                                 return method.ImportTo(module);
800                         }
801                         return module.ImportMethodOrField(declaringType, this.Name, this.MethodSignature);
802                 }
803
804                 public override string Name
805                 {
806                         get { return name; }
807                 }
808
809                 public override Type DeclaringType
810                 {
811                         get { return declaringType.IsModulePseudoType ? null : declaringType; }
812                 }
813
814                 public override Module Module
815                 {
816                         get { return declaringType.Module; }
817                 }
818
819                 public override bool Equals(object obj)
820                 {
821                         MissingMethod other = obj as MissingMethod;
822                         return other != null
823                                 && other.declaringType == declaringType
824                                 && other.name == name
825                                 && other.signature.Equals(signature);
826                 }
827
828                 public override int GetHashCode()
829                 {
830                         return declaringType.GetHashCode() ^ name.GetHashCode() ^ signature.GetHashCode();
831                 }
832
833                 internal override MethodBase BindTypeParameters(Type type)
834                 {
835                         MethodInfo forwarder = TryGetForwarder();
836                         if (forwarder != null)
837                         {
838                                 return forwarder.BindTypeParameters(type);
839                         }
840                         return new GenericMethodInstance(type, this, null);
841                 }
842
843                 public override bool ContainsGenericParameters
844                 {
845                         get { return Forwarder.ContainsGenericParameters; }
846                 }
847
848                 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
849                 {
850                         return Forwarder.GetCustomAttributesData(attributeType);
851                 }
852
853                 public override Type[] GetGenericArguments()
854                 {
855                         MethodInfo method = TryGetForwarder();
856                         if (method != null)
857                         {
858                                 return Forwarder.GetGenericArguments();
859                         }
860                         if (typeArgs == null)
861                         {
862                                 typeArgs = new Type[signature.GenericParameterCount];
863                                 for (int i = 0; i < typeArgs.Length; i++)
864                                 {
865                                         typeArgs[i] = new MissingTypeParameter(this, i);
866                                 }
867                         }
868                         return Util.Copy(typeArgs);
869                 }
870
871                 internal override Type GetGenericMethodArgument(int index)
872                 {
873                         return GetGenericArguments()[index];
874                 }
875
876                 internal override int GetGenericMethodArgumentCount()
877                 {
878                         return Forwarder.GetGenericMethodArgumentCount();
879                 }
880
881                 public override MethodInfo GetGenericMethodDefinition()
882                 {
883                         return Forwarder.GetGenericMethodDefinition();
884                 }
885
886                 internal override MethodInfo GetMethodOnTypeDefinition()
887                 {
888                         return Forwarder.GetMethodOnTypeDefinition();
889                 }
890
891                 internal override bool HasThis
892                 {
893                         get { return (signature.CallingConvention & (CallingConventions.HasThis | CallingConventions.ExplicitThis)) == CallingConventions.HasThis; }
894                 }
895
896                 public override bool IsGenericMethod
897                 {
898                         get { return IsGenericMethodDefinition; }
899                 }
900
901                 public override bool IsGenericMethodDefinition
902                 {
903                         get { return signature.GenericParameterCount != 0; }
904                 }
905
906                 public override MethodInfo MakeGenericMethod(params Type[] typeArguments)
907                 {
908                         MethodInfo method = TryGetForwarder();
909                         if (method != null)
910                         {
911                                 return method.MakeGenericMethod(typeArguments);
912                         }
913                         return new GenericMethodInstance(declaringType, this, typeArguments);
914                 }
915
916                 public override int MetadataToken
917                 {
918                         get { return Forwarder.MetadataToken; }
919                 }
920         }
921
922         sealed class MissingField : FieldInfo
923         {
924                 private readonly Type declaringType;
925                 private readonly string name;
926                 private readonly FieldSignature signature;
927                 private FieldInfo forwarder;
928
929                 internal MissingField(Type declaringType, string name, FieldSignature signature)
930                 {
931                         this.declaringType = declaringType;
932                         this.name = name;
933                         this.signature = signature;
934                 }
935
936                 private FieldInfo Forwarder
937                 {
938                         get
939                         {
940                                 FieldInfo field = TryGetForwarder();
941                                 if (field == null)
942                                 {
943                                         throw new MissingMemberException(this);
944                                 }
945                                 return field;
946                         }
947                 }
948
949                 private FieldInfo TryGetForwarder()
950                 {
951                         if (forwarder == null && !declaringType.__IsMissing)
952                         {
953                                 forwarder = declaringType.FindField(name, signature);
954                         }
955                         return forwarder;
956                 }
957
958                 public override bool __IsMissing
959                 {
960                         get { return TryGetForwarder() == null; }
961                 }
962
963                 public override FieldAttributes Attributes
964                 {
965                         get { return Forwarder.Attributes; }
966                 }
967
968                 public override void __GetDataFromRVA(byte[] data, int offset, int length)
969                 {
970                         Forwarder.__GetDataFromRVA(data, offset, length);
971                 }
972
973                 public override int __FieldRVA
974                 {
975                         get { return Forwarder.__FieldRVA; }
976                 }
977
978                 public override object GetRawConstantValue()
979                 {
980                         return Forwarder.GetRawConstantValue();
981                 }
982
983                 internal override FieldSignature FieldSignature
984                 {
985                         get { return signature; }
986                 }
987
988                 internal override int ImportTo(IKVM.Reflection.Emit.ModuleBuilder module)
989                 {
990                         FieldInfo field = TryGetForwarder();
991                         if (field != null)
992                         {
993                                 return field.ImportTo(module);
994                         }
995                         return module.ImportMethodOrField(declaringType, this.Name, this.FieldSignature);
996                 }
997
998                 public override string Name
999                 {
1000                         get { return name; }
1001                 }
1002
1003                 public override Type DeclaringType
1004                 {
1005                         get { return declaringType.IsModulePseudoType ? null : declaringType; }
1006                 }
1007
1008                 public override Module Module
1009                 {
1010                         get { return declaringType.Module; }
1011                 }
1012
1013                 internal override FieldInfo BindTypeParameters(Type type)
1014                 {
1015                         FieldInfo forwarder = TryGetForwarder();
1016                         if (forwarder != null)
1017                         {
1018                                 return forwarder.BindTypeParameters(type);
1019                         }
1020                         return new GenericFieldInstance(type, this);
1021                 }
1022
1023                 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
1024                 {
1025                         return Forwarder.GetCustomAttributesData(attributeType);
1026                 }
1027
1028                 public override int MetadataToken
1029                 {
1030                         get { return Forwarder.MetadataToken; }
1031                 }
1032
1033                 public override bool Equals(object obj)
1034                 {
1035                         MissingField other = obj as MissingField;
1036                         return other != null
1037                                 && other.declaringType == declaringType
1038                                 && other.name == name
1039                                 && other.signature.Equals(signature);
1040                 }
1041
1042                 public override int GetHashCode()
1043                 {
1044                         return declaringType.GetHashCode() ^ name.GetHashCode() ^ signature.GetHashCode();
1045                 }
1046
1047                 public override string ToString()
1048                 {
1049                         return this.FieldType.Name + " " + this.Name;
1050                 }
1051         }
1052
1053         // NOTE this is currently only used by CustomAttributeData (because there is no other way to refer to a property)
1054         sealed class MissingProperty : PropertyInfo
1055         {
1056                 private readonly Type declaringType;
1057                 private readonly string name;
1058                 private readonly PropertySignature signature;
1059
1060                 internal MissingProperty(Type declaringType, string name, PropertySignature signature)
1061                 {
1062                         this.declaringType = declaringType;
1063                         this.name = name;
1064                         this.signature = signature;
1065                 }
1066
1067                 public override PropertyAttributes Attributes
1068                 {
1069                         get { throw new MissingMemberException(this); }
1070                 }
1071
1072                 public override bool CanRead
1073                 {
1074                         get { throw new MissingMemberException(this); }
1075                 }
1076
1077                 public override bool CanWrite
1078                 {
1079                         get { throw new MissingMemberException(this); }
1080                 }
1081
1082                 public override MethodInfo GetGetMethod(bool nonPublic)
1083                 {
1084                         throw new MissingMemberException(this);
1085                 }
1086
1087                 public override MethodInfo GetSetMethod(bool nonPublic)
1088                 {
1089                         throw new MissingMemberException(this);
1090                 }
1091
1092                 public override MethodInfo[] GetAccessors(bool nonPublic)
1093                 {
1094                         throw new MissingMemberException(this);
1095                 }
1096
1097                 public override object GetRawConstantValue()
1098                 {
1099                         throw new MissingMemberException(this);
1100                 }
1101
1102                 internal override bool IsPublic
1103                 {
1104                         get { throw new MissingMemberException(this); }
1105                 }
1106
1107                 internal override bool IsNonPrivate
1108                 {
1109                         get { throw new MissingMemberException(this); }
1110                 }
1111
1112                 internal override bool IsStatic
1113                 {
1114                         get { throw new MissingMemberException(this); }
1115                 }
1116
1117                 internal override PropertySignature PropertySignature
1118                 {
1119                         get { return signature; }
1120                 }
1121
1122                 public override string Name
1123                 {
1124                         get { return name; }
1125                 }
1126
1127                 public override Type DeclaringType
1128                 {
1129                         get { return declaringType; }
1130                 }
1131
1132                 public override Module Module
1133                 {
1134                         get { return declaringType.Module; }
1135                 }
1136         }
1137 }