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;
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 IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
228 throw new MissingAssemblyException(this);
232 sealed class MissingModule : NonPEModule
234 private readonly MissingAssembly assembly;
236 internal MissingModule(MissingAssembly assembly)
237 : base(assembly.universe)
239 this.assembly = assembly;
242 public override int MDStreamVersion
244 get { throw new MissingModuleException(this); }
247 public override Assembly Assembly
249 get { return assembly; }
252 public override string FullyQualifiedName
254 get { throw new MissingModuleException(this); }
257 public override string Name
259 get { throw new MissingModuleException(this); }
262 public override Guid ModuleVersionId
264 get { throw new MissingModuleException(this); }
267 public override string ScopeName
269 get { throw new MissingModuleException(this); }
272 internal override Type FindType(TypeName typeName)
277 internal override void GetTypesImpl(System.Collections.Generic.List<Type> list)
279 throw new MissingModuleException(this);
282 public override void __GetDataDirectoryEntry(int index, out int rva, out int length)
284 throw new MissingModuleException(this);
287 public override IList<CustomAttributeData> __GetPlaceholderAssemblyCustomAttributes(bool multiple, bool security)
289 throw new MissingModuleException(this);
292 public override long __RelativeVirtualAddressToFileOffset(int rva)
294 throw new MissingModuleException(this);
297 public override __StandAloneMethodSig __ResolveStandAloneMethodSig(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
299 throw new MissingModuleException(this);
302 public override int __Subsystem
304 get { throw new MissingModuleException(this); }
307 internal override void ExportTypes(int fileToken, IKVM.Reflection.Emit.ModuleBuilder manifestModule)
309 throw new MissingModuleException(this);
312 public override void GetPEKind(out PortableExecutableKinds peKind, out ImageFileMachine machine)
314 throw new MissingModuleException(this);
317 public override bool __IsMissing
322 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
324 throw new MissingModuleException(this);
327 protected override Exception InvalidOperationException()
329 return new MissingModuleException(this);
332 protected override Exception NotSupportedException()
334 return new MissingModuleException(this);
337 protected override Exception ArgumentOutOfRangeException()
339 return new MissingModuleException(this);
343 sealed class MissingType : Type
345 private readonly Module module;
346 private readonly Type declaringType;
347 private readonly string ns;
348 private readonly string name;
349 private Type[] typeArgs;
352 internal MissingType(Module module, Type declaringType, string ns, string name)
354 this.module = module;
355 this.declaringType = declaringType;
358 MarkEnumOrValueType(ns, name);
361 internal override MethodBase FindMethod(string name, MethodSignature signature)
363 MethodInfo method = new MissingMethod(this, name, signature);
366 return new ConstructorInfoImpl(method);
371 internal override FieldInfo FindField(string name, FieldSignature signature)
373 return new MissingField(this, name, signature);
376 internal override Type FindNestedType(TypeName name)
381 public override bool __IsMissing
386 public override Type DeclaringType
388 get { return declaringType; }
391 public override string __Name
396 public override string __Namespace
401 public override string Name
403 get { return TypeNameParser.Escape(name); }
406 public override string FullName
408 get { return GetFullName(); }
411 public override Module Module
413 get { return module; }
416 public override int MetadataToken
418 get { return token; }
421 public override bool IsValueType
425 switch (typeFlags & (TypeFlags.ValueType | TypeFlags.NotValueType))
427 case TypeFlags.ValueType:
429 case TypeFlags.NotValueType:
432 throw new MissingMemberException(this);
437 public override Type BaseType
439 get { throw new MissingMemberException(this); }
442 public override TypeAttributes Attributes
444 get { throw new MissingMemberException(this); }
447 public override Type[] __GetDeclaredTypes()
449 throw new MissingMemberException(this);
452 public override Type[] __GetDeclaredInterfaces()
454 throw new MissingMemberException(this);
457 public override MethodBase[] __GetDeclaredMethods()
459 throw new MissingMemberException(this);
462 public override __MethodImplMap __GetMethodImplMap()
464 throw new MissingMemberException(this);
467 public override FieldInfo[] __GetDeclaredFields()
469 throw new MissingMemberException(this);
472 public override EventInfo[] __GetDeclaredEvents()
474 throw new MissingMemberException(this);
477 public override PropertyInfo[] __GetDeclaredProperties()
479 throw new MissingMemberException(this);
482 public override CustomModifiers __GetCustomModifiers()
484 throw new MissingMemberException(this);
487 public override Type[] GetGenericArguments()
489 throw new MissingMemberException(this);
492 public override CustomModifiers[] __GetGenericArgumentsCustomModifiers()
494 throw new MissingMemberException(this);
497 public override StructLayoutAttribute StructLayoutAttribute
499 get { throw new MissingMemberException(this); }
502 public override bool IsGenericType
504 get { throw new MissingMemberException(this); }
507 public override bool IsGenericTypeDefinition
509 get { throw new MissingMemberException(this); }
512 internal override Type GetGenericTypeArgument(int index)
514 if (typeArgs == null)
516 typeArgs = new Type[index + 1];
518 else if (typeArgs.Length <= index)
520 Array.Resize(ref typeArgs, index + 1);
522 return typeArgs[index] ?? (typeArgs[index] = new MissingTypeParameter(this, index));
525 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
527 throw new MissingMemberException(this);
530 internal override Type BindTypeParameters(IGenericBinder binder)
535 internal override Type SetMetadataTokenForMissing(int token)
542 sealed class MissingTypeParameter : IKVM.Reflection.Reader.TypeParameterType
544 private readonly MemberInfo owner;
545 private readonly int index;
547 internal MissingTypeParameter(MemberInfo owner, int index)
553 public override Module Module
555 get { return owner.Module; }
558 public override string Name
563 public override int GenericParameterPosition
565 get { return index; }
568 public override MethodBase DeclaringMethod
570 get { return owner as MethodBase; }
573 public override Type DeclaringType
575 get { return owner as Type; }
578 internal override Type BindTypeParameters(IGenericBinder binder)
580 if (owner is MethodBase)
582 return binder.BindMethodParameter(this);
586 return binder.BindTypeParameter(this);
591 sealed class MissingMethod : MethodInfo
593 private readonly Type declaringType;
594 private readonly string name;
595 internal MethodSignature signature;
596 private MethodInfo forwarder;
597 private Type[] typeArgs;
599 internal MissingMethod(Type declaringType, string name, MethodSignature signature)
601 this.declaringType = declaringType;
603 this.signature = signature;
606 private MethodInfo Forwarder
610 MethodInfo method = TryGetForwarder();
613 throw new MissingMemberException(this);
619 private MethodInfo TryGetForwarder()
621 if (forwarder == null && !declaringType.__IsMissing)
623 MethodBase mb = declaringType.FindMethod(name, signature);
624 ConstructorInfo ci = mb as ConstructorInfo;
627 forwarder = ci.GetMethodInfo();
631 forwarder = (MethodInfo)mb;
637 public override bool __IsMissing
639 get { return TryGetForwarder() == null; }
642 public override Type ReturnType
644 get { return signature.GetReturnType(this); }
647 public override ParameterInfo ReturnParameter
649 get { return new ParameterInfoImpl(this, -1); }
652 internal override MethodSignature MethodSignature
654 get { return signature; }
657 internal override int ParameterCount
659 get { return signature.GetParameterCount(); }
662 private sealed class ParameterInfoImpl : ParameterInfo
664 private readonly MissingMethod method;
665 private readonly int index;
667 internal ParameterInfoImpl(MissingMethod method, int index)
669 this.method = method;
673 private ParameterInfo Forwarder
675 get { return index == -1 ? method.Forwarder.ReturnParameter : method.Forwarder.GetParameters()[index]; }
678 public override string Name
680 get { return Forwarder.Name; }
683 public override Type ParameterType
685 get { return index == -1 ? method.signature.GetReturnType(method) : method.signature.GetParameterType(method, index); }
688 public override ParameterAttributes Attributes
690 get { return Forwarder.Attributes; }
693 public override int Position
695 get { return index; }
698 public override object RawDefaultValue
700 get { return Forwarder.RawDefaultValue; }
703 public override CustomModifiers __GetCustomModifiers()
706 ? method.signature.GetReturnTypeCustomModifiers(method)
707 : method.signature.GetParameterCustomModifiers(method, index);
710 public override MemberInfo Member
712 get { return method; }
715 public override int MetadataToken
717 get { return Forwarder.MetadataToken; }
720 internal override Module Module
722 get { return method.Module; }
725 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
727 return Forwarder.GetCustomAttributesData(attributeType);
730 public override string ToString()
732 return Forwarder.ToString();
736 public override ParameterInfo[] GetParameters()
738 ParameterInfo[] parameters = new ParameterInfo[signature.GetParameterCount()];
739 for (int i = 0; i < parameters.Length; i++)
741 parameters[i] = new ParameterInfoImpl(this, i);
746 public override MethodAttributes Attributes
748 get { return Forwarder.Attributes; }
751 public override MethodImplAttributes GetMethodImplementationFlags()
753 return Forwarder.GetMethodImplementationFlags();
756 public override MethodBody GetMethodBody()
758 return Forwarder.GetMethodBody();
761 public override int __MethodRVA
763 get { return Forwarder.__MethodRVA; }
766 public override CallingConventions CallingConvention
768 get { return signature.CallingConvention; }
771 internal override int ImportTo(IKVM.Reflection.Emit.ModuleBuilder module)
773 MethodInfo method = TryGetForwarder();
776 return method.ImportTo(module);
778 return module.ImportMethodOrField(declaringType, this.Name, this.MethodSignature);
781 public override string Name
786 public override Type DeclaringType
788 get { return declaringType.IsModulePseudoType ? null : declaringType; }
791 public override Module Module
793 get { return declaringType.Module; }
796 public override bool Equals(object obj)
798 MissingMethod other = obj as MissingMethod;
800 && other.declaringType == declaringType
801 && other.name == name
802 && other.signature.Equals(signature);
805 public override int GetHashCode()
807 return declaringType.GetHashCode() ^ name.GetHashCode() ^ signature.GetHashCode();
810 internal override MethodBase BindTypeParameters(Type type)
812 MethodInfo forwarder = TryGetForwarder();
813 if (forwarder != null)
815 return forwarder.BindTypeParameters(type);
817 return new GenericMethodInstance(type, this, null);
820 public override bool ContainsGenericParameters
822 get { return Forwarder.ContainsGenericParameters; }
825 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
827 return Forwarder.GetCustomAttributesData(attributeType);
830 public override Type[] GetGenericArguments()
832 MethodInfo method = TryGetForwarder();
835 return Forwarder.GetGenericArguments();
837 if (typeArgs == null)
839 typeArgs = new Type[signature.GenericParameterCount];
840 for (int i = 0; i < typeArgs.Length; i++)
842 typeArgs[i] = new MissingTypeParameter(this, i);
845 return Util.Copy(typeArgs);
848 internal override Type GetGenericMethodArgument(int index)
850 return GetGenericArguments()[index];
853 internal override int GetGenericMethodArgumentCount()
855 return Forwarder.GetGenericMethodArgumentCount();
858 public override MethodInfo GetGenericMethodDefinition()
860 return Forwarder.GetGenericMethodDefinition();
863 internal override MethodInfo GetMethodOnTypeDefinition()
865 return Forwarder.GetMethodOnTypeDefinition();
868 internal override bool HasThis
870 get { return (signature.CallingConvention & (CallingConventions.HasThis | CallingConventions.ExplicitThis)) == CallingConventions.HasThis; }
873 public override bool IsGenericMethod
875 get { return IsGenericMethodDefinition; }
878 public override bool IsGenericMethodDefinition
880 get { return signature.GenericParameterCount != 0; }
883 public override MethodInfo MakeGenericMethod(params Type[] typeArguments)
885 MethodInfo method = TryGetForwarder();
888 return method.MakeGenericMethod(typeArguments);
890 return new GenericMethodInstance(declaringType, this, typeArguments);
893 public override int MetadataToken
895 get { return Forwarder.MetadataToken; }
899 sealed class MissingField : FieldInfo
901 private readonly Type declaringType;
902 private readonly string name;
903 private readonly FieldSignature signature;
904 private FieldInfo forwarder;
906 internal MissingField(Type declaringType, string name, FieldSignature signature)
908 this.declaringType = declaringType;
910 this.signature = signature;
913 private FieldInfo Forwarder
917 FieldInfo field = TryGetForwarder();
920 throw new MissingMemberException(this);
926 private FieldInfo TryGetForwarder()
928 if (forwarder == null && !declaringType.__IsMissing)
930 forwarder = declaringType.FindField(name, signature);
935 public override bool __IsMissing
937 get { return TryGetForwarder() == null; }
940 public override FieldAttributes Attributes
942 get { return Forwarder.Attributes; }
945 public override void __GetDataFromRVA(byte[] data, int offset, int length)
947 Forwarder.__GetDataFromRVA(data, offset, length);
950 public override int __FieldRVA
952 get { return Forwarder.__FieldRVA; }
955 public override object GetRawConstantValue()
957 return Forwarder.GetRawConstantValue();
960 internal override FieldSignature FieldSignature
962 get { return signature; }
965 internal override int ImportTo(IKVM.Reflection.Emit.ModuleBuilder module)
967 FieldInfo field = TryGetForwarder();
970 return field.ImportTo(module);
972 return module.ImportMethodOrField(declaringType, this.Name, this.FieldSignature);
975 public override string Name
980 public override Type DeclaringType
982 get { return declaringType.IsModulePseudoType ? null : declaringType; }
985 public override Module Module
987 get { return declaringType.Module; }
990 internal override FieldInfo BindTypeParameters(Type type)
992 FieldInfo forwarder = TryGetForwarder();
993 if (forwarder != null)
995 return forwarder.BindTypeParameters(type);
997 return new GenericFieldInstance(type, this);
1000 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
1002 return Forwarder.GetCustomAttributesData(attributeType);
1005 public override int MetadataToken
1007 get { return Forwarder.MetadataToken; }
1010 public override bool Equals(object obj)
1012 MissingField other = obj as MissingField;
1013 return other != null
1014 && other.declaringType == declaringType
1015 && other.name == name
1016 && other.signature.Equals(signature);
1019 public override int GetHashCode()
1021 return declaringType.GetHashCode() ^ name.GetHashCode() ^ signature.GetHashCode();
1024 public override string ToString()
1026 return this.FieldType.Name + " " + this.Name;
1030 // NOTE this is currently only used by CustomAttributeData (because there is no other way to refer to a property)
1031 sealed class MissingProperty : PropertyInfo
1033 private readonly Type declaringType;
1034 private readonly string name;
1035 private readonly PropertySignature signature;
1037 internal MissingProperty(Type declaringType, string name, PropertySignature signature)
1039 this.declaringType = declaringType;
1041 this.signature = signature;
1044 public override PropertyAttributes Attributes
1046 get { throw new MissingMemberException(this); }
1049 public override bool CanRead
1051 get { throw new MissingMemberException(this); }
1054 public override bool CanWrite
1056 get { throw new MissingMemberException(this); }
1059 public override MethodInfo GetGetMethod(bool nonPublic)
1061 throw new MissingMemberException(this);
1064 public override MethodInfo GetSetMethod(bool nonPublic)
1066 throw new MissingMemberException(this);
1069 public override MethodInfo[] GetAccessors(bool nonPublic)
1071 throw new MissingMemberException(this);
1074 public override object GetRawConstantValue()
1076 throw new MissingMemberException(this);
1079 internal override bool IsPublic
1081 get { throw new MissingMemberException(this); }
1084 internal override bool IsStatic
1086 get { throw new MissingMemberException(this); }
1089 internal override PropertySignature PropertySignature
1091 get { return signature; }
1094 public override string Name
1096 get { return name; }
1099 public override Type DeclaringType
1101 get { return declaringType; }
1104 public override Module Module
1106 get { return declaringType.Module; }