📄 assembly.cs
字号:
/* * Assembly.cs - Implementation of the "System.Reflection.Assembly" class. * * Copyright (C) 2001 Southern Storm Software, Pty Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */namespace System.Reflection{#if CONFIG_RUNTIME_INFRAusing System;using System.IO;using System.Globalization;using System.Security;using System.Security.Policy;using System.Runtime.CompilerServices;using System.Runtime.InteropServices;using System.Runtime.Serialization;using System.Configuration.Assemblies;#if CONFIG_COM_INTEROP[ClassInterface(ClassInterfaceType.AutoDual)]#endifpublic class Assembly : IClrProgramItem#if CONFIG_REFLECTION , ICustomAttributeProvider#endif#if CONFIG_SERIALIZATION , ISerializable#endif#if !ECMA_COMPAT , IEvidenceFactory#endif{ // Built-in handle for the assembly. This must be the first field. internal IntPtr privateData; // Private constructor. Normally called by the runtime engine only. internal Assembly() {} // Implement the IClrProgramItem interface. IntPtr IClrProgramItem.ClrHandle { get { return privateData; } }#if CONFIG_REFLECTION // Create an instance of a specific type within this assembly. public Object CreateInstance(String typeName) { return CreateInstance(typeName, false, BindingFlags.Public | BindingFlags.Instance, null, null, null, null); }#if !ECMA_COMPAT public Object CreateInstance(String typeName, bool ignoreCase) { return CreateInstance(typeName, ignoreCase, BindingFlags.Public | BindingFlags.Instance, null, null, null, null); } public#else // ECMA_COMPAT private#endif // ECMA_COMPAT Object CreateInstance(String typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) { Type type = GetType(typeName, false, ignoreCase); if(type == null) { return null; } return Activator.CreateInstance(type, bindingAttr, binder, args, culture, activationAttributes); }#endif // CONFIG_REFLECTION#if !ECMA_COMPAT // Create a qualified type name. public static String CreateQualifiedName(String assemblyName, String typeName) { return typeName + ", " + assemblyName; }#endif // !ECMA_COMPAT#if !ECMA_COMPAT // Get the custom attributes associated with this assembly. public virtual Object[] GetCustomAttributes(bool inherit) { return ClrHelpers.GetCustomAttributes(this, inherit); } public virtual Object[] GetCustomAttributes(Type type, bool inherit) { return ClrHelpers.GetCustomAttributes(this, type, inherit); } // Determine if custom attributes are associated with this assembly. public virtual bool IsDefined(Type type, bool inherit) { return ClrHelpers.IsDefined(this, type, inherit); }#elif CONFIG_REFLECTION // Get the custom attributes associated with this assembly. Object[] ICustomAttributeProvider.GetCustomAttributes(bool inherit) { return ClrHelpers.GetCustomAttributes(this, inherit); } Object[] ICustomAttributeProvider.GetCustomAttributes (Type type, bool inherit) { return ClrHelpers.GetCustomAttributes(this, type, inherit); } // Determine if custom attributes are associated with this assembly. bool ICustomAttributeProvider.IsDefined(Type type, bool inherit) { return ClrHelpers.IsDefined(this, type, inherit); }#endif // CONFIG_REFLECTION // The following three methods are not strictly speaking // ECMA-compatible, but they are useful in ECMA systems. // Get the assembly that called the method that called this method. [MethodImpl(MethodImplOptions.InternalCall)] extern public static Assembly GetCallingAssembly(); // Get the assembly that called this method. [MethodImpl(MethodImplOptions.InternalCall)] extern public static Assembly GetExecutingAssembly(); // Get the assembly that contained the program entry point. [MethodImpl(MethodImplOptions.InternalCall)] extern public static Assembly GetEntryAssembly();#if !ECMA_COMPAT // Get an array of all exported types in an assembly. [MethodImpl(MethodImplOptions.InternalCall)] extern public virtual Type[] GetExportedTypes(); // Get a file stream for a particular public manifest file. [MethodImpl(MethodImplOptions.InternalCall)] extern public virtual FileStream GetFile(String name); // Get file streams for all public manifest files. [MethodImpl(MethodImplOptions.InternalCall)] extern public virtual FileStream[] GetFiles(bool getResourceModules); public virtual FileStream[] GetFiles() { return GetFiles(false); } // Get information about a particular manifest resource. [MethodImpl(MethodImplOptions.InternalCall)] extern public virtual ManifestResourceInfo GetManifestResourceInfo(String resourceName); // Get the names of all manifest resources in this assembly. [MethodImpl(MethodImplOptions.InternalCall)] extern public virtual String[] GetManifestResourceNames(); // Get a stream for a particular manifest resource. [MethodImpl(MethodImplOptions.InternalCall)] extern public virtual Stream GetManifestResourceStream(String name); // Get a stream for a particular manifest resource, scoped by a type. public virtual Stream GetManifestResourceStream(Type type, String name) { if(name == null) { return null; } else if(type != null) { String nspace = type.Namespace; if(nspace != null && nspace != String.Empty) { return GetManifestResourceStream(nspace + "." + name); } } return GetManifestResourceStream(name); }#else // ECMA_COMPAT // Get a file stream for a particular public manifest file. // Not strictly speaking ECMA-compatible, but needed for I18N. [MethodImpl(MethodImplOptions.InternalCall)] extern public virtual FileStream GetFile(String name); // Get a stream for a particular manifest resource. // Not strictly speaking ECMA-compatible, but needed for I18N. [MethodImpl(MethodImplOptions.InternalCall)] extern public virtual Stream GetManifestResourceStream(String name);#endif // ECMA_COMPAT // Get a particular type from this assembly. public virtual Type GetType(String typeName) { return GetType(typeName, false, false); }#if !ECMA_COMPAT public virtual Type GetType(String typeName, bool throwOnError) { return GetType(typeName, throwOnError, false); } [MethodImpl(MethodImplOptions.InternalCall)] extern public Type GetType(String typeName, bool throwOnError, bool ignoreCase);#else [MethodImpl(MethodImplOptions.InternalCall)] extern internal Type GetType(String typeName, bool throwOnError, bool ignoreCase);#endif // Get an array of all types in an assembly. [MethodImpl(MethodImplOptions.InternalCall)] extern public virtual Type[] GetTypes(); // Error codes for "LoadFromName" and "LoadFromFile". internal const int LoadError_OK = 0; internal const int LoadError_InvalidName = 1; internal const int LoadError_FileNotFound = 2; internal const int LoadError_BadImage = 3; internal const int LoadError_Security = 4; // Internal version of "Load". [MethodImpl(MethodImplOptions.InternalCall)] extern private static Assembly LoadFromName(String name, out int error, Assembly parent); // Internal version of "LoadFrom". [MethodImpl(MethodImplOptions.InternalCall)] extern internal static Assembly LoadFromFile(String name, out int error, Assembly parent); // Internal version of "AppDomain.Load" for a byte array. [MethodImpl(MethodImplOptions.InternalCall)] extern internal static Assembly LoadFromBytes(byte[] bytes, out int error, Assembly parent); // Throw an exception based on a load error. internal static void ThrowLoadError(String name, int error) { if(error == LoadError_InvalidName) { throw new ArgumentException(_("Reflection_AssemblyName")); } else if(error == LoadError_FileNotFound) { throw new FileNotFoundException (String.Format(_("Reflection_AssemblyFile"), name)); } else if(error == LoadError_BadImage) { throw new BadImageFormatException (String.Format(_("Reflection_BadImage"), name)); } else { throw new SecurityException (String.Format(_("Reflection_AssemblySecurity"), name)); } } // Load a particular assembly. public static Assembly Load(String assemblyString) { return Load(assemblyString, GetCallingAssembly()); } internal static Assembly Load(String assemblyString, Assembly caller) { Assembly assembly; int error; if(assemblyString == null) { throw new ArgumentNullException("assemblyString"); } if(assemblyString.Length >= 7 && String.Compare(assemblyString, 0, "file://", 0, 7, true) == 0) { if(assemblyString.Length >= 10 && assemblyString[7] == '/' && assemblyString[9] == ':') { // Specification of the form "file:///X:...". assemblyString = assemblyString.Substring(8); } else { // Some other type of file specification. assemblyString = assemblyString.Substring(7); } assembly = LoadFromFile(assemblyString, out error, caller); } else { AssemblyName name = AssemblyName.Parse(assemblyString); assembly = LoadFromName(name.Name, out error, caller); } if(error == LoadError_OK) { return assembly; } else { ThrowLoadError(assemblyString, error); return null; } } // Load a particular assembly from a file.#if !ECMA_COMPAT public#else internal#endif static Assembly LoadFrom(String assemblyFile) { return LoadFrom(assemblyFile, GetCallingAssembly()); } internal static Assembly LoadFrom(String assemblyFile, Assembly caller) { char [] pathChars = new char[] { Path.DirectorySeparatorChar, Path.VolumeSeparatorChar, Path.AltDirectorySeparatorChar }; if(assemblyFile == null) { throw new ArgumentNullException("assemblyFile"); } int error; Assembly assembly; if(assemblyFile.Length >= 7 && String.Compare(assemblyFile, 0, "file://", 0, 7, true) == 0) { if(assemblyFile.Length >= 10 && assemblyFile[7] == '/' && assemblyFile[9] == ':') { // Specification of the form "file:///X:...". assemblyFile = assemblyFile.Substring(8); } else { // Some other type of file specification. assemblyFile = assemblyFile.Substring(7); } } if(assemblyFile.EndsWith(".dll") || assemblyFile.EndsWith(".DLL") || (assemblyFile.IndexOfAny(pathChars) != -1)) { assembly = LoadFromFile(assemblyFile, out error, caller); } else { AssemblyName name = AssemblyName.Parse(assemblyFile); assembly = LoadFromName(name.Name, out error, caller); } if(error == LoadError_OK) { return assembly; } else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -