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