2 Copyright (C) 2011 Jeroen Frijters
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.
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:
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.
25 using System.Collections.Generic;
26 using System.Runtime.InteropServices;
28 namespace IKVM.Reflection
31 public sealed class MissingAssemblyException : InvalidOperationException
34 private readonly MissingAssembly assembly;
36 internal MissingAssemblyException(MissingAssembly assembly)
37 : base("Assembly '" + assembly.FullName + "' is a missing assembly and does not support the requested operation.")
39 this.assembly = assembly;
42 private MissingAssemblyException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
47 public Assembly Assembly
49 get { return assembly; }
54 public sealed class MissingModuleException : InvalidOperationException
57 private readonly MissingModule module;
59 internal MissingModuleException(MissingModule module)
60 : base("Module from missing assembly '" + module.Assembly.FullName + "' does not support the requested operation.")
65 private MissingModuleException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
72 get { return module; }
77 public sealed class MissingMemberException : InvalidOperationException
80 private readonly MemberInfo member;
82 internal MissingMemberException(MemberInfo member)
83 : base("Member '" + member + "' is a missing member and does not support the requested operation.")
88 private MissingMemberException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
93 public MemberInfo MemberInfo
95 get { return member; }
99 public struct MissingGenericMethodBuilder
101 private readonly MissingMethod method;
103 public MissingGenericMethodBuilder(Type declaringType, CallingConventions callingConvention, string name, int genericParameterCount)
105 method = new MissingMethod(declaringType, name, new MethodSignature(null, null, null, callingConvention, genericParameterCount));
108 public Type[] GetGenericArguments()
110 return method.GetGenericArguments();
113 public void SetSignature(Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
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);
123 public MethodInfo Finish()
129 sealed class MissingAssembly : Assembly
131 private readonly MissingModule module;
132 private readonly string name;
134 internal MissingAssembly(Universe universe, string name)
137 module = new MissingModule(this);
141 public override Type[] GetTypes()
143 throw new MissingAssemblyException(this);
146 public override string FullName
151 public override AssemblyName GetName()
153 return new AssemblyName(name);
156 public override string ImageRuntimeVersion
158 get { throw new MissingAssemblyException(this); }
161 public override Module ManifestModule
163 get { return module; }
166 public override MethodInfo EntryPoint
168 get { throw new MissingAssemblyException(this); }
171 public override string Location
173 get { throw new MissingAssemblyException(this); }
176 public override AssemblyName[] GetReferencedAssemblies()
178 throw new MissingAssemblyException(this);
181 public override Module[] GetModules(bool getResourceModules)
183 throw new MissingAssemblyException(this);
186 public override Module[] GetLoadedModules(bool getResourceModules)
188 throw new MissingAssemblyException(this);
191 public override Module GetModule(string name)
193 throw new MissingAssemblyException(this);
196 public override string[] GetManifestResourceNames()
198 throw new MissingAssemblyException(this);
201 public override ManifestResourceInfo GetManifestResourceInfo(string resourceName)
203 throw new MissingAssemblyException(this);
206 public override System.IO.Stream GetManifestResourceStream(string resourceName)
208 throw new MissingAssemblyException(this);
211 public override bool __IsMissing
216 internal override Type FindType(TypeName typeName)
221 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
223 throw new MissingAssemblyException(this);
227 sealed class MissingModule : NonPEModule
229 private readonly MissingAssembly assembly;
231 internal MissingModule(MissingAssembly assembly)
232 : base(assembly.universe)
234 this.assembly = assembly;
237 public override int MDStreamVersion
239 get { throw new MissingModuleException(this); }
242 public override Assembly Assembly
244 get { return assembly; }
247 public override string FullyQualifiedName
249 get { throw new MissingModuleException(this); }
252 public override string Name
254 get { throw new MissingModuleException(this); }
257 public override Guid ModuleVersionId
259 get { throw new MissingModuleException(this); }
262 public override string ScopeName
264 get { throw new MissingModuleException(this); }
267 internal override Type FindType(TypeName typeName)
272 internal override void GetTypesImpl(System.Collections.Generic.List<Type> list)
274 throw new MissingModuleException(this);
277 public override void __GetDataDirectoryEntry(int index, out int rva, out int length)
279 throw new MissingModuleException(this);
282 public override IList<CustomAttributeData> __GetPlaceholderAssemblyCustomAttributes(bool multiple, bool security)
284 throw new MissingModuleException(this);
287 public override long __RelativeVirtualAddressToFileOffset(int rva)
289 throw new MissingModuleException(this);
292 public override __StandAloneMethodSig __ResolveStandAloneMethodSig(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
294 throw new MissingModuleException(this);
297 public override int __Subsystem
299 get { throw new MissingModuleException(this); }
302 internal override void ExportTypes(int fileToken, IKVM.Reflection.Emit.ModuleBuilder manifestModule)
304 throw new MissingModuleException(this);
307 public override void GetPEKind(out PortableExecutableKinds peKind, out ImageFileMachine machine)
309 throw new MissingModuleException(this);
312 public override bool __IsMissing
317 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
319 throw new MissingModuleException(this);
322 protected override Exception InvalidOperationException()
324 return new MissingModuleException(this);
327 protected override Exception NotSupportedException()
329 return new MissingModuleException(this);
332 protected override Exception ArgumentOutOfRangeException()
334 return new MissingModuleException(this);
338 sealed class MissingType : Type
340 private readonly Module module;
341 private readonly Type declaringType;
342 private readonly string ns;
343 private readonly string name;
344 private Type[] typeArgs;
347 internal MissingType(Module module, Type declaringType, string ns, string name)
349 this.module = module;
350 this.declaringType = declaringType;
353 MarkEnumOrValueType(ns, name);
356 internal override MethodBase FindMethod(string name, MethodSignature signature)
358 MethodInfo method = new MissingMethod(this, name, signature);
361 return new ConstructorInfoImpl(method);
366 internal override FieldInfo FindField(string name, FieldSignature signature)
368 return new MissingField(this, name, signature);
371 internal override Type FindNestedType(TypeName name)
376 public override bool __IsMissing
381 public override Type DeclaringType
383 get { return declaringType; }
386 public override string __Name
391 public override string __Namespace
396 public override string Name
398 get { return TypeNameParser.Escape(name); }
401 public override string FullName
403 get { return GetFullName(); }
406 public override Module Module
408 get { return module; }
411 public override bool IsValueType
415 switch (typeFlags & (TypeFlags.ValueType | TypeFlags.NotValueType))
417 case TypeFlags.ValueType:
419 case TypeFlags.NotValueType:
422 throw new MissingMemberException(this);
427 public override Type BaseType
429 get { throw new MissingMemberException(this); }
432 public override TypeAttributes Attributes
434 get { throw new MissingMemberException(this); }
437 public override Type[] __GetDeclaredTypes()
439 throw new MissingMemberException(this);
442 public override Type[] __GetDeclaredInterfaces()
444 throw new MissingMemberException(this);
447 public override MethodBase[] __GetDeclaredMethods()
449 throw new MissingMemberException(this);
452 public override __MethodImplMap __GetMethodImplMap()
454 throw new MissingMemberException(this);
457 public override FieldInfo[] __GetDeclaredFields()
459 throw new MissingMemberException(this);
462 public override EventInfo[] __GetDeclaredEvents()
464 throw new MissingMemberException(this);
467 public override PropertyInfo[] __GetDeclaredProperties()
469 throw new MissingMemberException(this);
472 public override Type[] __GetRequiredCustomModifiers()
474 throw new MissingMemberException(this);
477 public override Type[] __GetOptionalCustomModifiers()
479 throw new MissingMemberException(this);
482 public override Type[] GetGenericArguments()
484 throw new MissingMemberException(this);
487 public override Type[][] __GetGenericArgumentsRequiredCustomModifiers()
489 throw new MissingMemberException(this);
492 public override Type[][] __GetGenericArgumentsOptionalCustomModifiers()
494 throw new MissingMemberException(this);
497 public override StructLayoutAttribute StructLayoutAttribute
499 get { throw new MissingMemberException(this); }
502 public override bool IsGenericType
504 get { throw new MissingMemberException(this); }
507 public override bool IsGenericTypeDefinition
509 get { throw new MissingMemberException(this); }
512 internal override Type GetGenericTypeArgument(int index)
514 if (typeArgs == null)
516 typeArgs = new Type[index + 1];
518 else if (typeArgs.Length <= index)
520 Array.Resize(ref typeArgs, index + 1);
522 return typeArgs[index] ?? (typeArgs[index] = new MissingTypeParameter(this, index));
525 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
527 throw new MissingMemberException(this);
530 internal override Type BindTypeParameters(IGenericBinder binder)
535 internal int GetMetadataTokenForMissing()
540 internal override Type SetMetadataTokenForMissing(int token)
547 sealed class MissingTypeParameter : IKVM.Reflection.Reader.TypeParameterType
549 private readonly MemberInfo owner;
550 private readonly int index;
552 internal MissingTypeParameter(MemberInfo owner, int index)
558 public override Module Module
560 get { return owner.Module; }
563 public override string Name
568 public override int GenericParameterPosition
570 get { return index; }
573 public override MethodBase DeclaringMethod
575 get { return owner as MethodBase; }
578 public override Type DeclaringType
580 get { return owner as Type; }
584 sealed class MissingMethod : MethodInfo
586 private readonly Type declaringType;
587 private readonly string name;
588 internal MethodSignature signature;
589 private MethodInfo forwarder;
590 private Type[] typeArgs;
592 internal MissingMethod(Type declaringType, string name, MethodSignature signature)
594 this.declaringType = declaringType;
596 this.signature = signature;
599 private MethodInfo Forwarder
603 MethodInfo method = TryGetForwarder();
606 throw new MissingMemberException(this);
612 private MethodInfo TryGetForwarder()
614 if (forwarder == null && !declaringType.__IsMissing)
616 MethodBase mb = declaringType.FindMethod(name, signature);
617 ConstructorInfo ci = mb as ConstructorInfo;
620 forwarder = ci.GetMethodInfo();
624 forwarder = (MethodInfo)mb;
630 public override bool __IsMissing
632 get { return TryGetForwarder() == null; }
635 public override Type ReturnType
637 get { return signature.GetReturnType(this); }
640 public override ParameterInfo ReturnParameter
642 get { return new ParameterInfoImpl(this, -1); }
645 internal override MethodSignature MethodSignature
647 get { return signature; }
650 internal override int ParameterCount
652 get { return signature.GetParameterCount(); }
655 private sealed class ParameterInfoImpl : ParameterInfo
657 private readonly MissingMethod method;
658 private readonly int index;
660 internal ParameterInfoImpl(MissingMethod method, int index)
662 this.method = method;
666 private ParameterInfo Forwarder
668 get { return index == -1 ? method.Forwarder.ReturnParameter : method.Forwarder.GetParameters()[index]; }
671 public override string Name
673 get { return Forwarder.Name; }
676 public override Type ParameterType
678 get { return index == -1 ? method.signature.GetReturnType(method) : method.signature.GetParameterType(method, index); }
681 public override ParameterAttributes Attributes
683 get { return Forwarder.Attributes; }
686 public override int Position
688 get { return index; }
691 public override object RawDefaultValue
693 get { return Forwarder.RawDefaultValue; }
696 public override Type[] GetOptionalCustomModifiers()
700 return Util.Copy(method.signature.GetReturnTypeOptionalCustomModifiers(method));
702 return Util.Copy(method.signature.GetParameterOptionalCustomModifiers(method, index));
705 public override Type[] GetRequiredCustomModifiers()
709 return Util.Copy(method.signature.GetReturnTypeRequiredCustomModifiers(method));
711 return Util.Copy(method.signature.GetParameterRequiredCustomModifiers(method, index));
714 public override MemberInfo Member
716 get { return method; }
719 public override int MetadataToken
721 get { return Forwarder.MetadataToken; }
724 internal override Module Module
726 get { return method.Module; }
729 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
731 return Forwarder.GetCustomAttributesData(attributeType);
734 public override string ToString()
736 return Forwarder.ToString();
740 public override ParameterInfo[] GetParameters()
742 ParameterInfo[] parameters = new ParameterInfo[signature.GetParameterCount()];
743 for (int i = 0; i < parameters.Length; i++)
745 parameters[i] = new ParameterInfoImpl(this, i);
750 public override MethodAttributes Attributes
752 get { return Forwarder.Attributes; }
755 public override MethodImplAttributes GetMethodImplementationFlags()
757 return Forwarder.GetMethodImplementationFlags();
760 public override MethodBody GetMethodBody()
762 return Forwarder.GetMethodBody();
765 public override CallingConventions CallingConvention
767 get { return signature.CallingConvention; }
770 internal override int ImportTo(IKVM.Reflection.Emit.ModuleBuilder module)
772 MethodInfo method = TryGetForwarder();
775 return method.ImportTo(module);
777 return module.ImportMethodOrField(declaringType, this.Name, this.MethodSignature);
780 public override string Name
785 public override Type DeclaringType
787 get { return declaringType.IsModulePseudoType ? null : declaringType; }
790 public override Module Module
792 get { return declaringType.Module; }
795 public override bool Equals(object obj)
797 MissingMethod other = obj as MissingMethod;
799 && other.declaringType == declaringType
800 && other.name == name
801 && other.signature.Equals(signature);
804 public override int GetHashCode()
806 return declaringType.GetHashCode() ^ name.GetHashCode() ^ signature.GetHashCode();
809 internal override MethodBase BindTypeParameters(Type type)
811 MethodInfo forwarder = TryGetForwarder();
812 if (forwarder != null)
814 return forwarder.BindTypeParameters(type);
816 return new GenericMethodInstance(type, this, null);
819 public override bool ContainsGenericParameters
821 get { return Forwarder.ContainsGenericParameters; }
824 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
826 return Forwarder.GetCustomAttributesData(attributeType);
829 public override Type[] GetGenericArguments()
831 MethodInfo method = TryGetForwarder();
834 return Forwarder.GetGenericArguments();
836 if (typeArgs == null)
838 typeArgs = new Type[signature.GenericParameterCount];
839 for (int i = 0; i < typeArgs.Length; i++)
841 typeArgs[i] = new MissingTypeParameter(this, i);
844 return Util.Copy(typeArgs);
847 internal override Type GetGenericMethodArgument(int index)
849 return GetGenericArguments()[index];
852 internal override int GetGenericMethodArgumentCount()
854 return Forwarder.GetGenericMethodArgumentCount();
857 public override MethodInfo GetGenericMethodDefinition()
859 return Forwarder.GetGenericMethodDefinition();
862 internal override MethodInfo GetMethodOnTypeDefinition()
864 return Forwarder.GetMethodOnTypeDefinition();
867 internal override bool HasThis
869 get { return (signature.CallingConvention & (CallingConventions.HasThis | CallingConventions.ExplicitThis)) == CallingConventions.HasThis; }
872 public override bool IsGenericMethod
874 get { return IsGenericMethodDefinition; }
877 public override bool IsGenericMethodDefinition
879 get { return signature.GenericParameterCount != 0; }
882 public override MethodInfo MakeGenericMethod(params Type[] typeArguments)
884 MethodInfo method = TryGetForwarder();
887 return method.MakeGenericMethod(typeArguments);
889 return new GenericMethodInstance(declaringType, this, typeArguments);
892 public override int MetadataToken
894 get { return Forwarder.MetadataToken; }
898 sealed class MissingField : FieldInfo
900 private readonly Type declaringType;
901 private readonly string name;
902 private readonly FieldSignature signature;
903 private FieldInfo forwarder;
905 internal MissingField(Type declaringType, string name, FieldSignature signature)
907 this.declaringType = declaringType;
909 this.signature = signature;
912 private FieldInfo Forwarder
916 FieldInfo field = TryGetForwarder();
919 throw new MissingMemberException(this);
925 private FieldInfo TryGetForwarder()
927 if (forwarder == null && !declaringType.__IsMissing)
929 forwarder = declaringType.FindField(name, signature);
934 public override bool __IsMissing
936 get { return TryGetForwarder() == null; }
939 public override FieldAttributes Attributes
941 get { return Forwarder.Attributes; }
944 public override void __GetDataFromRVA(byte[] data, int offset, int length)
946 Forwarder.__GetDataFromRVA(data, offset, length);
949 public override int __FieldRVA
951 get { return Forwarder.__FieldRVA; }
954 public override object GetRawConstantValue()
956 return Forwarder.GetRawConstantValue();
959 internal override FieldSignature FieldSignature
961 get { return signature; }
964 internal override int ImportTo(IKVM.Reflection.Emit.ModuleBuilder module)
966 FieldInfo field = TryGetForwarder();
969 return field.ImportTo(module);
971 return module.ImportMethodOrField(declaringType, this.Name, this.FieldSignature);
974 public override string Name
979 public override Type DeclaringType
981 get { return declaringType.IsModulePseudoType ? null : declaringType; }
984 public override Module Module
986 get { return declaringType.Module; }
989 internal override FieldInfo BindTypeParameters(Type type)
991 FieldInfo forwarder = TryGetForwarder();
992 if (forwarder != null)
994 return forwarder.BindTypeParameters(type);
996 return new GenericFieldInstance(type, this);
999 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
1001 return Forwarder.GetCustomAttributesData(attributeType);
1004 public override int MetadataToken
1006 get { return Forwarder.MetadataToken; }
1009 public override bool Equals(object obj)
1011 MissingField other = obj as MissingField;
1012 return other != null
1013 && other.declaringType == declaringType
1014 && other.name == name
1015 && other.signature.Equals(signature);
1018 public override int GetHashCode()
1020 return declaringType.GetHashCode() ^ name.GetHashCode() ^ signature.GetHashCode();
1023 public override string ToString()
1025 return this.FieldType.Name + " " + this.Name;
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
1032 private readonly Type declaringType;
1033 private readonly string name;
1034 private readonly PropertySignature signature;
1036 internal MissingProperty(Type declaringType, string name, PropertySignature signature)
1038 this.declaringType = declaringType;
1040 this.signature = signature;
1043 public override PropertyAttributes Attributes
1045 get { throw new MissingMemberException(this); }
1048 public override bool CanRead
1050 get { throw new MissingMemberException(this); }
1053 public override bool CanWrite
1055 get { throw new MissingMemberException(this); }
1058 public override MethodInfo GetGetMethod(bool nonPublic)
1060 throw new MissingMemberException(this);
1063 public override MethodInfo GetSetMethod(bool nonPublic)
1065 throw new MissingMemberException(this);
1068 public override MethodInfo[] GetAccessors(bool nonPublic)
1070 throw new MissingMemberException(this);
1073 public override object GetRawConstantValue()
1075 throw new MissingMemberException(this);
1078 internal override bool IsPublic
1080 get { throw new MissingMemberException(this); }
1083 internal override bool IsStatic
1085 get { throw new MissingMemberException(this); }
1088 internal override PropertySignature PropertySignature
1090 get { return signature; }
1093 public override string Name
1095 get { return name; }
1098 public override Type DeclaringType
1100 get { return declaringType; }
1103 public override Module Module
1105 get { return declaringType.Module; }