2 Copyright (C) 2011-2012 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, new PackedCustomModifiers(), callingConvention, genericParameterCount));
108 public Type[] GetGenericArguments()
110 return method.GetGenericArguments();
113 public void SetSignature(Type returnType, CustomModifiers returnTypeCustomModifiers, Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers)
115 method.signature = new MethodSignature(
116 returnType ?? method.Module.universe.System_Void,
117 Util.Copy(parameterTypes),
118 PackedCustomModifiers.CreateFromExternal(returnTypeCustomModifiers, parameterTypeCustomModifiers, parameterTypes.Length),
119 method.signature.CallingConvention,
120 method.signature.GenericParameterCount);
123 [Obsolete("Please use SetSignature(Type, CustomModifiers, Type[], CustomModifiers[]) instead.")]
124 public void SetSignature(Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
126 method.signature = new MethodSignature(
127 returnType ?? method.Module.universe.System_Void,
128 Util.Copy(parameterTypes),
129 PackedCustomModifiers.CreateFromExternal(returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers, parameterTypeRequiredCustomModifiers, parameterTypes.Length),
130 method.signature.CallingConvention,
131 method.signature.GenericParameterCount);
134 public MethodInfo Finish()
140 sealed class MissingAssembly : Assembly
142 private readonly MissingModule module;
144 internal MissingAssembly(Universe universe, string name)
147 module = new MissingModule(this);
148 this.fullName = name;
151 public override Type[] GetTypes()
153 throw new MissingAssemblyException(this);
156 public override AssemblyName GetName()
158 return new AssemblyName(fullName);
161 public override string ImageRuntimeVersion
163 get { throw new MissingAssemblyException(this); }
166 public override Module ManifestModule
168 get { return module; }
171 public override MethodInfo EntryPoint
173 get { throw new MissingAssemblyException(this); }
176 public override string Location
178 get { throw new MissingAssemblyException(this); }
181 public override AssemblyName[] GetReferencedAssemblies()
183 throw new MissingAssemblyException(this);
186 public override Module[] GetModules(bool getResourceModules)
188 throw new MissingAssemblyException(this);
191 public override Module[] GetLoadedModules(bool getResourceModules)
193 throw new MissingAssemblyException(this);
196 public override Module GetModule(string name)
198 throw new MissingAssemblyException(this);
201 public override string[] GetManifestResourceNames()
203 throw new MissingAssemblyException(this);
206 public override ManifestResourceInfo GetManifestResourceInfo(string resourceName)
208 throw new MissingAssemblyException(this);
211 public override System.IO.Stream GetManifestResourceStream(string resourceName)
213 throw new MissingAssemblyException(this);
216 public override bool __IsMissing
221 internal override Type FindType(TypeName typeName)
226 internal override Type FindTypeIgnoreCase(TypeName lowerCaseName)
231 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
233 throw new MissingAssemblyException(this);
237 sealed class MissingModule : NonPEModule
239 private readonly MissingAssembly assembly;
241 internal MissingModule(MissingAssembly assembly)
242 : base(assembly.universe)
244 this.assembly = assembly;
247 public override int MDStreamVersion
249 get { throw new MissingModuleException(this); }
252 public override Assembly Assembly
254 get { return assembly; }
257 public override string FullyQualifiedName
259 get { throw new MissingModuleException(this); }
262 public override string Name
264 get { throw new MissingModuleException(this); }
267 public override Guid ModuleVersionId
269 get { throw new MissingModuleException(this); }
272 public override string ScopeName
274 get { throw new MissingModuleException(this); }
277 internal override Type FindType(TypeName typeName)
282 internal override Type FindTypeIgnoreCase(TypeName lowerCaseName)
287 internal override void GetTypesImpl(System.Collections.Generic.List<Type> list)
289 throw new MissingModuleException(this);
292 public override void __GetDataDirectoryEntry(int index, out int rva, out int length)
294 throw new MissingModuleException(this);
297 public override IList<CustomAttributeData> __GetPlaceholderAssemblyCustomAttributes(bool multiple, bool security)
299 throw new MissingModuleException(this);
302 public override long __RelativeVirtualAddressToFileOffset(int rva)
304 throw new MissingModuleException(this);
307 public override __StandAloneMethodSig __ResolveStandAloneMethodSig(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
309 throw new MissingModuleException(this);
312 public override int __Subsystem
314 get { throw new MissingModuleException(this); }
317 internal override void ExportTypes(int fileToken, IKVM.Reflection.Emit.ModuleBuilder manifestModule)
319 throw new MissingModuleException(this);
322 public override void GetPEKind(out PortableExecutableKinds peKind, out ImageFileMachine machine)
324 throw new MissingModuleException(this);
327 public override bool __IsMissing
332 protected override Exception InvalidOperationException()
334 return new MissingModuleException(this);
337 protected override Exception NotSupportedException()
339 return new MissingModuleException(this);
342 protected override Exception ArgumentOutOfRangeException()
344 return new MissingModuleException(this);
348 sealed class MissingType : Type
350 private readonly Module module;
351 private readonly Type declaringType;
352 private readonly string ns;
353 private readonly string name;
354 private Type[] typeArgs;
357 internal MissingType(Module module, Type declaringType, string ns, string name)
359 this.module = module;
360 this.declaringType = declaringType;
363 MarkEnumOrValueType(ns, name);
366 internal override MethodBase FindMethod(string name, MethodSignature signature)
368 MethodInfo method = new MissingMethod(this, name, signature);
371 return new ConstructorInfoImpl(method);
376 internal override FieldInfo FindField(string name, FieldSignature signature)
378 return new MissingField(this, name, signature);
381 internal override Type FindNestedType(TypeName name)
386 internal override Type FindNestedTypeIgnoreCase(TypeName lowerCaseName)
391 public override bool __IsMissing
396 public override Type DeclaringType
398 get { return declaringType; }
401 public override string __Name
406 public override string __Namespace
411 public override string Name
413 get { return TypeNameParser.Escape(name); }
416 public override string FullName
418 get { return GetFullName(); }
421 public override Module Module
423 get { return module; }
426 public override int MetadataToken
428 get { return token; }
431 public override bool IsValueType
435 switch (typeFlags & (TypeFlags.ValueType | TypeFlags.NotValueType))
437 case TypeFlags.ValueType:
439 case TypeFlags.NotValueType:
442 if (module.universe.ResolveMissingTypeIsValueType(this))
444 typeFlags |= TypeFlags.ValueType;
448 typeFlags |= TypeFlags.NotValueType;
450 return (typeFlags & TypeFlags.ValueType) != 0;
455 public override Type BaseType
457 get { throw new MissingMemberException(this); }
460 public override TypeAttributes Attributes
462 get { throw new MissingMemberException(this); }
465 public override Type[] __GetDeclaredTypes()
467 throw new MissingMemberException(this);
470 public override Type[] __GetDeclaredInterfaces()
472 throw new MissingMemberException(this);
475 public override MethodBase[] __GetDeclaredMethods()
477 throw new MissingMemberException(this);
480 public override __MethodImplMap __GetMethodImplMap()
482 throw new MissingMemberException(this);
485 public override FieldInfo[] __GetDeclaredFields()
487 throw new MissingMemberException(this);
490 public override EventInfo[] __GetDeclaredEvents()
492 throw new MissingMemberException(this);
495 public override PropertyInfo[] __GetDeclaredProperties()
497 throw new MissingMemberException(this);
500 public override CustomModifiers __GetCustomModifiers()
502 throw new MissingMemberException(this);
505 public override Type[] GetGenericArguments()
507 throw new MissingMemberException(this);
510 public override CustomModifiers[] __GetGenericArgumentsCustomModifiers()
512 throw new MissingMemberException(this);
515 public override StructLayoutAttribute StructLayoutAttribute
517 get { throw new MissingMemberException(this); }
520 public override bool IsGenericType
522 get { throw new MissingMemberException(this); }
525 public override bool IsGenericTypeDefinition
527 get { throw new MissingMemberException(this); }
530 internal override Type GetGenericTypeArgument(int index)
532 if (typeArgs == null)
534 typeArgs = new Type[index + 1];
536 else if (typeArgs.Length <= index)
538 Array.Resize(ref typeArgs, index + 1);
540 return typeArgs[index] ?? (typeArgs[index] = new MissingTypeParameter(this, index));
543 internal override Type BindTypeParameters(IGenericBinder binder)
548 internal override Type SetMetadataTokenForMissing(int token)
554 internal override bool IsBaked
556 get { throw new MissingMemberException(this); }
560 sealed class MissingTypeParameter : IKVM.Reflection.Reader.TypeParameterType
562 private readonly MemberInfo owner;
563 private readonly int index;
565 internal MissingTypeParameter(MemberInfo owner, int index)
571 public override Module Module
573 get { return owner.Module; }
576 public override string Name
581 public override int GenericParameterPosition
583 get { return index; }
586 public override MethodBase DeclaringMethod
588 get { return owner as MethodBase; }
591 public override Type DeclaringType
593 get { return owner as Type; }
596 internal override Type BindTypeParameters(IGenericBinder binder)
598 if (owner is MethodBase)
600 return binder.BindMethodParameter(this);
604 return binder.BindTypeParameter(this);
608 internal override bool IsBaked
610 get { return owner.IsBaked; }
614 sealed class MissingMethod : MethodInfo
616 private readonly Type declaringType;
617 private readonly string name;
618 internal MethodSignature signature;
619 private MethodInfo forwarder;
620 private Type[] typeArgs;
622 internal MissingMethod(Type declaringType, string name, MethodSignature signature)
624 this.declaringType = declaringType;
626 this.signature = signature;
629 private MethodInfo Forwarder
633 MethodInfo method = TryGetForwarder();
636 throw new MissingMemberException(this);
642 private MethodInfo TryGetForwarder()
644 if (forwarder == null && !declaringType.__IsMissing)
646 MethodBase mb = declaringType.FindMethod(name, signature);
647 ConstructorInfo ci = mb as ConstructorInfo;
650 forwarder = ci.GetMethodInfo();
654 forwarder = (MethodInfo)mb;
660 public override bool __IsMissing
662 get { return TryGetForwarder() == null; }
665 public override Type ReturnType
667 get { return signature.GetReturnType(this); }
670 public override ParameterInfo ReturnParameter
672 get { return new ParameterInfoImpl(this, -1); }
675 internal override MethodSignature MethodSignature
677 get { return signature; }
680 internal override int ParameterCount
682 get { return signature.GetParameterCount(); }
685 private sealed class ParameterInfoImpl : ParameterInfo
687 private readonly MissingMethod method;
688 private readonly int index;
690 internal ParameterInfoImpl(MissingMethod method, int index)
692 this.method = method;
696 private ParameterInfo Forwarder
698 get { return index == -1 ? method.Forwarder.ReturnParameter : method.Forwarder.GetParameters()[index]; }
701 public override string Name
703 get { return Forwarder.Name; }
706 public override Type ParameterType
708 get { return index == -1 ? method.signature.GetReturnType(method) : method.signature.GetParameterType(method, index); }
711 public override ParameterAttributes Attributes
713 get { return Forwarder.Attributes; }
716 public override int Position
718 get { return index; }
721 public override object RawDefaultValue
723 get { return Forwarder.RawDefaultValue; }
726 public override CustomModifiers __GetCustomModifiers()
729 ? method.signature.GetReturnTypeCustomModifiers(method)
730 : method.signature.GetParameterCustomModifiers(method, index);
733 public override bool __TryGetFieldMarshal(out FieldMarshal fieldMarshal)
735 return Forwarder.__TryGetFieldMarshal(out fieldMarshal);
738 public override MemberInfo Member
740 get { return method; }
743 public override int MetadataToken
745 get { return Forwarder.MetadataToken; }
748 internal override Module Module
750 get { return method.Module; }
753 public override string ToString()
755 return Forwarder.ToString();
759 public override ParameterInfo[] GetParameters()
761 ParameterInfo[] parameters = new ParameterInfo[signature.GetParameterCount()];
762 for (int i = 0; i < parameters.Length; i++)
764 parameters[i] = new ParameterInfoImpl(this, i);
769 public override MethodAttributes Attributes
771 get { return Forwarder.Attributes; }
774 public override MethodImplAttributes GetMethodImplementationFlags()
776 return Forwarder.GetMethodImplementationFlags();
779 public override MethodBody GetMethodBody()
781 return Forwarder.GetMethodBody();
784 public override int __MethodRVA
786 get { return Forwarder.__MethodRVA; }
789 public override CallingConventions CallingConvention
791 get { return signature.CallingConvention; }
794 internal override int ImportTo(IKVM.Reflection.Emit.ModuleBuilder module)
796 MethodInfo method = TryGetForwarder();
799 return method.ImportTo(module);
801 return module.ImportMethodOrField(declaringType, this.Name, this.MethodSignature);
804 public override string Name
809 public override Type DeclaringType
811 get { return declaringType.IsModulePseudoType ? null : declaringType; }
814 public override Module Module
816 get { return declaringType.Module; }
819 public override bool Equals(object obj)
821 MissingMethod other = obj as MissingMethod;
823 && other.declaringType == declaringType
824 && other.name == name
825 && other.signature.Equals(signature);
828 public override int GetHashCode()
830 return declaringType.GetHashCode() ^ name.GetHashCode() ^ signature.GetHashCode();
833 internal override MethodBase BindTypeParameters(Type type)
835 MethodInfo forwarder = TryGetForwarder();
836 if (forwarder != null)
838 return forwarder.BindTypeParameters(type);
840 return new GenericMethodInstance(type, this, null);
843 public override bool ContainsGenericParameters
845 get { return Forwarder.ContainsGenericParameters; }
848 public override Type[] GetGenericArguments()
850 MethodInfo method = TryGetForwarder();
853 return Forwarder.GetGenericArguments();
855 if (typeArgs == null)
857 typeArgs = new Type[signature.GenericParameterCount];
858 for (int i = 0; i < typeArgs.Length; i++)
860 typeArgs[i] = new MissingTypeParameter(this, i);
863 return Util.Copy(typeArgs);
866 internal override Type GetGenericMethodArgument(int index)
868 return GetGenericArguments()[index];
871 internal override int GetGenericMethodArgumentCount()
873 return Forwarder.GetGenericMethodArgumentCount();
876 public override MethodInfo GetGenericMethodDefinition()
878 return Forwarder.GetGenericMethodDefinition();
881 internal override MethodInfo GetMethodOnTypeDefinition()
883 return Forwarder.GetMethodOnTypeDefinition();
886 internal override bool HasThis
888 get { return (signature.CallingConvention & (CallingConventions.HasThis | CallingConventions.ExplicitThis)) == CallingConventions.HasThis; }
891 public override bool IsGenericMethod
893 get { return IsGenericMethodDefinition; }
896 public override bool IsGenericMethodDefinition
898 get { return signature.GenericParameterCount != 0; }
901 public override MethodInfo MakeGenericMethod(params Type[] typeArguments)
903 MethodInfo method = TryGetForwarder();
906 return method.MakeGenericMethod(typeArguments);
908 return new GenericMethodInstance(declaringType, this, typeArguments);
911 public override int MetadataToken
913 get { return Forwarder.MetadataToken; }
916 internal override int GetCurrentToken()
918 return Forwarder.GetCurrentToken();
921 internal override bool IsBaked
923 get { return Forwarder.IsBaked; }
927 sealed class MissingField : FieldInfo
929 private readonly Type declaringType;
930 private readonly string name;
931 private readonly FieldSignature signature;
932 private FieldInfo forwarder;
934 internal MissingField(Type declaringType, string name, FieldSignature signature)
936 this.declaringType = declaringType;
938 this.signature = signature;
941 private FieldInfo Forwarder
945 FieldInfo field = TryGetForwarder();
948 throw new MissingMemberException(this);
954 private FieldInfo TryGetForwarder()
956 if (forwarder == null && !declaringType.__IsMissing)
958 forwarder = declaringType.FindField(name, signature);
963 public override bool __IsMissing
965 get { return TryGetForwarder() == null; }
968 public override FieldAttributes Attributes
970 get { return Forwarder.Attributes; }
973 public override void __GetDataFromRVA(byte[] data, int offset, int length)
975 Forwarder.__GetDataFromRVA(data, offset, length);
978 public override int __FieldRVA
980 get { return Forwarder.__FieldRVA; }
983 public override bool __TryGetFieldOffset(out int offset)
985 return Forwarder.__TryGetFieldOffset(out offset);
988 public override object GetRawConstantValue()
990 return Forwarder.GetRawConstantValue();
993 internal override FieldSignature FieldSignature
995 get { return signature; }
998 internal override int ImportTo(IKVM.Reflection.Emit.ModuleBuilder module)
1000 FieldInfo field = TryGetForwarder();
1003 return field.ImportTo(module);
1005 return module.ImportMethodOrField(declaringType, this.Name, this.FieldSignature);
1008 public override string Name
1010 get { return name; }
1013 public override Type DeclaringType
1015 get { return declaringType.IsModulePseudoType ? null : declaringType; }
1018 public override Module Module
1020 get { return declaringType.Module; }
1023 internal override FieldInfo BindTypeParameters(Type type)
1025 FieldInfo forwarder = TryGetForwarder();
1026 if (forwarder != null)
1028 return forwarder.BindTypeParameters(type);
1030 return new GenericFieldInstance(type, this);
1033 public override int MetadataToken
1035 get { return Forwarder.MetadataToken; }
1038 public override bool Equals(object obj)
1040 MissingField other = obj as MissingField;
1041 return other != null
1042 && other.declaringType == declaringType
1043 && other.name == name
1044 && other.signature.Equals(signature);
1047 public override int GetHashCode()
1049 return declaringType.GetHashCode() ^ name.GetHashCode() ^ signature.GetHashCode();
1052 public override string ToString()
1054 return this.FieldType.Name + " " + this.Name;
1057 internal override int GetCurrentToken()
1059 return Forwarder.GetCurrentToken();
1062 internal override bool IsBaked
1064 get { return Forwarder.IsBaked; }
1068 // NOTE this is currently only used by CustomAttributeData (because there is no other way to refer to a property)
1069 sealed class MissingProperty : PropertyInfo
1071 private readonly Type declaringType;
1072 private readonly string name;
1073 private readonly PropertySignature signature;
1075 internal MissingProperty(Type declaringType, string name, PropertySignature signature)
1077 this.declaringType = declaringType;
1079 this.signature = signature;
1082 public override PropertyAttributes Attributes
1084 get { throw new MissingMemberException(this); }
1087 public override bool CanRead
1089 get { throw new MissingMemberException(this); }
1092 public override bool CanWrite
1094 get { throw new MissingMemberException(this); }
1097 public override MethodInfo GetGetMethod(bool nonPublic)
1099 throw new MissingMemberException(this);
1102 public override MethodInfo GetSetMethod(bool nonPublic)
1104 throw new MissingMemberException(this);
1107 public override MethodInfo[] GetAccessors(bool nonPublic)
1109 throw new MissingMemberException(this);
1112 public override object GetRawConstantValue()
1114 throw new MissingMemberException(this);
1117 internal override bool IsPublic
1119 get { throw new MissingMemberException(this); }
1122 internal override bool IsNonPrivate
1124 get { throw new MissingMemberException(this); }
1127 internal override bool IsStatic
1129 get { throw new MissingMemberException(this); }
1132 internal override PropertySignature PropertySignature
1134 get { return signature; }
1137 public override string Name
1139 get { return name; }
1142 public override Type DeclaringType
1144 get { return declaringType; }
1147 public override Module Module
1149 get { return declaringType.Module; }
1152 internal override bool IsBaked
1154 get { return declaringType.IsBaked; }
1157 internal override int GetCurrentToken()
1159 throw new MissingMemberException(this);