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 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
334 throw new MissingModuleException(this);
337 protected override Exception InvalidOperationException()
339 return new MissingModuleException(this);
342 protected override Exception NotSupportedException()
344 return new MissingModuleException(this);
347 protected override Exception ArgumentOutOfRangeException()
349 return new MissingModuleException(this);
353 sealed class MissingType : Type
355 private readonly Module module;
356 private readonly Type declaringType;
357 private readonly string ns;
358 private readonly string name;
359 private Type[] typeArgs;
362 internal MissingType(Module module, Type declaringType, string ns, string name)
364 this.module = module;
365 this.declaringType = declaringType;
368 MarkEnumOrValueType(ns, name);
371 internal override MethodBase FindMethod(string name, MethodSignature signature)
373 MethodInfo method = new MissingMethod(this, name, signature);
376 return new ConstructorInfoImpl(method);
381 internal override FieldInfo FindField(string name, FieldSignature signature)
383 return new MissingField(this, name, signature);
386 internal override Type FindNestedType(TypeName name)
391 internal override Type FindNestedTypeIgnoreCase(TypeName lowerCaseName)
396 public override bool __IsMissing
401 public override Type DeclaringType
403 get { return declaringType; }
406 public override string __Name
411 public override string __Namespace
416 public override string Name
418 get { return TypeNameParser.Escape(name); }
421 public override string FullName
423 get { return GetFullName(); }
426 public override Module Module
428 get { return module; }
431 public override int MetadataToken
433 get { return token; }
436 public override bool IsValueType
440 switch (typeFlags & (TypeFlags.ValueType | TypeFlags.NotValueType))
442 case TypeFlags.ValueType:
444 case TypeFlags.NotValueType:
447 if (module.universe.ResolveMissingTypeIsValueType(this))
449 typeFlags |= TypeFlags.ValueType;
453 typeFlags |= TypeFlags.NotValueType;
455 return (typeFlags & TypeFlags.ValueType) != 0;
460 public override Type BaseType
462 get { throw new MissingMemberException(this); }
465 public override TypeAttributes Attributes
467 get { throw new MissingMemberException(this); }
470 public override Type[] __GetDeclaredTypes()
472 throw new MissingMemberException(this);
475 public override Type[] __GetDeclaredInterfaces()
477 throw new MissingMemberException(this);
480 public override MethodBase[] __GetDeclaredMethods()
482 throw new MissingMemberException(this);
485 public override __MethodImplMap __GetMethodImplMap()
487 throw new MissingMemberException(this);
490 public override FieldInfo[] __GetDeclaredFields()
492 throw new MissingMemberException(this);
495 public override EventInfo[] __GetDeclaredEvents()
497 throw new MissingMemberException(this);
500 public override PropertyInfo[] __GetDeclaredProperties()
502 throw new MissingMemberException(this);
505 public override CustomModifiers __GetCustomModifiers()
507 throw new MissingMemberException(this);
510 public override Type[] GetGenericArguments()
512 throw new MissingMemberException(this);
515 public override CustomModifiers[] __GetGenericArgumentsCustomModifiers()
517 throw new MissingMemberException(this);
520 public override StructLayoutAttribute StructLayoutAttribute
522 get { throw new MissingMemberException(this); }
525 public override bool IsGenericType
527 get { throw new MissingMemberException(this); }
530 public override bool IsGenericTypeDefinition
532 get { throw new MissingMemberException(this); }
535 internal override Type GetGenericTypeArgument(int index)
537 if (typeArgs == null)
539 typeArgs = new Type[index + 1];
541 else if (typeArgs.Length <= index)
543 Array.Resize(ref typeArgs, index + 1);
545 return typeArgs[index] ?? (typeArgs[index] = new MissingTypeParameter(this, index));
548 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
550 throw new MissingMemberException(this);
553 internal override Type BindTypeParameters(IGenericBinder binder)
558 internal override Type SetMetadataTokenForMissing(int token)
565 sealed class MissingTypeParameter : IKVM.Reflection.Reader.TypeParameterType
567 private readonly MemberInfo owner;
568 private readonly int index;
570 internal MissingTypeParameter(MemberInfo owner, int index)
576 public override Module Module
578 get { return owner.Module; }
581 public override string Name
586 public override int GenericParameterPosition
588 get { return index; }
591 public override MethodBase DeclaringMethod
593 get { return owner as MethodBase; }
596 public override Type DeclaringType
598 get { return owner as Type; }
601 internal override Type BindTypeParameters(IGenericBinder binder)
603 if (owner is MethodBase)
605 return binder.BindMethodParameter(this);
609 return binder.BindTypeParameter(this);
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 MemberInfo Member
735 get { return method; }
738 public override int MetadataToken
740 get { return Forwarder.MetadataToken; }
743 internal override Module Module
745 get { return method.Module; }
748 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
750 return Forwarder.GetCustomAttributesData(attributeType);
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 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
850 return Forwarder.GetCustomAttributesData(attributeType);
853 public override Type[] GetGenericArguments()
855 MethodInfo method = TryGetForwarder();
858 return Forwarder.GetGenericArguments();
860 if (typeArgs == null)
862 typeArgs = new Type[signature.GenericParameterCount];
863 for (int i = 0; i < typeArgs.Length; i++)
865 typeArgs[i] = new MissingTypeParameter(this, i);
868 return Util.Copy(typeArgs);
871 internal override Type GetGenericMethodArgument(int index)
873 return GetGenericArguments()[index];
876 internal override int GetGenericMethodArgumentCount()
878 return Forwarder.GetGenericMethodArgumentCount();
881 public override MethodInfo GetGenericMethodDefinition()
883 return Forwarder.GetGenericMethodDefinition();
886 internal override MethodInfo GetMethodOnTypeDefinition()
888 return Forwarder.GetMethodOnTypeDefinition();
891 internal override bool HasThis
893 get { return (signature.CallingConvention & (CallingConventions.HasThis | CallingConventions.ExplicitThis)) == CallingConventions.HasThis; }
896 public override bool IsGenericMethod
898 get { return IsGenericMethodDefinition; }
901 public override bool IsGenericMethodDefinition
903 get { return signature.GenericParameterCount != 0; }
906 public override MethodInfo MakeGenericMethod(params Type[] typeArguments)
908 MethodInfo method = TryGetForwarder();
911 return method.MakeGenericMethod(typeArguments);
913 return new GenericMethodInstance(declaringType, this, typeArguments);
916 public override int MetadataToken
918 get { return Forwarder.MetadataToken; }
922 sealed class MissingField : FieldInfo
924 private readonly Type declaringType;
925 private readonly string name;
926 private readonly FieldSignature signature;
927 private FieldInfo forwarder;
929 internal MissingField(Type declaringType, string name, FieldSignature signature)
931 this.declaringType = declaringType;
933 this.signature = signature;
936 private FieldInfo Forwarder
940 FieldInfo field = TryGetForwarder();
943 throw new MissingMemberException(this);
949 private FieldInfo TryGetForwarder()
951 if (forwarder == null && !declaringType.__IsMissing)
953 forwarder = declaringType.FindField(name, signature);
958 public override bool __IsMissing
960 get { return TryGetForwarder() == null; }
963 public override FieldAttributes Attributes
965 get { return Forwarder.Attributes; }
968 public override void __GetDataFromRVA(byte[] data, int offset, int length)
970 Forwarder.__GetDataFromRVA(data, offset, length);
973 public override int __FieldRVA
975 get { return Forwarder.__FieldRVA; }
978 public override object GetRawConstantValue()
980 return Forwarder.GetRawConstantValue();
983 internal override FieldSignature FieldSignature
985 get { return signature; }
988 internal override int ImportTo(IKVM.Reflection.Emit.ModuleBuilder module)
990 FieldInfo field = TryGetForwarder();
993 return field.ImportTo(module);
995 return module.ImportMethodOrField(declaringType, this.Name, this.FieldSignature);
998 public override string Name
1000 get { return name; }
1003 public override Type DeclaringType
1005 get { return declaringType.IsModulePseudoType ? null : declaringType; }
1008 public override Module Module
1010 get { return declaringType.Module; }
1013 internal override FieldInfo BindTypeParameters(Type type)
1015 FieldInfo forwarder = TryGetForwarder();
1016 if (forwarder != null)
1018 return forwarder.BindTypeParameters(type);
1020 return new GenericFieldInstance(type, this);
1023 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
1025 return Forwarder.GetCustomAttributesData(attributeType);
1028 public override int MetadataToken
1030 get { return Forwarder.MetadataToken; }
1033 public override bool Equals(object obj)
1035 MissingField other = obj as MissingField;
1036 return other != null
1037 && other.declaringType == declaringType
1038 && other.name == name
1039 && other.signature.Equals(signature);
1042 public override int GetHashCode()
1044 return declaringType.GetHashCode() ^ name.GetHashCode() ^ signature.GetHashCode();
1047 public override string ToString()
1049 return this.FieldType.Name + " " + this.Name;
1053 // NOTE this is currently only used by CustomAttributeData (because there is no other way to refer to a property)
1054 sealed class MissingProperty : PropertyInfo
1056 private readonly Type declaringType;
1057 private readonly string name;
1058 private readonly PropertySignature signature;
1060 internal MissingProperty(Type declaringType, string name, PropertySignature signature)
1062 this.declaringType = declaringType;
1064 this.signature = signature;
1067 public override PropertyAttributes Attributes
1069 get { throw new MissingMemberException(this); }
1072 public override bool CanRead
1074 get { throw new MissingMemberException(this); }
1077 public override bool CanWrite
1079 get { throw new MissingMemberException(this); }
1082 public override MethodInfo GetGetMethod(bool nonPublic)
1084 throw new MissingMemberException(this);
1087 public override MethodInfo GetSetMethod(bool nonPublic)
1089 throw new MissingMemberException(this);
1092 public override MethodInfo[] GetAccessors(bool nonPublic)
1094 throw new MissingMemberException(this);
1097 public override object GetRawConstantValue()
1099 throw new MissingMemberException(this);
1102 internal override bool IsPublic
1104 get { throw new MissingMemberException(this); }
1107 internal override bool IsNonPrivate
1109 get { throw new MissingMemberException(this); }
1112 internal override bool IsStatic
1114 get { throw new MissingMemberException(this); }
1117 internal override PropertySignature PropertySignature
1119 get { return signature; }
1122 public override string Name
1124 get { return name; }
1127 public override Type DeclaringType
1129 get { return declaringType; }
1132 public override Module Module
1134 get { return declaringType.Module; }