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                         MarkEnumOrValueType(ns, name);
354                 }
355
356                 internal override MethodBase FindMethod(string name, MethodSignature signature)
357                 {
358                         MethodInfo method = new MissingMethod(this, name, signature);
359                         if (name == ".ctor")
360                         {
361                                 return new ConstructorInfoImpl(method);
362                         }
363                         return method;
364                 }
365
366                 internal override FieldInfo FindField(string name, FieldSignature signature)
367                 {
368                         return new MissingField(this, name, signature);
369                 }
370
371                 internal override Type FindNestedType(TypeName name)
372                 {
373                         return null;
374                 }
375
376                 public override bool __IsMissing
377                 {
378                         get { return true; }
379                 }
380
381                 public override Type DeclaringType
382                 {
383                         get { return declaringType; }
384                 }
385
386                 public override string __Name
387                 {
388                         get { return name; }
389                 }
390
391                 public override string __Namespace
392                 {
393                         get { return ns; }
394                 }
395
396                 public override string Name
397                 {
398                         get { return TypeNameParser.Escape(name); }
399                 }
400
401                 public override string FullName
402                 {
403                         get { return GetFullName(); }
404                 }
405
406                 public override Module Module
407                 {
408                         get { return module; }
409                 }
410
411                 public override bool IsValueType
412                 {
413                         get
414                         {
415                                 switch (typeFlags & (TypeFlags.ValueType | TypeFlags.NotValueType))
416                                 {
417                                         case TypeFlags.ValueType:
418                                                 return true;
419                                         case TypeFlags.NotValueType:
420                                                 return false;
421                                         default:
422                                                 throw new MissingMemberException(this);
423                                 }
424                         }
425                 }
426
427                 public override Type BaseType
428                 {
429                         get { throw new MissingMemberException(this); }
430                 }
431
432                 public override TypeAttributes Attributes
433                 {
434                         get { throw new MissingMemberException(this); }
435                 }
436
437                 public override Type[] __GetDeclaredTypes()
438                 {
439                         throw new MissingMemberException(this);
440                 }
441
442                 public override Type[] __GetDeclaredInterfaces()
443                 {
444                         throw new MissingMemberException(this);
445                 }
446
447                 public override MethodBase[] __GetDeclaredMethods()
448                 {
449                         throw new MissingMemberException(this);
450                 }
451
452                 public override __MethodImplMap __GetMethodImplMap()
453                 {
454                         throw new MissingMemberException(this);
455                 }
456
457                 public override FieldInfo[] __GetDeclaredFields()
458                 {
459                         throw new MissingMemberException(this);
460                 }
461
462                 public override EventInfo[] __GetDeclaredEvents()
463                 {
464                         throw new MissingMemberException(this);
465                 }
466
467                 public override PropertyInfo[] __GetDeclaredProperties()
468                 {
469                         throw new MissingMemberException(this);
470                 }
471
472                 public override Type[] __GetRequiredCustomModifiers()
473                 {
474                         throw new MissingMemberException(this);
475                 }
476
477                 public override Type[] __GetOptionalCustomModifiers()
478                 {
479                         throw new MissingMemberException(this);
480                 }
481
482                 public override Type[] GetGenericArguments()
483                 {
484                         throw new MissingMemberException(this);
485                 }
486
487                 public override Type[][] __GetGenericArgumentsRequiredCustomModifiers()
488                 {
489                         throw new MissingMemberException(this);
490                 }
491
492                 public override Type[][] __GetGenericArgumentsOptionalCustomModifiers()
493                 {
494                         throw new MissingMemberException(this);
495                 }
496
497                 public override StructLayoutAttribute StructLayoutAttribute
498                 {
499                         get { throw new MissingMemberException(this); }
500                 }
501
502                 public override bool IsGenericType
503                 {
504                         get { throw new MissingMemberException(this); }
505                 }
506
507                 public override bool IsGenericTypeDefinition
508                 {
509                         get { throw new MissingMemberException(this); }
510                 }
511
512                 internal override Type GetGenericTypeArgument(int index)
513                 {
514                         if (typeArgs == null)
515                         {
516                                 typeArgs = new Type[index + 1];
517                         }
518                         else if (typeArgs.Length <= index)
519                         {
520                                 Array.Resize(ref typeArgs, index + 1);
521                         }
522                         return typeArgs[index] ?? (typeArgs[index] = new MissingTypeParameter(this, index));
523                 }
524
525                 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
526                 {
527                         throw new MissingMemberException(this);
528                 }
529
530                 internal override Type BindTypeParameters(IGenericBinder binder)
531                 {
532                         return this;
533                 }
534
535                 internal int GetMetadataTokenForMissing()
536                 {
537                         return token;
538                 }
539
540                 internal override Type SetMetadataTokenForMissing(int token)
541                 {
542                         this.token = token;
543                         return this;
544                 }
545         }
546
547         sealed class MissingTypeParameter : IKVM.Reflection.Reader.TypeParameterType
548         {
549                 private readonly MemberInfo owner;
550                 private readonly int index;
551
552                 internal MissingTypeParameter(MemberInfo owner, int index)
553                 {
554                         this.owner = owner;
555                         this.index = index;
556                 }
557
558                 public override Module Module
559                 {
560                         get { return owner.Module; }
561                 }
562
563                 public override string Name
564                 {
565                         get { return null; }
566                 }
567
568                 public override int GenericParameterPosition
569                 {
570                         get { return index; }
571                 }
572
573                 public override MethodBase DeclaringMethod
574                 {
575                         get { return owner as MethodBase; }
576                 }
577
578                 public override Type DeclaringType
579                 {
580                         get { return owner as Type; }
581                 }
582         }
583
584         sealed class MissingMethod : MethodInfo
585         {
586                 private readonly Type declaringType;
587                 private readonly string name;
588                 internal MethodSignature signature;
589                 private MethodInfo forwarder;
590                 private Type[] typeArgs;
591
592                 internal MissingMethod(Type declaringType, string name, MethodSignature signature)
593                 {
594                         this.declaringType = declaringType;
595                         this.name = name;
596                         this.signature = signature;
597                 }
598
599                 private MethodInfo Forwarder
600                 {
601                         get
602                         {
603                                 MethodInfo method = TryGetForwarder();
604                                 if (method == null)
605                                 {
606                                         throw new MissingMemberException(this);
607                                 }
608                                 return method;
609                         }
610                 }
611
612                 private MethodInfo TryGetForwarder()
613                 {
614                         if (forwarder == null && !declaringType.__IsMissing)
615                         {
616                                 MethodBase mb = declaringType.FindMethod(name, signature);
617                                 ConstructorInfo ci = mb as ConstructorInfo;
618                                 if (ci != null)
619                                 {
620                                         forwarder = ci.GetMethodInfo();
621                                 }
622                                 else
623                                 {
624                                         forwarder = (MethodInfo)mb;
625                                 }
626                         }
627                         return forwarder;
628                 }
629
630                 public override bool __IsMissing
631                 {
632                         get { return TryGetForwarder() == null; }
633                 }
634
635                 public override Type ReturnType
636                 {
637                         get { return signature.GetReturnType(this); }
638                 }
639
640                 public override ParameterInfo ReturnParameter
641                 {
642                         get { return new ParameterInfoImpl(this, -1); }
643                 }
644
645                 internal override MethodSignature MethodSignature
646                 {
647                         get { return signature; }
648                 }
649
650                 internal override int ParameterCount
651                 {
652                         get { return signature.GetParameterCount(); }
653                 }
654
655                 private sealed class ParameterInfoImpl : ParameterInfo
656                 {
657                         private readonly MissingMethod method;
658                         private readonly int index;
659
660                         internal ParameterInfoImpl(MissingMethod method, int index)
661                         {
662                                 this.method = method;
663                                 this.index = index;
664                         }
665
666                         private ParameterInfo Forwarder
667                         {
668                                 get { return index == -1 ? method.Forwarder.ReturnParameter : method.Forwarder.GetParameters()[index]; }
669                         }
670
671                         public override string Name
672                         {
673                                 get { return Forwarder.Name; }
674                         }
675
676                         public override Type ParameterType
677                         {
678                                 get { return index == -1 ? method.signature.GetReturnType(method) : method.signature.GetParameterType(method, index); }
679                         }
680
681                         public override ParameterAttributes Attributes
682                         {
683                                 get { return Forwarder.Attributes; }
684                         }
685
686                         public override int Position
687                         {
688                                 get { return index; }
689                         }
690
691                         public override object RawDefaultValue
692                         {
693                                 get { return Forwarder.RawDefaultValue; }
694                         }
695
696                         public override Type[] GetOptionalCustomModifiers()
697                         {
698                                 if (index == -1)
699                                 {
700                                         return Util.Copy(method.signature.GetReturnTypeOptionalCustomModifiers(method));
701                                 }
702                                 return Util.Copy(method.signature.GetParameterOptionalCustomModifiers(method, index));
703                         }
704
705                         public override Type[] GetRequiredCustomModifiers()
706                         {
707                                 if (index == -1)
708                                 {
709                                         return Util.Copy(method.signature.GetReturnTypeRequiredCustomModifiers(method));
710                                 }
711                                 return Util.Copy(method.signature.GetParameterRequiredCustomModifiers(method, index));
712                         }
713
714                         public override MemberInfo Member
715                         {
716                                 get { return method; }
717                         }
718
719                         public override int MetadataToken
720                         {
721                                 get { return Forwarder.MetadataToken; }
722                         }
723
724                         internal override Module Module
725                         {
726                                 get { return method.Module; }
727                         }
728
729                         internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
730                         {
731                                 return Forwarder.GetCustomAttributesData(attributeType);
732                         }
733
734                         public override string ToString()
735                         {
736                                 return Forwarder.ToString();
737                         }
738                 }
739
740                 public override ParameterInfo[] GetParameters()
741                 {
742                         ParameterInfo[] parameters = new ParameterInfo[signature.GetParameterCount()];
743                         for (int i = 0; i < parameters.Length; i++)
744                         {
745                                 parameters[i] = new ParameterInfoImpl(this, i);
746                         }
747                         return parameters;
748                 }
749
750                 public override MethodAttributes Attributes
751                 {
752                         get { return Forwarder.Attributes; }
753                 }
754
755                 public override MethodImplAttributes GetMethodImplementationFlags()
756                 {
757                         return Forwarder.GetMethodImplementationFlags();
758                 }
759
760                 public override MethodBody GetMethodBody()
761                 {
762                         return Forwarder.GetMethodBody();
763                 }
764
765                 public override CallingConventions CallingConvention
766                 {
767                         get { return signature.CallingConvention; }
768                 }
769
770                 internal override int ImportTo(IKVM.Reflection.Emit.ModuleBuilder module)
771                 {
772                         MethodInfo method = TryGetForwarder();
773                         if (method != null)
774                         {
775                                 return method.ImportTo(module);
776                         }
777                         return module.ImportMethodOrField(declaringType, this.Name, this.MethodSignature);
778                 }
779
780                 public override string Name
781                 {
782                         get { return name; }
783                 }
784
785                 public override Type DeclaringType
786                 {
787                         get { return declaringType.IsModulePseudoType ? null : declaringType; }
788                 }
789
790                 public override Module Module
791                 {
792                         get { return declaringType.Module; }
793                 }
794
795                 public override bool Equals(object obj)
796                 {
797                         MissingMethod other = obj as MissingMethod;
798                         return other != null
799                                 && other.declaringType == declaringType
800                                 && other.name == name
801                                 && other.signature.Equals(signature);
802                 }
803
804                 public override int GetHashCode()
805                 {
806                         return declaringType.GetHashCode() ^ name.GetHashCode() ^ signature.GetHashCode();
807                 }
808
809                 internal override MethodBase BindTypeParameters(Type type)
810                 {
811                         MethodInfo forwarder = TryGetForwarder();
812                         if (forwarder != null)
813                         {
814                                 return forwarder.BindTypeParameters(type);
815                         }
816                         return new GenericMethodInstance(type, this, null);
817                 }
818
819                 public override bool ContainsGenericParameters
820                 {
821                         get { return Forwarder.ContainsGenericParameters; }
822                 }
823
824                 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
825                 {
826                         return Forwarder.GetCustomAttributesData(attributeType);
827                 }
828
829                 public override Type[] GetGenericArguments()
830                 {
831                         MethodInfo method = TryGetForwarder();
832                         if (method != null)
833                         {
834                                 return Forwarder.GetGenericArguments();
835                         }
836                         if (typeArgs == null)
837                         {
838                                 typeArgs = new Type[signature.GenericParameterCount];
839                                 for (int i = 0; i < typeArgs.Length; i++)
840                                 {
841                                         typeArgs[i] = new MissingTypeParameter(this, i);
842                                 }
843                         }
844                         return Util.Copy(typeArgs);
845                 }
846
847                 internal override Type GetGenericMethodArgument(int index)
848                 {
849                         return GetGenericArguments()[index];
850                 }
851
852                 internal override int GetGenericMethodArgumentCount()
853                 {
854                         return Forwarder.GetGenericMethodArgumentCount();
855                 }
856
857                 public override MethodInfo GetGenericMethodDefinition()
858                 {
859                         return Forwarder.GetGenericMethodDefinition();
860                 }
861
862                 internal override MethodInfo GetMethodOnTypeDefinition()
863                 {
864                         return Forwarder.GetMethodOnTypeDefinition();
865                 }
866
867                 internal override bool HasThis
868                 {
869                         get { return (signature.CallingConvention & (CallingConventions.HasThis | CallingConventions.ExplicitThis)) == CallingConventions.HasThis; }
870                 }
871
872                 public override bool IsGenericMethod
873                 {
874                         get { return IsGenericMethodDefinition; }
875                 }
876
877                 public override bool IsGenericMethodDefinition
878                 {
879                         get { return signature.GenericParameterCount != 0; }
880                 }
881
882                 public override MethodInfo MakeGenericMethod(params Type[] typeArguments)
883                 {
884                         MethodInfo method = TryGetForwarder();
885                         if (method != null)
886                         {
887                                 return method.MakeGenericMethod(typeArguments);
888                         }
889                         return new GenericMethodInstance(declaringType, this, typeArguments);
890                 }
891
892                 public override int MetadataToken
893                 {
894                         get { return Forwarder.MetadataToken; }
895                 }
896         }
897
898         sealed class MissingField : FieldInfo
899         {
900                 private readonly Type declaringType;
901                 private readonly string name;
902                 private readonly FieldSignature signature;
903                 private FieldInfo forwarder;
904
905                 internal MissingField(Type declaringType, string name, FieldSignature signature)
906                 {
907                         this.declaringType = declaringType;
908                         this.name = name;
909                         this.signature = signature;
910                 }
911
912                 private FieldInfo Forwarder
913                 {
914                         get
915                         {
916                                 FieldInfo field = TryGetForwarder();
917                                 if (field == null)
918                                 {
919                                         throw new MissingMemberException(this);
920                                 }
921                                 return field;
922                         }
923                 }
924
925                 private FieldInfo TryGetForwarder()
926                 {
927                         if (forwarder == null && !declaringType.__IsMissing)
928                         {
929                                 forwarder = declaringType.FindField(name, signature);
930                         }
931                         return forwarder;
932                 }
933
934                 public override bool __IsMissing
935                 {
936                         get { return TryGetForwarder() == null; }
937                 }
938
939                 public override FieldAttributes Attributes
940                 {
941                         get { return Forwarder.Attributes; }
942                 }
943
944                 public override void __GetDataFromRVA(byte[] data, int offset, int length)
945                 {
946                         Forwarder.__GetDataFromRVA(data, offset, length);
947                 }
948
949                 public override int __FieldRVA
950                 {
951                         get { return Forwarder.__FieldRVA; }
952                 }
953
954                 public override object GetRawConstantValue()
955                 {
956                         return Forwarder.GetRawConstantValue();
957                 }
958
959                 internal override FieldSignature FieldSignature
960                 {
961                         get { return signature; }
962                 }
963
964                 internal override int ImportTo(IKVM.Reflection.Emit.ModuleBuilder module)
965                 {
966                         FieldInfo field = TryGetForwarder();
967                         if (field != null)
968                         {
969                                 return field.ImportTo(module);
970                         }
971                         return module.ImportMethodOrField(declaringType, this.Name, this.FieldSignature);
972                 }
973
974                 public override string Name
975                 {
976                         get { return name; }
977                 }
978
979                 public override Type DeclaringType
980                 {
981                         get { return declaringType.IsModulePseudoType ? null : declaringType; }
982                 }
983
984                 public override Module Module
985                 {
986                         get { return declaringType.Module; }
987                 }
988
989                 internal override FieldInfo BindTypeParameters(Type type)
990                 {
991                         FieldInfo forwarder = TryGetForwarder();
992                         if (forwarder != null)
993                         {
994                                 return forwarder.BindTypeParameters(type);
995                         }
996                         return new GenericFieldInstance(type, this);
997                 }
998
999                 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
1000                 {
1001                         return Forwarder.GetCustomAttributesData(attributeType);
1002                 }
1003
1004                 public override int MetadataToken
1005                 {
1006                         get { return Forwarder.MetadataToken; }
1007                 }
1008
1009                 public override bool Equals(object obj)
1010                 {
1011                         MissingField other = obj as MissingField;
1012                         return other != null
1013                                 && other.declaringType == declaringType
1014                                 && other.name == name
1015                                 && other.signature.Equals(signature);
1016                 }
1017
1018                 public override int GetHashCode()
1019                 {
1020                         return declaringType.GetHashCode() ^ name.GetHashCode() ^ signature.GetHashCode();
1021                 }
1022
1023                 public override string ToString()
1024                 {
1025                         return this.FieldType.Name + " " + this.Name;
1026                 }
1027         }
1028
1029         // NOTE this is currently only used by CustomAttributeData (because there is no other way to refer to a property)
1030         sealed class MissingProperty : PropertyInfo
1031         {
1032                 private readonly Type declaringType;
1033                 private readonly string name;
1034                 private readonly PropertySignature signature;
1035
1036                 internal MissingProperty(Type declaringType, string name, PropertySignature signature)
1037                 {
1038                         this.declaringType = declaringType;
1039                         this.name = name;
1040                         this.signature = signature;
1041                 }
1042
1043                 public override PropertyAttributes Attributes
1044                 {
1045                         get { throw new MissingMemberException(this); }
1046                 }
1047
1048                 public override bool CanRead
1049                 {
1050                         get { throw new MissingMemberException(this); }
1051                 }
1052
1053                 public override bool CanWrite
1054                 {
1055                         get { throw new MissingMemberException(this); }
1056                 }
1057
1058                 public override MethodInfo GetGetMethod(bool nonPublic)
1059                 {
1060                         throw new MissingMemberException(this);
1061                 }
1062
1063                 public override MethodInfo GetSetMethod(bool nonPublic)
1064                 {
1065                         throw new MissingMemberException(this);
1066                 }
1067
1068                 public override MethodInfo[] GetAccessors(bool nonPublic)
1069                 {
1070                         throw new MissingMemberException(this);
1071                 }
1072
1073                 public override object GetRawConstantValue()
1074                 {
1075                         throw new MissingMemberException(this);
1076                 }
1077
1078                 internal override bool IsPublic
1079                 {
1080                         get { throw new MissingMemberException(this); }
1081                 }
1082
1083                 internal override bool IsStatic
1084                 {
1085                         get { throw new MissingMemberException(this); }
1086                 }
1087
1088                 internal override PropertySignature PropertySignature
1089                 {
1090                         get { return signature; }
1091                 }
1092
1093                 public override string Name
1094                 {
1095                         get { return name; }
1096                 }
1097
1098                 public override Type DeclaringType
1099                 {
1100                         get { return declaringType; }
1101                 }
1102
1103                 public override Module Module
1104                 {
1105                         get { return declaringType.Module; }
1106                 }
1107         }
1108 }