// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. //////////////////////////////////////////////////////////////////////////////// // namespace System.Reflection { using System; using System.Diagnostics.SymbolStore; using System.Runtime.Remoting; using System.Runtime.InteropServices; using System.Runtime.Serialization; using System.Collections; using System.Collections.Generic; using System.Threading; using System.Runtime.CompilerServices; using System.Security; using System.Security.Permissions; using System.IO; using System.Globalization; using System.Runtime.Versioning; using System.Diagnostics.Contracts; [Serializable] [Flags] [System.Runtime.InteropServices.ComVisible(true)] public enum PortableExecutableKinds { NotAPortableExecutableImage = 0x0, ILOnly = 0x1, Required32Bit = 0x2, PE32Plus = 0x4, Unmanaged32Bit = 0x8, [ComVisible(false)] Preferred32Bit = 0x10, } [Serializable] [System.Runtime.InteropServices.ComVisible(true)] public enum ImageFileMachine { I386 = 0x014c, IA64 = 0x0200, AMD64 = 0x8664, ARM = 0x01c4, } [Serializable] [ClassInterface(ClassInterfaceType.None)] [ComDefaultInterface(typeof(_Module))] [System.Runtime.InteropServices.ComVisible(true)] public abstract class Module : _Module, ISerializable, ICustomAttributeProvider { #region Static Constructor static Module() { __Filters _fltObj; _fltObj = new __Filters(); FilterTypeName = new TypeFilter(_fltObj.FilterTypeName); FilterTypeNameIgnoreCase = new TypeFilter(_fltObj.FilterTypeNameIgnoreCase); } #endregion #region Constructor protected Module() { } #endregion #region Public Statics public static readonly TypeFilter FilterTypeName; public static readonly TypeFilter FilterTypeNameIgnoreCase; public static bool operator ==(Module left, Module right) { if (ReferenceEquals(left, right)) return true; if ((object)left == null || (object)right == null || left is RuntimeModule || right is RuntimeModule) { return false; } return left.Equals(right); } public static bool operator !=(Module left, Module right) { return !(left == right); } public override bool Equals(object o) { return base.Equals(o); } public override int GetHashCode() { return base.GetHashCode(); } #endregion #region Literals private const BindingFlags DefaultLookup = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public; #endregion #region object overrides public override String ToString() { return ScopeName; } #endregion public virtual IEnumerable CustomAttributes { get { return GetCustomAttributesData(); } } #region ICustomAttributeProvider Members public virtual Object[] GetCustomAttributes(bool inherit) { throw new NotImplementedException(); } public virtual Object[] GetCustomAttributes(Type attributeType, bool inherit) { throw new NotImplementedException(); } public virtual bool IsDefined(Type attributeType, bool inherit) { throw new NotImplementedException(); } public virtual IList GetCustomAttributesData() { throw new NotImplementedException(); } #endregion #region public instances members public MethodBase ResolveMethod(int metadataToken) { return ResolveMethod(metadataToken, null, null); } public virtual MethodBase ResolveMethod(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) { // This API was made virtual in V4. Code compiled against V2 might use // "call" rather than "callvirt" to call it. // This makes sure those code still works. RuntimeModule rtModule = this as RuntimeModule; if (rtModule != null) return rtModule.ResolveMethod(metadataToken, genericTypeArguments, genericMethodArguments); throw new NotImplementedException(); } public FieldInfo ResolveField(int metadataToken) { return ResolveField(metadataToken, null, null); } public virtual FieldInfo ResolveField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) { // This API was made virtual in V4. Code compiled against V2 might use // "call" rather than "callvirt" to call it. // This makes sure those code still works. RuntimeModule rtModule = this as RuntimeModule; if (rtModule != null) return rtModule.ResolveField(metadataToken, genericTypeArguments, genericMethodArguments); throw new NotImplementedException(); } public Type ResolveType(int metadataToken) { return ResolveType(metadataToken, null, null); } public virtual Type ResolveType(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) { // This API was made virtual in V4. Code compiled against V2 might use // "call" rather than "callvirt" to call it. // This makes sure those code still works. RuntimeModule rtModule = this as RuntimeModule; if (rtModule != null) return rtModule.ResolveType(metadataToken, genericTypeArguments, genericMethodArguments); throw new NotImplementedException(); } public MemberInfo ResolveMember(int metadataToken) { return ResolveMember(metadataToken, null, null); } public virtual MemberInfo ResolveMember(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) { // This API was made virtual in V4. Code compiled against V2 might use // "call" rather than "callvirt" to call it. // This makes sure those code still works. RuntimeModule rtModule = this as RuntimeModule; if (rtModule != null) return rtModule.ResolveMember(metadataToken, genericTypeArguments, genericMethodArguments); throw new NotImplementedException(); } public virtual byte[] ResolveSignature(int metadataToken) { // This API was made virtual in V4. Code compiled against V2 might use // "call" rather than "callvirt" to call it. // This makes sure those code still works. RuntimeModule rtModule = this as RuntimeModule; if (rtModule != null) return rtModule.ResolveSignature(metadataToken); throw new NotImplementedException(); } public virtual string ResolveString(int metadataToken) { // This API was made virtual in V4. Code compiled against V2 might use // "call" rather than "callvirt" to call it. // This makes sure those code still works. RuntimeModule rtModule = this as RuntimeModule; if (rtModule != null) return rtModule.ResolveString(metadataToken); throw new NotImplementedException(); } public virtual void GetPEKind(out PortableExecutableKinds peKind, out ImageFileMachine machine) { // This API was made virtual in V4. Code compiled against V2 might use // "call" rather than "callvirt" to call it. // This makes sure those code still works. RuntimeModule rtModule = this as RuntimeModule; if (rtModule != null) rtModule.GetPEKind(out peKind, out machine); throw new NotImplementedException(); } public virtual int MDStreamVersion { get { // This API was made virtual in V4. Code compiled against V2 might use // "call" rather than "callvirt" to call it. // This makes sure those code still works. RuntimeModule rtModule = this as RuntimeModule; if (rtModule != null) return rtModule.MDStreamVersion; throw new NotImplementedException(); } } public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { throw new NotImplementedException(); } [System.Runtime.InteropServices.ComVisible(true)] public virtual Type GetType(String className, bool ignoreCase) { return GetType(className, false, ignoreCase); } [System.Runtime.InteropServices.ComVisible(true)] public virtual Type GetType(String className) { return GetType(className, false, false); } [System.Runtime.InteropServices.ComVisible(true)] public virtual Type GetType(String className, bool throwOnError, bool ignoreCase) { throw new NotImplementedException(); } public virtual String FullyQualifiedName { get { throw new NotImplementedException(); } } public virtual Type[] FindTypes(TypeFilter filter,Object filterCriteria) { Type[] c = GetTypes(); int cnt = 0; for (int i = 0;i GetCustomAttributesData() { return CustomAttributeData.GetCustomAttributesInternal(this); } #endregion #region Public Virtuals public override void GetObjectData(SerializationInfo info, StreamingContext context) { if (info == null) { throw new ArgumentNullException(nameof(info)); } Contract.EndContractBlock(); UnitySerializationHolder.GetUnitySerializationInfo(info, UnitySerializationHolder.ModuleUnity, this.ScopeName, this.GetRuntimeAssembly()); } [System.Runtime.InteropServices.ComVisible(true)] public override Type GetType(String className, bool throwOnError, bool ignoreCase) { // throw on null strings regardless of the value of "throwOnError" if (className == null) throw new ArgumentNullException(nameof(className)); RuntimeType retType = null; Object keepAlive = null; GetType(GetNativeHandle(), className, throwOnError, ignoreCase, JitHelpers.GetObjectHandleOnStack(ref retType), JitHelpers.GetObjectHandleOnStack(ref keepAlive)); GC.KeepAlive(keepAlive); return retType; } internal string GetFullyQualifiedName() { String fullyQualifiedName = null; GetFullyQualifiedName(GetNativeHandle(), JitHelpers.GetStringHandleOnStack(ref fullyQualifiedName)); return fullyQualifiedName; } public override String FullyQualifiedName { get { String fullyQualifiedName = GetFullyQualifiedName(); if (fullyQualifiedName != null) { bool checkPermission = true; try { Path.GetFullPath(fullyQualifiedName); } catch(ArgumentException) { checkPermission = false; } if (checkPermission) { new FileIOPermission( FileIOPermissionAccess.PathDiscovery, fullyQualifiedName ).Demand(); } } return fullyQualifiedName; } } public override Type[] GetTypes() { return GetTypes(GetNativeHandle()); } #endregion #region Public Members public override Guid ModuleVersionId { get { unsafe { Guid mvid; MetadataImport.GetScopeProps(out mvid); return mvid; } } } public override int MetadataToken { get { return ModuleHandle.GetToken(GetNativeHandle()); } } public override bool IsResource() { return IsResource(GetNativeHandle()); } public override FieldInfo[] GetFields(BindingFlags bindingFlags) { if (RuntimeType == null) return new FieldInfo[0]; return RuntimeType.GetFields(bindingFlags); } public override FieldInfo GetField(String name, BindingFlags bindingAttr) { if (name == null) throw new ArgumentNullException(nameof(name)); if (RuntimeType == null) return null; return RuntimeType.GetField(name, bindingAttr); } public override MethodInfo[] GetMethods(BindingFlags bindingFlags) { if (RuntimeType == null) return new MethodInfo[0]; return RuntimeType.GetMethods(bindingFlags); } public override String ScopeName { get { string scopeName = null; GetScopeName(GetNativeHandle(), JitHelpers.GetStringHandleOnStack(ref scopeName)); return scopeName; } } public override String Name { get { String s = GetFullyQualifiedName(); #if !FEATURE_PAL int i = s.LastIndexOf('\\'); #else int i = s.LastIndexOf(System.IO.Path.DirectorySeparatorChar); #endif if (i == -1) return s; return s.Substring(i + 1); } } public override Assembly Assembly { [Pure] get { return GetRuntimeAssembly(); } } internal RuntimeAssembly GetRuntimeAssembly() { return m_runtimeAssembly; } internal override ModuleHandle GetModuleHandle() { return new ModuleHandle(this); } internal RuntimeModule GetNativeHandle() { return this; } #endregion } }