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, 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;
143 private readonly string name;
145 internal MissingAssembly(Universe universe, string name)
148 module = new MissingModule(this);
152 public override Type[] GetTypes()
154 throw new MissingAssemblyException(this);
157 public override string FullName
162 public override AssemblyName GetName()
164 return new AssemblyName(name);
167 public override string ImageRuntimeVersion
169 get { throw new MissingAssemblyException(this); }
172 public override Module ManifestModule
174 get { return module; }
177 public override MethodInfo EntryPoint
179 get { throw new MissingAssemblyException(this); }
182 public override string Location
184 get { throw new MissingAssemblyException(this); }
187 public override AssemblyName[] GetReferencedAssemblies()
189 throw new MissingAssemblyException(this);
192 public override Module[] GetModules(bool getResourceModules)
194 throw new MissingAssemblyException(this);
197 public override Module[] GetLoadedModules(bool getResourceModules)
199 throw new MissingAssemblyException(this);
202 public override Module GetModule(string name)
204 throw new MissingAssemblyException(this);
207 public override string[] GetManifestResourceNames()
209 throw new MissingAssemblyException(this);
212 public override ManifestResourceInfo GetManifestResourceInfo(string resourceName)
214 throw new MissingAssemblyException(this);
217 public override System.IO.Stream GetManifestResourceStream(string resourceName)
219 throw new MissingAssemblyException(this);
222 public override bool __IsMissing
227 internal override Type FindType(TypeName typeName)
232 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
234 throw new MissingAssemblyException(this);
238 sealed class MissingModule : NonPEModule
240 private readonly MissingAssembly assembly;
242 internal MissingModule(MissingAssembly assembly)
243 : base(assembly.universe)
245 this.assembly = assembly;
248 public override int MDStreamVersion
250 get { throw new MissingModuleException(this); }
253 public override Assembly Assembly
255 get { return assembly; }
258 public override string FullyQualifiedName
260 get { throw new MissingModuleException(this); }
263 public override string Name
265 get { throw new MissingModuleException(this); }
268 public override Guid ModuleVersionId
270 get { throw new MissingModuleException(this); }
273 public override string ScopeName
275 get { throw new MissingModuleException(this); }
278 internal override Type FindType(TypeName typeName)
283 internal override void GetTypesImpl(System.Collections.Generic.List<Type> list)
285 throw new MissingModuleException(this);
288 public override void __GetDataDirectoryEntry(int index, out int rva, out int length)
290 throw new MissingModuleException(this);
293 public override IList<CustomAttributeData> __GetPlaceholderAssemblyCustomAttributes(bool multiple, bool security)
295 throw new MissingModuleException(this);
298 public override long __RelativeVirtualAddressToFileOffset(int rva)
300 throw new MissingModuleException(this);
303 public override __StandAloneMethodSig __ResolveStandAloneMethodSig(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
305 throw new MissingModuleException(this);
308 public override int __Subsystem
310 get { throw new MissingModuleException(this); }
313 internal override void ExportTypes(int fileToken, IKVM.Reflection.Emit.ModuleBuilder manifestModule)
315 throw new MissingModuleException(this);
318 public override void GetPEKind(out PortableExecutableKinds peKind, out ImageFileMachine machine)
320 throw new MissingModuleException(this);
323 public override bool __IsMissing
328 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
330 throw new MissingModuleException(this);
333 protected override Exception InvalidOperationException()
335 return new MissingModuleException(this);
338 protected override Exception NotSupportedException()
340 return new MissingModuleException(this);
343 protected override Exception ArgumentOutOfRangeException()
345 return new MissingModuleException(this);
349 sealed class MissingType : Type
351 private readonly Module module;
352 private readonly Type declaringType;
353 private readonly string ns;
354 private readonly string name;
355 private Type[] typeArgs;
358 internal MissingType(Module module, Type declaringType, string ns, string name)
360 this.module = module;
361 this.declaringType = declaringType;
364 MarkEnumOrValueType(ns, name);
367 internal override MethodBase FindMethod(string name, MethodSignature signature)
369 MethodInfo method = new MissingMethod(this, name, signature);
372 return new ConstructorInfoImpl(method);
377 internal override FieldInfo FindField(string name, FieldSignature signature)
379 return new MissingField(this, name, signature);
382 internal override Type FindNestedType(TypeName name)
387 public override bool __IsMissing
392 public override Type DeclaringType
394 get { return declaringType; }
397 public override string __Name
402 public override string __Namespace
407 public override string Name
409 get { return TypeNameParser.Escape(name); }
412 public override string FullName
414 get { return GetFullName(); }
417 public override Module Module
419 get { return module; }
422 public override bool IsValueType
426 switch (typeFlags & (TypeFlags.ValueType | TypeFlags.NotValueType))
428 case TypeFlags.ValueType:
430 case TypeFlags.NotValueType:
433 throw new MissingMemberException(this);
438 public override Type BaseType
440 get { throw new MissingMemberException(this); }
443 public override TypeAttributes Attributes
445 get { throw new MissingMemberException(this); }
448 public override Type[] __GetDeclaredTypes()
450 throw new MissingMemberException(this);
453 public override Type[] __GetDeclaredInterfaces()
455 throw new MissingMemberException(this);
458 public override MethodBase[] __GetDeclaredMethods()
460 throw new MissingMemberException(this);
463 public override __MethodImplMap __GetMethodImplMap()
465 throw new MissingMemberException(this);
468 public override FieldInfo[] __GetDeclaredFields()
470 throw new MissingMemberException(this);
473 public override EventInfo[] __GetDeclaredEvents()
475 throw new MissingMemberException(this);
478 public override PropertyInfo[] __GetDeclaredProperties()
480 throw new MissingMemberException(this);
483 public override CustomModifiers __GetCustomModifiers()
485 throw new MissingMemberException(this);
488 public override Type[] GetGenericArguments()
490 throw new MissingMemberException(this);
493 public override CustomModifiers[] __GetGenericArgumentsCustomModifiers()
495 throw new MissingMemberException(this);
498 public override StructLayoutAttribute StructLayoutAttribute
500 get { throw new MissingMemberException(this); }
503 public override bool IsGenericType
505 get { throw new MissingMemberException(this); }
508 public override bool IsGenericTypeDefinition
510 get { throw new MissingMemberException(this); }
513 internal override Type GetGenericTypeArgument(int index)
515 if (typeArgs == null)
517 typeArgs = new Type[index + 1];
519 else if (typeArgs.Length <= index)
521 Array.Resize(ref typeArgs, index + 1);
523 return typeArgs[index] ?? (typeArgs[index] = new MissingTypeParameter(this, index));
526 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
528 throw new MissingMemberException(this);
531 internal override Type BindTypeParameters(IGenericBinder binder)
536 internal int GetMetadataTokenForMissing()
541 internal override Type SetMetadataTokenForMissing(int token)
548 sealed class MissingTypeParameter : IKVM.Reflection.Reader.TypeParameterType
550 private readonly MemberInfo owner;
551 private readonly int index;
553 internal MissingTypeParameter(MemberInfo owner, int index)
559 public override Module Module
561 get { return owner.Module; }
564 public override string Name
569 public override int GenericParameterPosition
571 get { return index; }
574 public override MethodBase DeclaringMethod
576 get { return owner as MethodBase; }
579 public override Type DeclaringType
581 get { return owner as Type; }
585 sealed class MissingMethod : MethodInfo
587 private readonly Type declaringType;
588 private readonly string name;
589 internal MethodSignature signature;
590 private MethodInfo forwarder;
591 private Type[] typeArgs;
593 internal MissingMethod(Type declaringType, string name, MethodSignature signature)
595 this.declaringType = declaringType;
597 this.signature = signature;
600 private MethodInfo Forwarder
604 MethodInfo method = TryGetForwarder();
607 throw new MissingMemberException(this);
613 private MethodInfo TryGetForwarder()
615 if (forwarder == null && !declaringType.__IsMissing)
617 MethodBase mb = declaringType.FindMethod(name, signature);
618 ConstructorInfo ci = mb as ConstructorInfo;
621 forwarder = ci.GetMethodInfo();
625 forwarder = (MethodInfo)mb;
631 public override bool __IsMissing
633 get { return TryGetForwarder() == null; }
636 public override Type ReturnType
638 get { return signature.GetReturnType(this); }
641 public override ParameterInfo ReturnParameter
643 get { return new ParameterInfoImpl(this, -1); }
646 internal override MethodSignature MethodSignature
648 get { return signature; }
651 internal override int ParameterCount
653 get { return signature.GetParameterCount(); }
656 private sealed class ParameterInfoImpl : ParameterInfo
658 private readonly MissingMethod method;
659 private readonly int index;
661 internal ParameterInfoImpl(MissingMethod method, int index)
663 this.method = method;
667 private ParameterInfo Forwarder
669 get { return index == -1 ? method.Forwarder.ReturnParameter : method.Forwarder.GetParameters()[index]; }
672 public override string Name
674 get { return Forwarder.Name; }
677 public override Type ParameterType
679 get { return index == -1 ? method.signature.GetReturnType(method) : method.signature.GetParameterType(method, index); }
682 public override ParameterAttributes Attributes
684 get { return Forwarder.Attributes; }
687 public override int Position
689 get { return index; }
692 public override object RawDefaultValue
694 get { return Forwarder.RawDefaultValue; }
697 public override CustomModifiers __GetCustomModifiers()
700 ? method.signature.GetReturnTypeCustomModifiers(method)
701 : method.signature.GetParameterCustomModifiers(method, index);
704 public override MemberInfo Member
706 get { return method; }
709 public override int MetadataToken
711 get { return Forwarder.MetadataToken; }
714 internal override Module Module
716 get { return method.Module; }
719 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
721 return Forwarder.GetCustomAttributesData(attributeType);
724 public override string ToString()
726 return Forwarder.ToString();
730 public override ParameterInfo[] GetParameters()
732 ParameterInfo[] parameters = new ParameterInfo[signature.GetParameterCount()];
733 for (int i = 0; i < parameters.Length; i++)
735 parameters[i] = new ParameterInfoImpl(this, i);
740 public override MethodAttributes Attributes
742 get { return Forwarder.Attributes; }
745 public override MethodImplAttributes GetMethodImplementationFlags()
747 return Forwarder.GetMethodImplementationFlags();
750 public override MethodBody GetMethodBody()
752 return Forwarder.GetMethodBody();
755 public override CallingConventions CallingConvention
757 get { return signature.CallingConvention; }
760 internal override int ImportTo(IKVM.Reflection.Emit.ModuleBuilder module)
762 MethodInfo method = TryGetForwarder();
765 return method.ImportTo(module);
767 return module.ImportMethodOrField(declaringType, this.Name, this.MethodSignature);
770 public override string Name
775 public override Type DeclaringType
777 get { return declaringType.IsModulePseudoType ? null : declaringType; }
780 public override Module Module
782 get { return declaringType.Module; }
785 public override bool Equals(object obj)
787 MissingMethod other = obj as MissingMethod;
789 && other.declaringType == declaringType
790 && other.name == name
791 && other.signature.Equals(signature);
794 public override int GetHashCode()
796 return declaringType.GetHashCode() ^ name.GetHashCode() ^ signature.GetHashCode();
799 internal override MethodBase BindTypeParameters(Type type)
801 MethodInfo forwarder = TryGetForwarder();
802 if (forwarder != null)
804 return forwarder.BindTypeParameters(type);
806 return new GenericMethodInstance(type, this, null);
809 public override bool ContainsGenericParameters
811 get { return Forwarder.ContainsGenericParameters; }
814 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
816 return Forwarder.GetCustomAttributesData(attributeType);
819 public override Type[] GetGenericArguments()
821 MethodInfo method = TryGetForwarder();
824 return Forwarder.GetGenericArguments();
826 if (typeArgs == null)
828 typeArgs = new Type[signature.GenericParameterCount];
829 for (int i = 0; i < typeArgs.Length; i++)
831 typeArgs[i] = new MissingTypeParameter(this, i);
834 return Util.Copy(typeArgs);
837 internal override Type GetGenericMethodArgument(int index)
839 return GetGenericArguments()[index];
842 internal override int GetGenericMethodArgumentCount()
844 return Forwarder.GetGenericMethodArgumentCount();
847 public override MethodInfo GetGenericMethodDefinition()
849 return Forwarder.GetGenericMethodDefinition();
852 internal override MethodInfo GetMethodOnTypeDefinition()
854 return Forwarder.GetMethodOnTypeDefinition();
857 internal override bool HasThis
859 get { return (signature.CallingConvention & (CallingConventions.HasThis | CallingConventions.ExplicitThis)) == CallingConventions.HasThis; }
862 public override bool IsGenericMethod
864 get { return IsGenericMethodDefinition; }
867 public override bool IsGenericMethodDefinition
869 get { return signature.GenericParameterCount != 0; }
872 public override MethodInfo MakeGenericMethod(params Type[] typeArguments)
874 MethodInfo method = TryGetForwarder();
877 return method.MakeGenericMethod(typeArguments);
879 return new GenericMethodInstance(declaringType, this, typeArguments);
882 public override int MetadataToken
884 get { return Forwarder.MetadataToken; }
888 sealed class MissingField : FieldInfo
890 private readonly Type declaringType;
891 private readonly string name;
892 private readonly FieldSignature signature;
893 private FieldInfo forwarder;
895 internal MissingField(Type declaringType, string name, FieldSignature signature)
897 this.declaringType = declaringType;
899 this.signature = signature;
902 private FieldInfo Forwarder
906 FieldInfo field = TryGetForwarder();
909 throw new MissingMemberException(this);
915 private FieldInfo TryGetForwarder()
917 if (forwarder == null && !declaringType.__IsMissing)
919 forwarder = declaringType.FindField(name, signature);
924 public override bool __IsMissing
926 get { return TryGetForwarder() == null; }
929 public override FieldAttributes Attributes
931 get { return Forwarder.Attributes; }
934 public override void __GetDataFromRVA(byte[] data, int offset, int length)
936 Forwarder.__GetDataFromRVA(data, offset, length);
939 public override int __FieldRVA
941 get { return Forwarder.__FieldRVA; }
944 public override object GetRawConstantValue()
946 return Forwarder.GetRawConstantValue();
949 internal override FieldSignature FieldSignature
951 get { return signature; }
954 internal override int ImportTo(IKVM.Reflection.Emit.ModuleBuilder module)
956 FieldInfo field = TryGetForwarder();
959 return field.ImportTo(module);
961 return module.ImportMethodOrField(declaringType, this.Name, this.FieldSignature);
964 public override string Name
969 public override Type DeclaringType
971 get { return declaringType.IsModulePseudoType ? null : declaringType; }
974 public override Module Module
976 get { return declaringType.Module; }
979 internal override FieldInfo BindTypeParameters(Type type)
981 FieldInfo forwarder = TryGetForwarder();
982 if (forwarder != null)
984 return forwarder.BindTypeParameters(type);
986 return new GenericFieldInstance(type, this);
989 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
991 return Forwarder.GetCustomAttributesData(attributeType);
994 public override int MetadataToken
996 get { return Forwarder.MetadataToken; }
999 public override bool Equals(object obj)
1001 MissingField other = obj as MissingField;
1002 return other != null
1003 && other.declaringType == declaringType
1004 && other.name == name
1005 && other.signature.Equals(signature);
1008 public override int GetHashCode()
1010 return declaringType.GetHashCode() ^ name.GetHashCode() ^ signature.GetHashCode();
1013 public override string ToString()
1015 return this.FieldType.Name + " " + this.Name;
1019 // NOTE this is currently only used by CustomAttributeData (because there is no other way to refer to a property)
1020 sealed class MissingProperty : PropertyInfo
1022 private readonly Type declaringType;
1023 private readonly string name;
1024 private readonly PropertySignature signature;
1026 internal MissingProperty(Type declaringType, string name, PropertySignature signature)
1028 this.declaringType = declaringType;
1030 this.signature = signature;
1033 public override PropertyAttributes Attributes
1035 get { throw new MissingMemberException(this); }
1038 public override bool CanRead
1040 get { throw new MissingMemberException(this); }
1043 public override bool CanWrite
1045 get { throw new MissingMemberException(this); }
1048 public override MethodInfo GetGetMethod(bool nonPublic)
1050 throw new MissingMemberException(this);
1053 public override MethodInfo GetSetMethod(bool nonPublic)
1055 throw new MissingMemberException(this);
1058 public override MethodInfo[] GetAccessors(bool nonPublic)
1060 throw new MissingMemberException(this);
1063 public override object GetRawConstantValue()
1065 throw new MissingMemberException(this);
1068 internal override bool IsPublic
1070 get { throw new MissingMemberException(this); }
1073 internal override bool IsStatic
1075 get { throw new MissingMemberException(this); }
1078 internal override PropertySignature PropertySignature
1080 get { return signature; }
1083 public override string Name
1085 get { return name; }
1088 public override Type DeclaringType
1090 get { return declaringType; }
1093 public override Module Module
1095 get { return declaringType.Module; }