3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 ////////////////////////////////////////////////////////////////////////////////
7 // <OWNER>[....]</OWNER>
8 // <OWNER>[....]</OWNER>
11 namespace System.Reflection
14 using System.Diagnostics.SymbolStore;
15 using System.Runtime.Remoting;
16 using System.Runtime.InteropServices;
17 using System.Runtime.Serialization;
18 using System.Collections;
19 using System.Collections.Generic;
20 using System.Threading;
21 using System.Runtime.CompilerServices;
22 using System.Security;
23 using System.Security.Permissions;
25 using System.Globalization;
26 using System.Runtime.Versioning;
27 using System.Diagnostics.Contracts;
31 [System.Runtime.InteropServices.ComVisible(true)]
32 public enum PortableExecutableKinds
34 NotAPortableExecutableImage = 0x0,
45 Preferred32Bit = 0x10,
49 [System.Runtime.InteropServices.ComVisible(true)]
50 public enum ImageFileMachine
62 [ClassInterface(ClassInterfaceType.None)]
63 [ComDefaultInterface(typeof(_Module))]
64 [System.Runtime.InteropServices.ComVisible(true)]
65 #pragma warning disable 618
66 [PermissionSetAttribute(SecurityAction.InheritanceDemand, Unrestricted = true)]
67 #pragma warning restore 618
68 public abstract class Module : _Module, ISerializable, ICustomAttributeProvider
70 #region Static Constructor
74 _fltObj = new __Filters();
75 FilterTypeName = new TypeFilter(_fltObj.FilterTypeName);
76 FilterTypeNameIgnoreCase = new TypeFilter(_fltObj.FilterTypeNameIgnoreCase);
86 #region Public Statics
87 public static readonly TypeFilter FilterTypeName;
88 public static readonly TypeFilter FilterTypeNameIgnoreCase;
91 public static bool operator ==(Module left, Module right)
93 if (ReferenceEquals(left, right))
96 if ((object)left == null || (object)right == null ||
97 left is RuntimeModule || right is RuntimeModule)
102 return left.Equals(right);
105 public static bool operator !=(Module left, Module right)
107 return !(left == right);
109 #endif // !FEATURE_CORECLR
110 #if FEATURE_NETCORE || !FEATURE_CORECLR
111 public override bool Equals(object o)
113 return base.Equals(o);
116 public override int GetHashCode()
118 return base.GetHashCode();
120 #endif //FEATURE_NETCORE || !FEATURE_CORECLR
124 private const BindingFlags DefaultLookup = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
127 #region object overrides
128 public override String ToString()
134 public virtual IEnumerable<CustomAttributeData> CustomAttributes
138 return GetCustomAttributesData();
141 #region ICustomAttributeProvider Members
142 public virtual Object[] GetCustomAttributes(bool inherit)
144 throw new NotImplementedException();
147 public virtual Object[] GetCustomAttributes(Type attributeType, bool inherit)
149 throw new NotImplementedException();
152 public virtual bool IsDefined(Type attributeType, bool inherit)
154 throw new NotImplementedException();
157 public virtual IList<CustomAttributeData> GetCustomAttributesData()
159 throw new NotImplementedException();
163 #region public instances members
164 public MethodBase ResolveMethod(int metadataToken)
166 return ResolveMethod(metadataToken, null, null);
169 public virtual MethodBase ResolveMethod(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
171 // This API was made virtual in V4. Code compiled against V2 might use
172 // "call" rather than "callvirt" to call it.
173 // This makes sure those code still works.
174 RuntimeModule rtModule = this as RuntimeModule;
175 if (rtModule != null)
176 return rtModule.ResolveMethod(metadataToken, genericTypeArguments, genericMethodArguments);
178 throw new NotImplementedException();
181 public FieldInfo ResolveField(int metadataToken)
183 return ResolveField(metadataToken, null, null);
186 public virtual FieldInfo ResolveField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
188 // This API was made virtual in V4. Code compiled against V2 might use
189 // "call" rather than "callvirt" to call it.
190 // This makes sure those code still works.
191 RuntimeModule rtModule = this as RuntimeModule;
192 if (rtModule != null)
193 return rtModule.ResolveField(metadataToken, genericTypeArguments, genericMethodArguments);
195 throw new NotImplementedException();
198 public Type ResolveType(int metadataToken)
200 return ResolveType(metadataToken, null, null);
203 public virtual Type ResolveType(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
205 // This API was made virtual in V4. Code compiled against V2 might use
206 // "call" rather than "callvirt" to call it.
207 // This makes sure those code still works.
208 RuntimeModule rtModule = this as RuntimeModule;
209 if (rtModule != null)
210 return rtModule.ResolveType(metadataToken, genericTypeArguments, genericMethodArguments);
212 throw new NotImplementedException();
215 public MemberInfo ResolveMember(int metadataToken)
217 return ResolveMember(metadataToken, null, null);
220 public virtual MemberInfo ResolveMember(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
222 // This API was made virtual in V4. Code compiled against V2 might use
223 // "call" rather than "callvirt" to call it.
224 // This makes sure those code still works.
225 RuntimeModule rtModule = this as RuntimeModule;
226 if (rtModule != null)
227 return rtModule.ResolveMember(metadataToken, genericTypeArguments, genericMethodArguments);
229 throw new NotImplementedException();
232 public virtual byte[] ResolveSignature(int metadataToken)
234 // This API was made virtual in V4. Code compiled against V2 might use
235 // "call" rather than "callvirt" to call it.
236 // This makes sure those code still works.
237 RuntimeModule rtModule = this as RuntimeModule;
238 if (rtModule != null)
239 return rtModule.ResolveSignature(metadataToken);
241 throw new NotImplementedException();
244 public virtual string ResolveString(int metadataToken)
246 // This API was made virtual in V4. Code compiled against V2 might use
247 // "call" rather than "callvirt" to call it.
248 // This makes sure those code still works.
249 RuntimeModule rtModule = this as RuntimeModule;
250 if (rtModule != null)
251 return rtModule.ResolveString(metadataToken);
253 throw new NotImplementedException();
256 public virtual void GetPEKind(out PortableExecutableKinds peKind, out ImageFileMachine machine)
258 // This API was made virtual in V4. Code compiled against V2 might use
259 // "call" rather than "callvirt" to call it.
260 // This makes sure those code still works.
261 RuntimeModule rtModule = this as RuntimeModule;
262 if (rtModule != null)
263 rtModule.GetPEKind(out peKind, out machine);
265 throw new NotImplementedException();
268 public virtual int MDStreamVersion
272 // This API was made virtual in V4. Code compiled against V2 might use
273 // "call" rather than "callvirt" to call it.
274 // This makes sure those code still works.
275 RuntimeModule rtModule = this as RuntimeModule;
276 if (rtModule != null)
277 return rtModule.MDStreamVersion;
279 throw new NotImplementedException();
283 [System.Security.SecurityCritical] // auto-generated_required
284 public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
286 throw new NotImplementedException();
289 [System.Runtime.InteropServices.ComVisible(true)]
290 public virtual Type GetType(String className, bool ignoreCase)
292 return GetType(className, false, ignoreCase);
295 [System.Runtime.InteropServices.ComVisible(true)]
296 public virtual Type GetType(String className) {
297 return GetType(className, false, false);
300 [System.Runtime.InteropServices.ComVisible(true)]
301 public virtual Type GetType(String className, bool throwOnError, bool ignoreCase)
303 throw new NotImplementedException();
306 public virtual String FullyQualifiedName
309 [System.Security.SecurityCritical] // auto-generated
311 [ResourceExposure(ResourceScope.Machine)]
312 [ResourceConsumption(ResourceScope.Machine)]
315 throw new NotImplementedException();
319 public virtual Type[] FindTypes(TypeFilter filter,Object filterCriteria)
321 Type[] c = GetTypes();
323 for (int i = 0;i<c.Length;i++) {
324 if (filter!=null && !filter(c[i],filterCriteria))
332 Type[] ret = new Type[cnt];
334 for (int i=0;i<c.Length;i++) {
341 public virtual Type[] GetTypes()
343 throw new NotImplementedException();
346 public virtual Guid ModuleVersionId
350 // This API was made virtual in V4. Code compiled against V2 might use
351 // "call" rather than "callvirt" to call it.
352 // This makes sure those code still works.
353 RuntimeModule rtModule = this as RuntimeModule;
354 if (rtModule != null)
355 return rtModule.ModuleVersionId;
357 throw new NotImplementedException();
361 public virtual int MetadataToken
365 // This API was made virtual in V4. Code compiled against V2 might use
366 // "call" rather than "callvirt" to call it.
367 // This makes sure those code still works.
368 RuntimeModule rtModule = this as RuntimeModule;
369 if (rtModule != null)
370 return rtModule.MetadataToken;
372 throw new NotImplementedException();
376 public virtual bool IsResource()
378 // This API was made virtual in V4. Code compiled against V2 might use
379 // "call" rather than "callvirt" to call it.
380 // This makes sure those code still works.
381 RuntimeModule rtModule = this as RuntimeModule;
382 if (rtModule != null)
383 return rtModule.IsResource();
385 throw new NotImplementedException();
388 public FieldInfo[] GetFields()
390 return GetFields(Module.DefaultLookup);
393 public virtual FieldInfo[] GetFields(BindingFlags bindingFlags)
395 // This API was made virtual in V4. Code compiled against V2 might use
396 // "call" rather than "callvirt" to call it.
397 // This makes sure those code still works.
398 RuntimeModule rtModule = this as RuntimeModule;
399 if (rtModule != null)
400 return rtModule.GetFields(bindingFlags);
402 throw new NotImplementedException();
405 public FieldInfo GetField(String name)
407 return GetField(name,Module.DefaultLookup);
410 public virtual FieldInfo GetField(String name, BindingFlags bindingAttr)
412 // This API was made virtual in V4. Code compiled against V2 might use
413 // "call" rather than "callvirt" to call it.
414 // This makes sure those code still works.
415 RuntimeModule rtModule = this as RuntimeModule;
416 if (rtModule != null)
417 return rtModule.GetField(name, bindingAttr);
419 throw new NotImplementedException();
422 public MethodInfo[] GetMethods()
424 return GetMethods(Module.DefaultLookup);
427 public virtual MethodInfo[] GetMethods(BindingFlags bindingFlags)
429 // This API was made virtual in V4. Code compiled against V2 might use
430 // "call" rather than "callvirt" to call it.
431 // This makes sure those code still works.
432 RuntimeModule rtModule = this as RuntimeModule;
433 if (rtModule != null)
434 return rtModule.GetMethods(bindingFlags);
436 throw new NotImplementedException();
439 public MethodInfo GetMethod(
440 String name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
443 throw new ArgumentNullException("name");
446 throw new ArgumentNullException("types");
447 Contract.EndContractBlock();
449 for (int i = 0; i < types.Length; i++)
451 if (types[i] == null)
452 throw new ArgumentNullException("types");
455 return GetMethodImpl(name, bindingAttr, binder, callConvention, types, modifiers);
458 public MethodInfo GetMethod(String name, Type[] types)
461 throw new ArgumentNullException("name");
464 throw new ArgumentNullException("types");
465 Contract.EndContractBlock();
467 for (int i = 0; i < types.Length; i++)
469 if (types[i] == null)
470 throw new ArgumentNullException("types");
473 return GetMethodImpl(name, Module.DefaultLookup, null, CallingConventions.Any, types, null);
476 public MethodInfo GetMethod(String name)
479 throw new ArgumentNullException("name");
480 Contract.EndContractBlock();
482 return GetMethodImpl(name, Module.DefaultLookup, null, CallingConventions.Any,
486 protected virtual MethodInfo GetMethodImpl(String name, BindingFlags bindingAttr, Binder binder,
487 CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
489 throw new NotImplementedException();
492 public virtual String ScopeName
496 // This API was made virtual in V4. Code compiled against V2 might use
497 // "call" rather than "callvirt" to call it.
498 // This makes sure those code still works.
499 RuntimeModule rtModule = this as RuntimeModule;
500 if (rtModule != null)
501 return rtModule.ScopeName;
503 throw new NotImplementedException();
507 public virtual String Name
509 [ResourceExposure(ResourceScope.None)]
510 [ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
513 // This API was made virtual in V4. Code compiled against V2 might use
514 // "call" rather than "callvirt" to call it.
515 // This makes sure those code still works.
516 RuntimeModule rtModule = this as RuntimeModule;
517 if (rtModule != null)
518 return rtModule.Name;
520 throw new NotImplementedException();
524 public virtual Assembly Assembly
529 // This API was made virtual in V4. Code compiled against V2 might use
530 // "call" rather than "callvirt" to call it.
531 // This makes sure those code still works.
532 RuntimeModule rtModule = this as RuntimeModule;
533 if (rtModule != null)
534 return rtModule.Assembly;
536 throw new NotImplementedException();
540 // This API never fails, it will return an empty handle for non-runtime handles and
541 // a valid handle for reflection only modules.
542 public ModuleHandle ModuleHandle
546 return GetModuleHandle();
550 // Used to provide implementation and overriding point for ModuleHandle.
551 // To get a module handle inside mscorlib, use GetNativeHandle instead.
552 internal virtual ModuleHandle GetModuleHandle()
554 return ModuleHandle.EmptyHandle;
557 #if FEATURE_X509 && FEATURE_CAS_POLICY
558 public virtual System.Security.Cryptography.X509Certificates.X509Certificate GetSignerCertificate()
560 throw new NotImplementedException();
562 #endif // FEATURE_X509 && FEATURE_CAS_POLICY
565 void _Module.GetTypeInfoCount(out uint pcTInfo)
567 throw new NotImplementedException();
570 void _Module.GetTypeInfo(uint iTInfo, uint lcid, IntPtr ppTInfo)
572 throw new NotImplementedException();
575 void _Module.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
577 throw new NotImplementedException();
580 void _Module.Invoke(uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
582 throw new NotImplementedException();
587 [System.Runtime.ForceTokenStabilization]
588 #endif //!FEATURE_CORECLR
590 internal class RuntimeModule : Module
592 internal RuntimeModule() { throw new NotSupportedException(); }
595 [System.Security.SecurityCritical] // auto-generated
596 [ResourceExposure(ResourceScope.None)]
597 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
598 [SuppressUnmanagedCodeSecurity]
599 private extern static void GetType(RuntimeModule module, String className, bool ignoreCase, bool throwOnError, ObjectHandleOnStack type);
601 [System.Security.SecurityCritical]
602 [ResourceExposure(ResourceScope.None)]
603 [DllImport(JitHelpers.QCall)]
604 [SuppressUnmanagedCodeSecurity]
605 private static extern bool nIsTransientInternal(RuntimeModule module);
607 [System.Security.SecurityCritical] // auto-generated
608 [ResourceExposure(ResourceScope.None)]
609 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
610 [SuppressUnmanagedCodeSecurity]
611 private extern static void GetScopeName(RuntimeModule module, StringHandleOnStack retString);
613 [System.Security.SecurityCritical] // auto-generated
614 [ResourceExposure(ResourceScope.Machine)]
615 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
616 [SuppressUnmanagedCodeSecurity]
617 private extern static void GetFullyQualifiedName(RuntimeModule module, StringHandleOnStack retString);
619 [System.Security.SecurityCritical] // auto-generated
620 [ResourceExposure(ResourceScope.None)]
621 [MethodImplAttribute(MethodImplOptions.InternalCall)]
622 private extern static RuntimeType[] GetTypes(RuntimeModule module);
624 [System.Security.SecuritySafeCritical] // auto-generated
625 internal RuntimeType[] GetDefinedTypes()
627 return GetTypes(GetNativeHandle());
630 [System.Security.SecuritySafeCritical] // auto-generated
631 [ResourceExposure(ResourceScope.None)]
632 [MethodImplAttribute(MethodImplOptions.InternalCall)]
633 private extern static bool IsResource(RuntimeModule module);
635 #if FEATURE_X509 && FEATURE_CAS_POLICY
636 [System.Security.SecurityCritical] // auto-generated
637 [ResourceExposure(ResourceScope.None)]
638 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
639 [SuppressUnmanagedCodeSecurity]
640 static private extern void GetSignerCertificate(RuntimeModule module, ObjectHandleOnStack retData);
641 #endif // FEATURE_X509 && FEATURE_CAS_POLICY
644 #region Module overrides
645 private static RuntimeTypeHandle[] ConvertToTypeHandleArray(Type[] genericArguments)
647 if (genericArguments == null)
650 int size = genericArguments.Length;
651 RuntimeTypeHandle[] typeHandleArgs = new RuntimeTypeHandle[size];
652 for (int i = 0; i < size; i++)
654 Type typeArg = genericArguments[i];
656 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidGenericInstArray"));
657 typeArg = typeArg.UnderlyingSystemType;
659 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidGenericInstArray"));
660 if (!(typeArg is RuntimeType))
661 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidGenericInstArray"));
662 typeHandleArgs[i] = typeArg.GetTypeHandleInternal();
664 return typeHandleArgs;
667 [System.Security.SecuritySafeCritical] // auto-generated
668 public override byte[] ResolveSignature(int metadataToken)
670 MetadataToken tk = new MetadataToken(metadataToken);
672 if (!MetadataImport.IsValidToken(tk))
673 throw new ArgumentOutOfRangeException("metadataToken",
674 Environment.GetResourceString("Argument_InvalidToken", tk, this));
676 if (!tk.IsMemberRef && !tk.IsMethodDef && !tk.IsTypeSpec && !tk.IsSignature && !tk.IsFieldDef)
677 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidToken", tk, this),
680 ConstArray signature;
682 signature = MetadataImport.GetMemberRefProps(metadataToken);
684 signature = MetadataImport.GetSignatureFromToken(metadataToken);
686 byte[] sig = new byte[signature.Length];
688 for (int i = 0; i < signature.Length; i++)
689 sig[i] = signature[i];
694 [System.Security.SecuritySafeCritical] // auto-generated
695 public override MethodBase ResolveMethod(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
697 MetadataToken tk = new MetadataToken(metadataToken);
699 if (!MetadataImport.IsValidToken(tk))
700 throw new ArgumentOutOfRangeException("metadataToken",
701 Environment.GetResourceString("Argument_InvalidToken", tk, this));
703 RuntimeTypeHandle[] typeArgs = ConvertToTypeHandleArray(genericTypeArguments);
704 RuntimeTypeHandle[] methodArgs = ConvertToTypeHandleArray(genericMethodArguments);
708 if (!tk.IsMethodDef && !tk.IsMethodSpec)
711 throw new ArgumentException("metadataToken",
712 Environment.GetResourceString("Argument_ResolveMethod", tk, this));
716 ConstArray sig = MetadataImport.GetMemberRefProps(tk);
718 if (*(MdSigCallingConvention*)sig.Signature.ToPointer() == MdSigCallingConvention.Field)
719 throw new ArgumentException("metadataToken",
720 Environment.GetResourceString("Argument_ResolveMethod", tk, this));
724 IRuntimeMethodInfo methodHandle = ModuleHandle.ResolveMethodHandleInternal(GetNativeHandle(), tk, typeArgs, methodArgs);
725 Type declaringType = RuntimeMethodHandle.GetDeclaringType(methodHandle);
727 if (declaringType.IsGenericType || declaringType.IsArray)
729 MetadataToken tkDeclaringType = new MetadataToken(MetadataImport.GetParentToken(tk));
732 tkDeclaringType = new MetadataToken(MetadataImport.GetParentToken(tkDeclaringType));
734 declaringType = ResolveType(tkDeclaringType, genericTypeArguments, genericMethodArguments);
737 return System.RuntimeType.GetMethodBase(declaringType as RuntimeType, methodHandle);
739 catch (BadImageFormatException e)
741 throw new ArgumentException(Environment.GetResourceString("Argument_BadImageFormatExceptionResolve"), e);
745 [System.Security.SecurityCritical] // auto-generated
746 private FieldInfo ResolveLiteralField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
748 MetadataToken tk = new MetadataToken(metadataToken);
750 if (!MetadataImport.IsValidToken(tk) || !tk.IsFieldDef)
751 throw new ArgumentOutOfRangeException("metadataToken",
752 String.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Argument_InvalidToken", tk, this)));
757 fieldName = MetadataImport.GetName(tk).ToString();
758 tkDeclaringType = MetadataImport.GetParentToken(tk);
760 Type declaringType = ResolveType(tkDeclaringType, genericTypeArguments, genericMethodArguments);
762 declaringType.GetFields();
766 return declaringType.GetField(fieldName,
767 BindingFlags.Static | BindingFlags.Instance |
768 BindingFlags.Public | BindingFlags.NonPublic |
769 BindingFlags.DeclaredOnly);
773 throw new ArgumentException(Environment.GetResourceString("Argument_ResolveField", tk, this), "metadataToken");
777 [System.Security.SecuritySafeCritical] // auto-generated
778 public override FieldInfo ResolveField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
780 MetadataToken tk = new MetadataToken(metadataToken);
782 if (!MetadataImport.IsValidToken(tk))
783 throw new ArgumentOutOfRangeException("metadataToken",
784 Environment.GetResourceString("Argument_InvalidToken", tk, this));
786 RuntimeTypeHandle[] typeArgs = ConvertToTypeHandleArray(genericTypeArguments);
787 RuntimeTypeHandle[] methodArgs = ConvertToTypeHandleArray(genericMethodArguments);
791 IRuntimeFieldInfo fieldHandle = null;
796 throw new ArgumentException("metadataToken",
797 Environment.GetResourceString("Argument_ResolveField", tk, this));
801 ConstArray sig = MetadataImport.GetMemberRefProps(tk);
803 if (*(MdSigCallingConvention*)sig.Signature.ToPointer() != MdSigCallingConvention.Field)
804 throw new ArgumentException("metadataToken",
805 Environment.GetResourceString("Argument_ResolveField", tk, this));
808 fieldHandle = ModuleHandle.ResolveFieldHandleInternal(GetNativeHandle(), tk, typeArgs, methodArgs);
811 fieldHandle = ModuleHandle.ResolveFieldHandleInternal(GetNativeHandle(), metadataToken, typeArgs, methodArgs);
812 RuntimeType declaringType = RuntimeFieldHandle.GetApproxDeclaringType(fieldHandle.Value);
814 if (declaringType.IsGenericType || declaringType.IsArray)
816 int tkDeclaringType = ModuleHandle.GetMetadataImport(GetNativeHandle()).GetParentToken(metadataToken);
817 declaringType = (RuntimeType)ResolveType(tkDeclaringType, genericTypeArguments, genericMethodArguments);
820 return System.RuntimeType.GetFieldInfo(declaringType, fieldHandle);
822 catch(MissingFieldException)
824 return ResolveLiteralField(tk, genericTypeArguments, genericMethodArguments);
826 catch (BadImageFormatException e)
828 throw new ArgumentException(Environment.GetResourceString("Argument_BadImageFormatExceptionResolve"), e);
832 [System.Security.SecuritySafeCritical] // auto-generated
833 public override Type ResolveType(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
835 MetadataToken tk = new MetadataToken(metadataToken);
837 if (tk.IsGlobalTypeDefToken)
838 throw new ArgumentException(Environment.GetResourceString("Argument_ResolveModuleType", tk), "metadataToken");
840 if (!MetadataImport.IsValidToken(tk))
841 throw new ArgumentOutOfRangeException("metadataToken",
842 Environment.GetResourceString("Argument_InvalidToken", tk, this));
844 if (!tk.IsTypeDef && !tk.IsTypeSpec && !tk.IsTypeRef)
845 throw new ArgumentException(Environment.GetResourceString("Argument_ResolveType", tk, this), "metadataToken");
847 RuntimeTypeHandle[] typeArgs = ConvertToTypeHandleArray(genericTypeArguments);
848 RuntimeTypeHandle[] methodArgs = ConvertToTypeHandleArray(genericMethodArguments);
852 Type t = GetModuleHandle().ResolveTypeHandle(metadataToken, typeArgs, methodArgs).GetRuntimeType();
855 throw new ArgumentException(Environment.GetResourceString("Argument_ResolveType", tk, this), "metadataToken");
859 catch (BadImageFormatException e)
861 throw new ArgumentException(Environment.GetResourceString("Argument_BadImageFormatExceptionResolve"), e);
865 [System.Security.SecuritySafeCritical] // auto-generated
866 public override MemberInfo ResolveMember(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
868 MetadataToken tk = new MetadataToken(metadataToken);
871 throw new ArgumentException(Environment.GetResourceString("InvalidOperation_PropertyInfoNotAvailable"));
874 throw new ArgumentException(Environment.GetResourceString("InvalidOperation_EventInfoNotAvailable"));
876 if (tk.IsMethodSpec || tk.IsMethodDef)
877 return ResolveMethod(metadataToken, genericTypeArguments, genericMethodArguments);
880 return ResolveField(metadataToken, genericTypeArguments, genericMethodArguments);
882 if (tk.IsTypeRef || tk.IsTypeDef || tk.IsTypeSpec)
883 return ResolveType(metadataToken, genericTypeArguments, genericMethodArguments);
887 if (!MetadataImport.IsValidToken(tk))
888 throw new ArgumentOutOfRangeException("metadataToken",
889 Environment.GetResourceString("Argument_InvalidToken", tk, this));
891 ConstArray sig = MetadataImport.GetMemberRefProps(tk);
895 if (*(MdSigCallingConvention*)sig.Signature.ToPointer() == MdSigCallingConvention.Field)
897 return ResolveField(tk, genericTypeArguments, genericMethodArguments);
901 return ResolveMethod(tk, genericTypeArguments, genericMethodArguments);
906 throw new ArgumentException("metadataToken",
907 Environment.GetResourceString("Argument_ResolveMember", tk, this));
910 [System.Security.SecuritySafeCritical] // auto-generated
911 public override string ResolveString(int metadataToken)
913 MetadataToken tk = new MetadataToken(metadataToken);
915 throw new ArgumentException(
916 String.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Argument_ResolveString"), metadataToken, ToString()));
918 if (!MetadataImport.IsValidToken(tk))
919 throw new ArgumentOutOfRangeException("metadataToken",
920 String.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Argument_InvalidToken", tk, this)));
922 string str = MetadataImport.GetUserString(metadataToken);
925 throw new ArgumentException(
926 String.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Argument_ResolveString"), metadataToken, ToString()));
931 public override void GetPEKind(out PortableExecutableKinds peKind, out ImageFileMachine machine)
933 ModuleHandle.GetPEKind(GetNativeHandle(), out peKind, out machine);
936 public override int MDStreamVersion
938 [System.Security.SecuritySafeCritical] // auto-generated
941 return ModuleHandle.GetMDStreamVersion(GetNativeHandle());
947 #pragma warning disable 169
948 // If you add any data members, you need to update the native declaration ReflectModuleBaseObject.
949 private RuntimeType m_runtimeType;
950 private RuntimeAssembly m_runtimeAssembly;
951 private IntPtr m_pRefClass;
953 [System.Runtime.ForceTokenStabilization]
954 #endif //!FEATURE_CORECLR
955 private IntPtr m_pData;
956 private IntPtr m_pGlobals;
957 private IntPtr m_pFields;
958 #pragma warning restore 169
961 #region Protected Virtuals
962 protected override MethodInfo GetMethodImpl(String name, BindingFlags bindingAttr, Binder binder,
963 CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
965 return GetMethodInternal(name, bindingAttr, binder, callConvention, types, modifiers);
968 internal MethodInfo GetMethodInternal(String name, BindingFlags bindingAttr, Binder binder,
969 CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
971 if (RuntimeType == null)
976 return RuntimeType.GetMethod(name, bindingAttr);
980 return RuntimeType.GetMethod(name, bindingAttr, binder, callConvention, types, modifiers);
985 #region Internal Members
986 internal RuntimeType RuntimeType
990 if (m_runtimeType == null)
991 m_runtimeType = ModuleHandle.GetModuleType(GetNativeHandle());
993 return m_runtimeType;
997 [System.Security.SecuritySafeCritical]
998 internal bool IsTransientInternal()
1000 return RuntimeModule.nIsTransientInternal(this.GetNativeHandle());
1003 internal MetadataImport MetadataImport
1005 [System.Security.SecurityCritical] // auto-generated
1010 return ModuleHandle.GetMetadataImport(GetNativeHandle());
1016 #region ICustomAttributeProvider Members
1017 public override Object[] GetCustomAttributes(bool inherit)
1019 return CustomAttribute.GetCustomAttributes(this, typeof(object) as RuntimeType);
1022 public override Object[] GetCustomAttributes(Type attributeType, bool inherit)
1024 if (attributeType == null)
1025 throw new ArgumentNullException("attributeType");
1026 Contract.EndContractBlock();
1028 RuntimeType attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;
1030 if (attributeRuntimeType == null)
1031 throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"),"attributeType");
1033 return CustomAttribute.GetCustomAttributes(this, attributeRuntimeType);
1036 [System.Security.SecuritySafeCritical] // auto-generated
1037 public override bool IsDefined(Type attributeType, bool inherit)
1039 if (attributeType == null)
1040 throw new ArgumentNullException("attributeType");
1041 Contract.EndContractBlock();
1043 RuntimeType attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;
1045 if (attributeRuntimeType == null)
1046 throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"),"attributeType");
1048 return CustomAttribute.IsDefined(this, attributeRuntimeType);
1051 public override IList<CustomAttributeData> GetCustomAttributesData()
1053 return CustomAttributeData.GetCustomAttributesInternal(this);
1057 #region Public Virtuals
1058 [System.Security.SecurityCritical] // auto-generated_required
1059 public override void GetObjectData(SerializationInfo info, StreamingContext context)
1063 throw new ArgumentNullException("info");
1065 Contract.EndContractBlock();
1066 UnitySerializationHolder.GetUnitySerializationInfo(info, UnitySerializationHolder.ModuleUnity, this.ScopeName, this.GetRuntimeAssembly());
1069 [System.Security.SecuritySafeCritical] // auto-generated
1070 [System.Runtime.InteropServices.ComVisible(true)]
1071 public override Type GetType(String className, bool throwOnError, bool ignoreCase)
1073 // throw on null strings regardless of the value of "throwOnError"
1074 if (className == null)
1075 throw new ArgumentNullException("className");
1077 RuntimeType retType = null;
1078 GetType(GetNativeHandle(), className, throwOnError, ignoreCase, JitHelpers.GetObjectHandleOnStack(ref retType));
1082 [System.Security.SecurityCritical] // auto-generated
1083 internal string GetFullyQualifiedName()
1085 String fullyQualifiedName = null;
1086 GetFullyQualifiedName(GetNativeHandle(), JitHelpers.GetStringHandleOnStack(ref fullyQualifiedName));
1087 return fullyQualifiedName;
1090 public override String FullyQualifiedName
1093 [System.Security.SecurityCritical] // auto-generated
1095 [System.Security.SecuritySafeCritical]
1097 [ResourceExposure(ResourceScope.Machine)]
1098 [ResourceConsumption(ResourceScope.Machine)]
1101 String fullyQualifiedName = GetFullyQualifiedName();
1103 if (fullyQualifiedName != null) {
1104 bool checkPermission = true;
1106 Path.GetFullPathInternal(fullyQualifiedName);
1108 catch(ArgumentException) {
1109 checkPermission = false;
1111 if (checkPermission) {
1112 new FileIOPermission( FileIOPermissionAccess.PathDiscovery, fullyQualifiedName ).Demand();
1116 return fullyQualifiedName;
1120 [System.Security.SecuritySafeCritical] // auto-generated
1121 public override Type[] GetTypes()
1123 return GetTypes(GetNativeHandle());
1128 #region Public Members
1130 public override Guid ModuleVersionId
1132 [System.Security.SecuritySafeCritical] // auto-generated
1138 MetadataImport.GetScopeProps(out mvid);
1144 public override int MetadataToken
1146 [System.Security.SecuritySafeCritical] // auto-generated
1149 return ModuleHandle.GetToken(GetNativeHandle());
1153 public override bool IsResource()
1155 return IsResource(GetNativeHandle());
1158 public override FieldInfo[] GetFields(BindingFlags bindingFlags)
1160 if (RuntimeType == null)
1161 return new FieldInfo[0];
1163 return RuntimeType.GetFields(bindingFlags);
1166 public override FieldInfo GetField(String name, BindingFlags bindingAttr)
1169 throw new ArgumentNullException("name");
1171 if (RuntimeType == null)
1174 return RuntimeType.GetField(name, bindingAttr);
1177 public override MethodInfo[] GetMethods(BindingFlags bindingFlags)
1179 if (RuntimeType == null)
1180 return new MethodInfo[0];
1182 return RuntimeType.GetMethods(bindingFlags);
1185 public override String ScopeName
1187 [System.Security.SecuritySafeCritical] // auto-generated
1190 string scopeName = null;
1191 GetScopeName(GetNativeHandle(), JitHelpers.GetStringHandleOnStack(ref scopeName));
1196 public override String Name
1198 [ResourceExposure(ResourceScope.None)]
1199 [ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
1200 [System.Security.SecuritySafeCritical] // auto-generated
1203 String s = GetFullyQualifiedName();
1206 int i = s.LastIndexOf('\\');
1208 int i = s.LastIndexOf(System.IO.Path.DirectorySeparatorChar);
1213 return new String(s.ToCharArray(), i + 1, s.Length - i - 1);
1217 public override Assembly Assembly
1222 return GetRuntimeAssembly();
1226 internal RuntimeAssembly GetRuntimeAssembly()
1228 return m_runtimeAssembly;
1232 internal override ModuleHandle GetModuleHandle()
1234 return new ModuleHandle(this);
1237 internal RuntimeModule GetNativeHandle()
1242 #if FEATURE_X509 && FEATURE_CAS_POLICY
1243 [System.Security.SecuritySafeCritical] // auto-generated
1244 public override System.Security.Cryptography.X509Certificates.X509Certificate GetSignerCertificate()
1247 GetSignerCertificate(GetNativeHandle(), JitHelpers.GetObjectHandleOnStack(ref data));
1248 return (data != null) ? new System.Security.Cryptography.X509Certificates.X509Certificate(data) : null;
1250 #endif // FEATURE_X509 && FEATURE_CAS_POLICY