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;
355 internal override MethodBase FindMethod(string name, MethodSignature signature)
357 MethodInfo method = new MissingMethod(this, name, signature);
360 return new ConstructorInfoImpl(method);
365 internal override FieldInfo FindField(string name, FieldSignature signature)
367 return new MissingField(this, name, signature);
370 internal override Type FindNestedType(TypeName name)
375 public override bool __IsMissing
380 public override Type DeclaringType
382 get { return declaringType; }
385 public override string __Name
390 public override string __Namespace
395 public override string Name
397 get { return TypeNameParser.Escape(name); }
400 public override string FullName
402 get { return GetFullName(); }
405 public override Module Module
407 get { return module; }
410 public override bool IsValueType
414 switch (typeFlags & (TypeFlags.ValueType | TypeFlags.NotValueType))
416 case TypeFlags.ValueType:
418 case TypeFlags.NotValueType:
421 throw new MissingMemberException(this);
426 public override Type BaseType
428 get { throw new MissingMemberException(this); }
431 public override TypeAttributes Attributes
433 get { throw new MissingMemberException(this); }
436 public override Type[] __GetDeclaredTypes()
438 throw new MissingMemberException(this);
441 public override Type[] __GetDeclaredInterfaces()
443 throw new MissingMemberException(this);
446 public override MethodBase[] __GetDeclaredMethods()
448 throw new MissingMemberException(this);
451 public override __MethodImplMap __GetMethodImplMap()
453 throw new MissingMemberException(this);
456 public override FieldInfo[] __GetDeclaredFields()
458 throw new MissingMemberException(this);
461 public override EventInfo[] __GetDeclaredEvents()
463 throw new MissingMemberException(this);
466 public override PropertyInfo[] __GetDeclaredProperties()
468 throw new MissingMemberException(this);
471 public override Type[] __GetRequiredCustomModifiers()
473 throw new MissingMemberException(this);
476 public override Type[] __GetOptionalCustomModifiers()
478 throw new MissingMemberException(this);
481 public override Type[] GetGenericArguments()
483 throw new MissingMemberException(this);
486 public override Type[][] __GetGenericArgumentsRequiredCustomModifiers()
488 throw new MissingMemberException(this);
491 public override Type[][] __GetGenericArgumentsOptionalCustomModifiers()
493 throw new MissingMemberException(this);
496 public override StructLayoutAttribute StructLayoutAttribute
498 get { throw new MissingMemberException(this); }
501 public override bool IsGenericType
503 get { throw new MissingMemberException(this); }
506 public override bool IsGenericTypeDefinition
508 get { throw new MissingMemberException(this); }
511 internal override Type GetGenericTypeArgument(int index)
513 if (typeArgs == null)
515 typeArgs = new Type[index + 1];
517 else if (typeArgs.Length <= index)
519 Array.Resize(ref typeArgs, index + 1);
521 return typeArgs[index] ?? (typeArgs[index] = new MissingTypeParameter(this, index));
524 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
526 throw new MissingMemberException(this);
529 internal override Type BindTypeParameters(IGenericBinder binder)
534 internal int GetMetadataTokenForMissing()
539 internal override Type SetMetadataTokenForMissing(int token)
546 sealed class MissingTypeParameter : IKVM.Reflection.Reader.TypeParameterType
548 private readonly MemberInfo owner;
549 private readonly int index;
551 internal MissingTypeParameter(MemberInfo owner, int index)
557 public override Module Module
559 get { return owner.Module; }
562 public override string Name
567 public override int GenericParameterPosition
569 get { return index; }
572 public override MethodBase DeclaringMethod
574 get { return owner as MethodBase; }
577 public override Type DeclaringType
579 get { return owner as Type; }
583 sealed class MissingMethod : MethodInfo
585 private readonly Type declaringType;
586 private readonly string name;
587 internal MethodSignature signature;
588 private MethodInfo forwarder;
589 private Type[] typeArgs;
591 internal MissingMethod(Type declaringType, string name, MethodSignature signature)
593 this.declaringType = declaringType;
595 this.signature = signature;
598 private MethodInfo Forwarder
602 MethodInfo method = TryGetForwarder();
605 throw new MissingMemberException(this);
611 private MethodInfo TryGetForwarder()
613 if (forwarder == null && !declaringType.__IsMissing)
615 MethodBase mb = declaringType.FindMethod(name, signature);
616 ConstructorInfo ci = mb as ConstructorInfo;
619 forwarder = ci.GetMethodInfo();
623 forwarder = (MethodInfo)mb;
629 public override bool __IsMissing
631 get { return TryGetForwarder() == null; }
634 public override Type ReturnType
636 get { return signature.GetReturnType(this); }
639 public override ParameterInfo ReturnParameter
641 get { return new ParameterInfoImpl(this, -1); }
644 internal override MethodSignature MethodSignature
646 get { return signature; }
649 internal override int ParameterCount
651 get { return signature.GetParameterCount(); }
654 private sealed class ParameterInfoImpl : ParameterInfo
656 private readonly MissingMethod method;
657 private readonly int index;
659 internal ParameterInfoImpl(MissingMethod method, int index)
661 this.method = method;
665 private ParameterInfo Forwarder
667 get { return index == -1 ? method.Forwarder.ReturnParameter : method.Forwarder.GetParameters()[index]; }
670 public override string Name
672 get { return Forwarder.Name; }
675 public override Type ParameterType
677 get { return index == -1 ? method.signature.GetReturnType(method) : method.signature.GetParameterType(method, index); }
680 public override ParameterAttributes Attributes
682 get { return Forwarder.Attributes; }
685 public override int Position
687 get { return index; }
690 public override object RawDefaultValue
692 get { return Forwarder.RawDefaultValue; }
695 public override Type[] GetOptionalCustomModifiers()
699 return Util.Copy(method.signature.GetReturnTypeOptionalCustomModifiers(method));
701 return Util.Copy(method.signature.GetParameterOptionalCustomModifiers(method, index));
704 public override Type[] GetRequiredCustomModifiers()
708 return Util.Copy(method.signature.GetReturnTypeRequiredCustomModifiers(method));
710 return Util.Copy(method.signature.GetParameterRequiredCustomModifiers(method, index));
713 public override MemberInfo Member
715 get { return method; }
718 public override int MetadataToken
720 get { return Forwarder.MetadataToken; }
723 internal override Module Module
725 get { return method.Module; }
728 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
730 return Forwarder.GetCustomAttributesData(attributeType);
733 public override string ToString()
735 return Forwarder.ToString();
739 public override ParameterInfo[] GetParameters()
741 ParameterInfo[] parameters = new ParameterInfo[signature.GetParameterCount()];
742 for (int i = 0; i < parameters.Length; i++)
744 parameters[i] = new ParameterInfoImpl(this, i);
749 public override MethodAttributes Attributes
751 get { return Forwarder.Attributes; }
754 public override MethodImplAttributes GetMethodImplementationFlags()
756 return Forwarder.GetMethodImplementationFlags();
759 public override MethodBody GetMethodBody()
761 return Forwarder.GetMethodBody();
764 public override CallingConventions CallingConvention
766 get { return signature.CallingConvention; }
769 internal override int ImportTo(IKVM.Reflection.Emit.ModuleBuilder module)
771 MethodInfo method = TryGetForwarder();
774 return method.ImportTo(module);
776 return module.ImportMethodOrField(declaringType, this.Name, this.MethodSignature);
779 public override string Name
784 public override Type DeclaringType
786 get { return declaringType.IsModulePseudoType ? null : declaringType; }
789 public override Module Module
791 get { return declaringType.Module; }
794 public override bool Equals(object obj)
796 MissingMethod other = obj as MissingMethod;
798 && other.declaringType == declaringType
799 && other.name == name
800 && other.signature.Equals(signature);
803 public override int GetHashCode()
805 return declaringType.GetHashCode() ^ name.GetHashCode() ^ signature.GetHashCode();
808 internal override MethodBase BindTypeParameters(Type type)
810 MethodInfo forwarder = TryGetForwarder();
811 if (forwarder != null)
813 return forwarder.BindTypeParameters(type);
815 return new GenericMethodInstance(type, this, null);
818 public override bool ContainsGenericParameters
820 get { return Forwarder.ContainsGenericParameters; }
823 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
825 return Forwarder.GetCustomAttributesData(attributeType);
828 public override Type[] GetGenericArguments()
830 MethodInfo method = TryGetForwarder();
833 return Forwarder.GetGenericArguments();
835 if (typeArgs == null)
837 typeArgs = new Type[signature.GenericParameterCount];
838 for (int i = 0; i < typeArgs.Length; i++)
840 typeArgs[i] = new MissingTypeParameter(this, i);
843 return Util.Copy(typeArgs);
846 internal override Type GetGenericMethodArgument(int index)
848 return GetGenericArguments()[index];
851 internal override int GetGenericMethodArgumentCount()
853 return Forwarder.GetGenericMethodArgumentCount();
856 public override MethodInfo GetGenericMethodDefinition()
858 return Forwarder.GetGenericMethodDefinition();
861 internal override MethodInfo GetMethodOnTypeDefinition()
863 return Forwarder.GetMethodOnTypeDefinition();
866 internal override bool HasThis
868 get { return (signature.CallingConvention & (CallingConventions.HasThis | CallingConventions.ExplicitThis)) == CallingConventions.HasThis; }
871 public override bool IsGenericMethod
873 get { return IsGenericMethodDefinition; }
876 public override bool IsGenericMethodDefinition
878 get { return signature.GenericParameterCount != 0; }
881 public override MethodInfo MakeGenericMethod(params Type[] typeArguments)
883 MethodInfo method = TryGetForwarder();
886 return method.MakeGenericMethod(typeArguments);
888 return new GenericMethodInstance(declaringType, this, typeArguments);
891 public override int MetadataToken
893 get { return Forwarder.MetadataToken; }
897 sealed class MissingField : FieldInfo
899 private readonly Type declaringType;
900 private readonly string name;
901 private readonly FieldSignature signature;
902 private FieldInfo forwarder;
904 internal MissingField(Type declaringType, string name, FieldSignature signature)
906 this.declaringType = declaringType;
908 this.signature = signature;
911 private FieldInfo Forwarder
915 FieldInfo field = TryGetForwarder();
918 throw new MissingMemberException(this);
924 private FieldInfo TryGetForwarder()
926 if (forwarder == null && !declaringType.__IsMissing)
928 forwarder = declaringType.FindField(name, signature);
933 public override bool __IsMissing
935 get { return TryGetForwarder() == null; }
938 public override FieldAttributes Attributes
940 get { return Forwarder.Attributes; }
943 public override void __GetDataFromRVA(byte[] data, int offset, int length)
945 Forwarder.__GetDataFromRVA(data, offset, length);
948 public override int __FieldRVA
950 get { return Forwarder.__FieldRVA; }
953 public override object GetRawConstantValue()
955 return Forwarder.GetRawConstantValue();
958 internal override FieldSignature FieldSignature
960 get { return signature; }
963 internal override int ImportTo(IKVM.Reflection.Emit.ModuleBuilder module)
965 FieldInfo field = TryGetForwarder();
968 return field.ImportTo(module);
970 return module.ImportMethodOrField(declaringType, this.Name, this.FieldSignature);
973 public override string Name
978 public override Type DeclaringType
980 get { return declaringType.IsModulePseudoType ? null : declaringType; }
983 public override Module Module
985 get { return declaringType.Module; }
988 internal override FieldInfo BindTypeParameters(Type type)
990 FieldInfo forwarder = TryGetForwarder();
991 if (forwarder != null)
993 return forwarder.BindTypeParameters(type);
995 return new GenericFieldInstance(type, this);
998 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
1000 return Forwarder.GetCustomAttributesData(attributeType);
1003 public override int MetadataToken
1005 get { return Forwarder.MetadataToken; }
1008 public override bool Equals(object obj)
1010 MissingField other = obj as MissingField;
1011 return other != null
1012 && other.declaringType == declaringType
1013 && other.name == name
1014 && other.signature.Equals(signature);
1017 public override int GetHashCode()
1019 return declaringType.GetHashCode() ^ name.GetHashCode() ^ signature.GetHashCode();
1022 public override string ToString()
1024 return this.FieldType.Name + " " + this.Name;
1028 // NOTE this is currently only used by CustomAttributeData (because there is no other way to refer to a property)
1029 sealed class MissingProperty : PropertyInfo
1031 private readonly Type declaringType;
1032 private readonly string name;
1033 private readonly PropertySignature signature;
1035 internal MissingProperty(Type declaringType, string name, PropertySignature signature)
1037 this.declaringType = declaringType;
1039 this.signature = signature;
1042 public override PropertyAttributes Attributes
1044 get { throw new MissingMemberException(this); }
1047 public override bool CanRead
1049 get { throw new MissingMemberException(this); }
1052 public override bool CanWrite
1054 get { throw new MissingMemberException(this); }
1057 public override MethodInfo GetGetMethod(bool nonPublic)
1059 throw new MissingMemberException(this);
1062 public override MethodInfo GetSetMethod(bool nonPublic)
1064 throw new MissingMemberException(this);
1067 public override MethodInfo[] GetAccessors(bool nonPublic)
1069 throw new MissingMemberException(this);
1072 public override object GetRawConstantValue()
1074 throw new MissingMemberException(this);
1077 internal override bool IsPublic
1079 get { throw new MissingMemberException(this); }
1082 internal override bool IsStatic
1084 get { throw new MissingMemberException(this); }
1087 internal override PropertySignature PropertySignature
1089 get { return signature; }
1092 public override string Name
1094 get { return name; }
1097 public override Type DeclaringType
1099 get { return declaringType; }
1102 public override Module Module
1104 get { return declaringType.Module; }