The Labs \ Source Viewer \ SSCLI \ System.Reflection \ Assembly

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*=============================================================================
  16. **
  17. ** Class: Assembly
  18. **
  19. **
  20. ** Purpose: For Assembly-related stuff.
  21. **
  22. **
  23. =============================================================================*/
  24. using System;
  25. using System.Collections;
  26. using CultureInfo = System.Globalization.CultureInfo;
  27. using System.Security;
  28. using System.Security.Policy;
  29. using System.Security.Permissions;
  30. using System.IO;
  31. using System.Reflection.Emit;
  32. using System.Reflection.Cache;
  33. using StringBuilder = System.Text.StringBuilder;
  34. using System.Configuration.Assemblies;
  35. using StackCrawlMark = System.Threading.StackCrawlMark;
  36. using System.Runtime.InteropServices;
  37. using BinaryFormatter = System.Runtime.Serialization.Formatters.Binary.BinaryFormatter;
  38. using System.Runtime.CompilerServices;
  39. using SecurityZone = System.Security.SecurityZone;
  40. using IEvidenceFactory = System.Security.IEvidenceFactory;
  41. using System.Runtime.Serialization;
  42. using Microsoft.Win32;
  43. using System.Threading;
  44. using __HResults = System.__HResults;
  45. using System.Runtime.Versioning;
  46. namespace System.Reflection
  47. {
  48.    
  49.     [Serializable()]
  50.     [System.Runtime.InteropServices.ComVisible(true)]
  51.     public delegate Module ModuleResolveEventHandler(object sender, ResolveEventArgs e);
  52.    
  53.    
  54.     [Serializable()]
  55.     [ClassInterface(ClassInterfaceType.None)]
  56.     [ComDefaultInterface(typeof(_Assembly))]
  57.     [System.Runtime.InteropServices.ComVisible(true)]
  58.     public class Assembly : _Assembly, IEvidenceFactory, ICustomAttributeProvider, ISerializable
  59.     {
  60.        
  61.         // READ ME
  62.         // If you modify any of these fields, you must also update the
  63.         // AssemblyBaseObject structure in object.h
  64.         internal AssemblyBuilderData m_assemblyData;
  65.        
  66.         [method: SecurityPermissionAttribute(SecurityAction.LinkDemand, ControlAppDomain = true)]
  67.         public event ModuleResolveEventHandler ModuleResolve;
  68.        
  69.         private InternalCache m_cachedData;
  70.         private IntPtr m_assembly;
  71.         // slack for ptr datum on unmanaged side
  72.         private const string s_localFilePrefix = "file:";
  73.        
  74.         public virtual string CodeBase {
  75.             get {
  76.                 string codeBase = nGetCodeBase(false);
  77.                 VerifyCodeBaseDiscovery(codeBase);
  78.                 return codeBase;
  79.             }
  80.         }
  81.        
  82.         public virtual string EscapedCodeBase {
  83.             get { return AssemblyName.EscapeCodeBase(CodeBase); }
  84.         }
  85.        
  86.         public virtual AssemblyName GetName()
  87.         {
  88.             return GetName(false);
  89.         }
  90.        
  91.         unsafe internal AssemblyHandle AssemblyHandle {
  92.             get { return new AssemblyHandle((void*)m_assembly); }
  93.         }
  94.        
  95.         // If the assembly is copied before it is loaded, the codebase will be set to the
  96.         // actual file loaded if fCopiedName is true. If it is false, then the original code base
  97.         // is returned.
  98.         public virtual AssemblyName GetName(bool copiedName)
  99.         {
  100.             AssemblyName an = new AssemblyName();
  101.            
  102.             string codeBase = nGetCodeBase(copiedName);
  103.             VerifyCodeBaseDiscovery(codeBase);
  104.            
  105.             an.Init(nGetSimpleName(), nGetPublicKey(), null, GetVersion(), GetLocale(), nGetHashAlgorithm(), AssemblyVersionCompatibility.SameMachine, codeBase, nGetFlags() | AssemblyNameFlags.PublicKey, null
  106.                 // public key token
  107.             );
  108.             // strong name key pair
  109.             an.ProcessorArchitecture = ComputeProcArchIndex();
  110.             return an;
  111.         }
  112.        
  113.         public virtual string FullName {
  114.             get {
  115.                 // If called by Object.ToString(), return val may be NULL.
  116.                 string s;
  117.                 if ((s = (string)Cache[CacheObjType.AssemblyName]) != null)
  118.                     return s;
  119.                
  120.                 s = GetFullName();
  121.                 if (s != null)
  122.                     Cache[CacheObjType.AssemblyName] = s;
  123.                
  124.                 return s;
  125.             }
  126.         }
  127.        
  128.        
  129.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  130.         public static extern string CreateQualifiedName(string assemblyName, string typeName);
  131.        
  132.         public virtual MethodInfo EntryPoint {
  133.             get {
  134.                 RuntimeMethodHandle methodHandle = nGetEntryPoint();
  135.                
  136.                 if (!methodHandle.IsNullHandle())
  137.                     return (MethodInfo)RuntimeType.GetMethodBase(methodHandle);
  138.                
  139.                 return null;
  140.             }
  141.         }
  142.        
  143.         public static Assembly GetAssembly(Type type)
  144.         {
  145.             if (type == null)
  146.                 throw new ArgumentNullException("type");
  147.            
  148.             Module m = type.Module;
  149.             if (m == null)
  150.                 return null;
  151.             else
  152.                 return m.Assembly;
  153.         }
  154.        
  155.         Type _Assembly.GetType()
  156.         {
  157.             return base.GetType();
  158.         }
  159.        
  160.         public virtual Type GetType(string name)
  161.         {
  162.             return GetType(name, false, false);
  163.         }
  164.        
  165.         public virtual Type GetType(string name, bool throwOnError)
  166.         {
  167.             return GetType(name, throwOnError, false);
  168.         }
  169.        
  170.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  171.         public extern Type GetType(string name, bool throwOnError, bool ignoreCase);
  172.        
  173.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  174.         public virtual extern Type[] GetExportedTypes();
  175.        
  176.         [ResourceExposure(ResourceScope.None)]
  177.         [ResourceConsumption(ResourceScope.Machine | ResourceScope.Assembly, ResourceScope.Machine | ResourceScope.Assembly)]
  178.         public virtual Type[] GetTypes()
  179.         {
  180.             Module[] m = nGetModules(true, false);
  181.            
  182.             int iNumModules = m.Length;
  183.             int iFinalLength = 0;
  184.             StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  185.             Type[][] ModuleTypes = new Type[iNumModules][];
  186.            
  187.             for (int i = 0; i < iNumModules; i++) {
  188.                 ModuleTypes[i] = m[i].GetTypesInternal(ref stackMark);
  189.                 iFinalLength += ModuleTypes[i].Length;
  190.             }
  191.            
  192.             int iCurrent = 0;
  193.             Type[] ret = new Type[iFinalLength];
  194.             for (int i = 0; i < iNumModules; i++) {
  195.                 int iLength = ModuleTypes[i].Length;
  196.                 Array.Copy(ModuleTypes[i], 0, ret, iCurrent, iLength);
  197.                 iCurrent += iLength;
  198.             }
  199.            
  200.             return ret;
  201.         }
  202.        
  203.         // Load a resource based on the NameSpace of the type.
  204.         public virtual Stream GetManifestResourceStream(Type type, string name)
  205.         {
  206.             StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  207.             return GetManifestResourceStream(type, name, false, ref stackMark);
  208.         }
  209.        
  210.         public virtual Stream GetManifestResourceStream(string name)
  211.         {
  212.             StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  213.             return GetManifestResourceStream(name, ref stackMark, false);
  214.         }
  215.        
  216.         public Assembly GetSatelliteAssembly(CultureInfo culture)
  217.         {
  218.             return InternalGetSatelliteAssembly(culture, null, true);
  219.         }
  220.        
  221.         // Useful for binding to a very specific version of a satellite assembly
  222.         public Assembly GetSatelliteAssembly(CultureInfo culture, Version version)
  223.         {
  224.             return InternalGetSatelliteAssembly(culture, version, true);
  225.         }
  226.        
  227.         public virtual Evidence Evidence {
  228.             [SecurityPermissionAttribute(SecurityAction.Demand, ControlEvidence = true)]
  229.             get { return nGetEvidence().Copy(); }
  230.         }
  231.        
  232.        
  233.         // ISerializable implementation
  234.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
  235.         public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
  236.         {
  237.             if (info == null)
  238.                 throw new ArgumentNullException("info");
  239.            
  240.            
  241.             UnitySerializationHolder.GetUnitySerializationInfo(info, UnitySerializationHolder.AssemblyUnity, this.FullName, this);
  242.         }
  243.        
  244.         internal bool AptcaCheck(Assembly sourceAssembly)
  245.         {
  246.             return AssemblyHandle.AptcaCheck(sourceAssembly.AssemblyHandle);
  247.         }
  248.        
  249.         [ComVisible(false)]
  250.         public Module ManifestModule {
  251.             get {
  252.                 ModuleHandle manifestModuleHandle = AssemblyHandle.GetManifestModule();
  253.                 if (!manifestModuleHandle.IsNullHandle())
  254.                     return manifestModuleHandle.GetModule();
  255.                 return null;
  256.             }
  257.         }
  258.        
  259.         public virtual object[] GetCustomAttributes(bool inherit)
  260.         {
  261.             return CustomAttribute.GetCustomAttributes(this, typeof(object) as RuntimeType);
  262.         }
  263.        
  264.         public virtual object[] GetCustomAttributes(Type attributeType, bool inherit)
  265.         {
  266.             if (attributeType == null)
  267.                 throw new ArgumentNullException("attributeType");
  268.            
  269.             RuntimeType attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;
  270.            
  271.             if (attributeRuntimeType == null)
  272.                 throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "attributeType");
  273.            
  274.             return CustomAttribute.GetCustomAttributes(this, attributeRuntimeType);
  275.         }
  276.        
  277.         public virtual bool IsDefined(Type attributeType, bool inherit)
  278.         {
  279.             if (attributeType == null)
  280.                 throw new ArgumentNullException("attributeType");
  281.            
  282.             RuntimeType attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;
  283.            
  284.             if (attributeRuntimeType == null)
  285.                 throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "caType");
  286.            
  287.             return CustomAttribute.IsDefined(this, attributeRuntimeType);
  288.         }
  289.        
  290.        
  291.         // Locate an assembly by the name of the file containing the manifest.
  292.         public static Assembly LoadFrom(string assemblyFile)
  293.         {
  294.             // The stack mark is used for MDA filtering
  295.             StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  296.                 // securityEvidence
  297.                 // hashValue
  298.                 // forIntrospection
  299.             return InternalLoadFrom(assemblyFile, null, null, AssemblyHashAlgorithm.None, false, ref stackMark);
  300.         }
  301.        
  302.         // Locate an assembly for reflection by the name of the file containing the manifest.
  303.         public static Assembly ReflectionOnlyLoadFrom(string assemblyFile)
  304.         {
  305.             // The stack mark is ingored for ReflectionOnlyLoadFrom
  306.             StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  307.                 //securityEvidence
  308.                 //hashValue
  309.                 //forIntrospection
  310.             return InternalLoadFrom(assemblyFile, null, null, AssemblyHashAlgorithm.None, true, ref stackMark);
  311.         }
  312.        
  313.         // Evidence is protected in Assembly.Load()
  314.         public static Assembly LoadFrom(string assemblyFile, Evidence securityEvidence)
  315.         {
  316.             // The stack mark is used for MDA filtering
  317.             StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  318.                 // hashValue
  319.                 // forIntrospection
  320.             return InternalLoadFrom(assemblyFile, securityEvidence, null, AssemblyHashAlgorithm.None, false, ref stackMark);
  321.         }
  322.        
  323.         // Evidence is protected in Assembly.Load()
  324.         public static Assembly LoadFrom(string assemblyFile, Evidence securityEvidence, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
  325.         {
  326.             // The stack mark is used for MDA filtering
  327.             StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  328.             return InternalLoadFrom(assemblyFile, securityEvidence, hashValue, hashAlgorithm, false, ref stackMark);
  329.         }
  330.        
  331.        
  332.         private static Assembly InternalLoadFrom(string assemblyFile, Evidence securityEvidence, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, bool forIntrospection, ref StackCrawlMark stackMark)
  333.         {
  334.             if (assemblyFile == null)
  335.                 throw new ArgumentNullException("assemblyFile");
  336.            
  337.             AssemblyName an = new AssemblyName();
  338.             an.CodeBase = assemblyFile;
  339.             an.SetHashControl(hashValue, hashAlgorithm);
  340.            
  341.             return InternalLoad(an, securityEvidence, ref stackMark, forIntrospection);
  342.         }
  343.        
  344.        
  345.         // Locate an assembly by the long form of the assembly name.
  346.         // eg. "Toolbox.dll, version=1.1.10.1220, locale=en, publickey=1234567890123456789012345678901234567890"
  347.         public static Assembly Load(string assemblyString)
  348.         {
  349.             StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  350.             return InternalLoad(assemblyString, null, ref stackMark, false);
  351.         }
  352.        
  353.         // Locate an assembly for reflection by the long form of the assembly name.
  354.         // eg. "Toolbox.dll, version=1.1.10.1220, locale=en, publickey=1234567890123456789012345678901234567890"
  355.         //
  356.         public static Assembly ReflectionOnlyLoad(string assemblyString)
  357.         {
  358.             StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  359.                 /*forIntrospection*/            return InternalLoad(assemblyString, null, ref stackMark, true);
  360.         }
  361.        
  362.         public static Assembly Load(string assemblyString, Evidence assemblySecurity)
  363.         {
  364.             StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  365.             return InternalLoad(assemblyString, assemblySecurity, ref stackMark, false);
  366.         }
  367.        
  368.         // Locate an assembly by its name. The name can be strong or
  369.         // weak. The assembly is loaded into the domain of the caller.
  370.         public static Assembly Load(AssemblyName assemblyRef)
  371.         {
  372.             StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  373.             return InternalLoad(assemblyRef, null, ref stackMark, false);
  374.         }
  375.        
  376.        
  377.         public static Assembly Load(AssemblyName assemblyRef, Evidence assemblySecurity)
  378.         {
  379.             StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  380.             return InternalLoad(assemblyRef, assemblySecurity, ref stackMark, false);
  381.         }
  382.        
  383.         // used by vm
  384.         unsafe private static IntPtr LoadWithPartialNameHack(string partialName, bool cropPublicKey)
  385.         {
  386.             StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  387.            
  388.             Assembly result = null;
  389.             AssemblyName an = new AssemblyName(partialName);
  390.            
  391.             if (!IsSimplyNamed(an)) {
  392.                 if (cropPublicKey) {
  393.                     an.SetPublicKey(null);
  394.                     an.SetPublicKeyToken(null);
  395.                 }
  396.                 AssemblyName GACAssembly = EnumerateCache(an);
  397.                 if (GACAssembly != null)
  398.                     result = InternalLoad(GACAssembly, null, ref stackMark, false);
  399.             }
  400.            
  401.             if (result == null)
  402.                 return (IntPtr)0;
  403.            
  404.             return (IntPtr)result.AssemblyHandle.Value;
  405.         }
  406.        
  407.         [Obsolete("This method has been deprecated. Please use Assembly.Load() instead. http://go.microsoft.com/fwlink/?linkid=14202")]
  408.         public static Assembly LoadWithPartialName(string partialName)
  409.         {
  410.             StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  411.             return LoadWithPartialNameInternal(partialName, null, ref stackMark);
  412.         }
  413.        
  414.         [Obsolete("This method has been deprecated. Please use Assembly.Load() instead. http://go.microsoft.com/fwlink/?linkid=14202")]
  415.         public static Assembly LoadWithPartialName(string partialName, Evidence securityEvidence)
  416.         {
  417.             StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  418.             return LoadWithPartialNameInternal(partialName, securityEvidence, ref stackMark);
  419.         }
  420.        
  421.         static internal Assembly LoadWithPartialNameInternal(string partialName, Evidence securityEvidence, ref StackCrawlMark stackMark)
  422.         {
  423.             if (securityEvidence != null)
  424.                 new SecurityPermission(SecurityPermissionFlag.ControlEvidence).Demand();
  425.            
  426.             Assembly result = null;
  427.             AssemblyName an = new AssemblyName(partialName);
  428.             try {
  429.                 result = nLoad(an, null, securityEvidence, null, ref stackMark, true, false);
  430.             }
  431.             catch (Exception e) {
  432.                 if (e.IsTransient)
  433.                     throw e;
  434.                
  435.                 if (IsSimplyNamed(an))
  436.                     return null;
  437.                
  438.                 AssemblyName GACAssembly = EnumerateCache(an);
  439.                 if (GACAssembly != null)
  440.                     return InternalLoad(GACAssembly, securityEvidence, ref stackMark, false);
  441.             }
  442.            
  443.            
  444.             return result;
  445.         }
  446.        
  447.        
  448.         [ComVisible(false)]
  449.         public virtual bool ReflectionOnly {
  450.             get { return nReflection(); }
  451.         }
  452.        
  453.        
  454.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  455.         internal extern bool nReflection();
  456.        
  457.         private static AssemblyName EnumerateCache(AssemblyName partialName)
  458.         {
  459.             new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert();
  460.            
  461.             partialName.Version = null;
  462.            
  463.             ArrayList a = new ArrayList();
  464.             Fusion.ReadCache(a, partialName.FullName, ASM_CACHE.GAC);
  465.            
  466.             IEnumerator myEnum = a.GetEnumerator();
  467.             AssemblyName ainfoBest = null;
  468.             CultureInfo refCI = partialName.CultureInfo;
  469.            
  470.             while (myEnum.MoveNext()) {
  471.                 AssemblyName ainfo = new AssemblyName((string)myEnum.Current);
  472.                
  473.                 if (CulturesEqual(refCI, ainfo.CultureInfo)) {
  474.                     if (ainfoBest == null)
  475.                         ainfoBest = ainfo;
  476.                     else {
  477.                         // Choose highest version
  478.                         if (ainfo.Version > ainfoBest.Version)
  479.                             ainfoBest = ainfo;
  480.                     }
  481.                 }
  482.             }
  483.            
  484.             return ainfoBest;
  485.         }
  486.        
  487.         private static bool CulturesEqual(CultureInfo refCI, CultureInfo defCI)
  488.         {
  489.             bool defNoCulture = defCI.Equals(CultureInfo.InvariantCulture);
  490.            
  491.             // cultured asms aren't allowed to be bound to if
  492.             // the ref doesn't ask for them specifically
  493.             if ((refCI == null) || refCI.Equals(CultureInfo.InvariantCulture))
  494.                 return defNoCulture;
  495.            
  496.             if (defNoCulture || (!defCI.Equals(refCI)))
  497.                 return false;
  498.            
  499.             return true;
  500.         }
  501.        
  502.         private static bool IsSimplyNamed(AssemblyName partialName)
  503.         {
  504.             byte[] pk = partialName.GetPublicKeyToken();
  505.             if ((pk != null) && (pk.Length == 0))
  506.                 return true;
  507.            
  508.             pk = partialName.GetPublicKey();
  509.             if ((pk != null) && (pk.Length == 0))
  510.                 return true;
  511.            
  512.             return false;
  513.         }
  514.        
  515.         // Loads the assembly with a COFF based IMAGE containing
  516.         // an emitted assembly. The assembly is loaded into the domain
  517.         // of the caller.
  518.         public static Assembly Load(byte[] rawAssembly)
  519.         {
  520.             StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  521.                 // symbol store
  522.                 // evidence
  523.                 // fIntrospection
  524.             return nLoadImage(rawAssembly, null, null, ref stackMark, false);
  525.         }
  526.        
  527.         // Loads the assembly for reflection with a COFF based IMAGE containing
  528.         // an emitted assembly. The assembly is loaded into the domain
  529.         // of the caller.
  530.         public static Assembly ReflectionOnlyLoad(byte[] rawAssembly)
  531.         {
  532.             StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  533.                 // symbol store
  534.                 // evidence
  535.                 // fIntrospection
  536.             return nLoadImage(rawAssembly, null, null, ref stackMark, true);
  537.         }
  538.        
  539.         // Loads the assembly with a COFF based IMAGE containing
  540.         // an emitted assembly. The assembly is loaded into the domain
  541.         // of the caller. The second parameter is the raw bytes
  542.         // representing the symbol store that matches the assembly.
  543.         public static Assembly Load(byte[] rawAssembly, byte[] rawSymbolStore)
  544.         {
  545.             StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  546.                 // evidence
  547.                 // fIntrospection
  548.             return nLoadImage(rawAssembly, rawSymbolStore, null, ref stackMark, false);
  549.         }
  550.        
  551.         [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlEvidence)]
  552.         public static Assembly Load(byte[] rawAssembly, byte[] rawSymbolStore, Evidence securityEvidence)
  553.         {
  554.             StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  555.                 // fIntrospection
  556.             return nLoadImage(rawAssembly, rawSymbolStore, securityEvidence, ref stackMark, false);
  557.         }
  558.        
  559.         public static Assembly LoadFile(string path)
  560.         {
  561.             new FileIOPermission(FileIOPermissionAccess.PathDiscovery | FileIOPermissionAccess.Read, path).Demand();
  562.             return nLoadFile(path, null);
  563.         }
  564.        
  565.         [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlEvidence)]
  566.         public static Assembly LoadFile(string path, Evidence securityEvidence)
  567.         {
  568.             new FileIOPermission(FileIOPermissionAccess.PathDiscovery | FileIOPermissionAccess.Read, path).Demand();
  569.             return nLoadFile(path, securityEvidence);
  570.         }
  571.        
  572.         public Module LoadModule(string moduleName, byte[] rawModule)
  573.         {
  574.             return nLoadModule(moduleName, rawModule, null, Evidence);
  575.             // does a ControlEvidence demand
  576.         }
  577.        
  578.         public Module LoadModule(string moduleName, byte[] rawModule, byte[] rawSymbolStore)
  579.         {
  580.             return nLoadModule(moduleName, rawModule, rawSymbolStore, Evidence);
  581.             // does a ControlEvidence demand
  582.         }
  583.        
  584.         //
  585.         // Locates a type from this assembly and creates an instance of it using
  586.         // the system activator.
  587.         //
  588.         public object CreateInstance(string typeName)
  589.         {
  590.                 // ignore case
  591.                 // binder
  592.                 // args
  593.                 // culture
  594.             return CreateInstance(typeName, false, BindingFlags.Public | BindingFlags.Instance, null, null, null, null);
  595.             // activation attributes
  596.         }
  597.        
  598.         public object CreateInstance(string typeName, bool ignoreCase)
  599.         {
  600.                 // binder
  601.                 // args
  602.                 // culture
  603.             return CreateInstance(typeName, ignoreCase, BindingFlags.Public | BindingFlags.Instance, null, null, null, null);
  604.             // activation attributes
  605.         }
  606.        
  607.         public object CreateInstance(string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes)
  608.         {
  609.             Type t = GetType(typeName, false, ignoreCase);
  610.             if (t == null)
  611.                 return null;
  612.             return Activator.CreateInstance(t, bindingAttr, binder, args, culture, activationAttributes);
  613.         }
  614.        
  615.         [ResourceExposure(ResourceScope.Machine | ResourceScope.Assembly)]
  616.         [ResourceConsumption(ResourceScope.Machine | ResourceScope.Assembly)]
  617.         public Module[] GetLoadedModules()
  618.         {
  619.             return nGetModules(false, false);
  620.         }
  621.        
  622.         [ResourceExposure(ResourceScope.Machine | ResourceScope.Assembly)]
  623.         [ResourceConsumption(ResourceScope.Machine | ResourceScope.Assembly)]
  624.         public Module[] GetLoadedModules(bool getResourceModules)
  625.         {
  626.             return nGetModules(false, getResourceModules);
  627.         }
  628.        
  629.         [ResourceExposure(ResourceScope.Machine | ResourceScope.Assembly)]
  630.         [ResourceConsumption(ResourceScope.Machine | ResourceScope.Assembly)]
  631.         public Module[] GetModules()
  632.         {
  633.             return nGetModules(true, false);
  634.         }
  635.        
  636.         [ResourceExposure(ResourceScope.Machine | ResourceScope.Assembly)]
  637.         [ResourceConsumption(ResourceScope.Machine | ResourceScope.Assembly)]
  638.         public Module[] GetModules(bool getResourceModules)
  639.         {
  640.             return nGetModules(true, getResourceModules);
  641.         }
  642.        
  643.         // Returns the module in this assembly with name 'name'
  644.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  645.         [ResourceExposure(ResourceScope.Machine | ResourceScope.Assembly)]
  646.         public extern Module GetModule(string name);
  647.        
  648.         // Returns the file in the File table of the manifest that matches the
  649.         // given name. (Name should not include path.)
  650.         [ResourceExposure(ResourceScope.Machine | ResourceScope.Assembly)]
  651.         [ResourceConsumption(ResourceScope.Machine | ResourceScope.Assembly)]
  652.         public virtual FileStream GetFile(string name)
  653.         {
  654.             Module m = GetModule(name);
  655.             if (m == null)
  656.                 return null;
  657.            
  658.             return new FileStream(m.InternalGetFullyQualifiedName(), FileMode.Open, FileAccess.Read, FileShare.Read);
  659.         }
  660.        
  661.         [ResourceExposure(ResourceScope.Machine | ResourceScope.Assembly)]
  662.         [ResourceConsumption(ResourceScope.Machine | ResourceScope.Assembly)]
  663.         public virtual FileStream[] GetFiles()
  664.         {
  665.             return GetFiles(false);
  666.         }
  667.        
  668.         [ResourceExposure(ResourceScope.Machine | ResourceScope.Assembly)]
  669.         [ResourceConsumption(ResourceScope.Machine | ResourceScope.Assembly)]
  670.         public virtual FileStream[] GetFiles(bool getResourceModules)
  671.         {
  672.             Module[] m = nGetModules(true, getResourceModules);
  673.             int iLength = m.Length;
  674.             FileStream[] fs = new FileStream[iLength];
  675.            
  676.             for (int i = 0; i < iLength; i++)
  677.                 fs[i] = new FileStream(m[i].InternalGetFullyQualifiedName(), FileMode.Open, FileAccess.Read, FileShare.Read);
  678.            
  679.             return fs;
  680.         }
  681.        
  682.         // Returns the names of all the resources
  683.         public virtual string[] GetManifestResourceNames()
  684.         {
  685.             return nGetManifestResourceNames();
  686.         }
  687.        
  688.         // Returns the names of all the resources
  689.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  690.         internal extern string[] nGetManifestResourceNames();
  691.        
  692. /*
  693.         * Get the assembly that the current code is running from.
  694.         */       
  695.         public static Assembly GetExecutingAssembly()
  696.         {
  697.             // passing address of local will also prevent inlining
  698.             StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  699.             return nGetExecutingAssembly(ref stackMark);
  700.         }
  701.        
  702.         public static Assembly GetCallingAssembly()
  703.         {
  704.             StackCrawlMark stackMark = StackCrawlMark.LookForMyCallersCaller;
  705.             return nGetExecutingAssembly(ref stackMark);
  706.         }
  707.        
  708.         public static Assembly GetEntryAssembly()
  709.         {
  710.             AppDomainManager domainManager = AppDomain.CurrentDomain.DomainManager;
  711.             if (domainManager == null)
  712.                 domainManager = new AppDomainManager();
  713.             return domainManager.EntryAssembly;
  714.         }
  715.        
  716.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  717.         public extern AssemblyName[] GetReferencedAssemblies();
  718.        
  719.        
  720.         public virtual ManifestResourceInfo GetManifestResourceInfo(string resourceName)
  721.         {
  722.             Assembly assemblyRef;
  723.             string fileName;
  724.             StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  725.             int location = nGetManifestResourceInfo(resourceName, out assemblyRef, out fileName, ref stackMark);
  726.            
  727.             if (location == -1)
  728.                 return null;
  729.             else
  730.                 return new ManifestResourceInfo(assemblyRef, fileName, (ResourceLocation)location);
  731.         }
  732.        
  733.         public override string ToString()
  734.         {
  735.             string displayName = FullName;
  736.             if (displayName == null)
  737.                 return base.ToString();
  738.             else
  739.                 return displayName;
  740.         }
  741.        
  742.         public virtual string Location {
  743.             get {
  744.                 string location = GetLocation();
  745.                
  746.                 if (location != null)
  747.                     new FileIOPermission(FileIOPermissionAccess.PathDiscovery, location).Demand();
  748.                
  749.                 return location;
  750.             }
  751.         }
  752.        
  753.         [ComVisible(false)]
  754.         public virtual string ImageRuntimeVersion {
  755.             get { return nGetImageRuntimeVersion(); }
  756.         }
  757.        
  758.        
  759. /*
  760.           Returns true if the assembly was loaded from the global assembly cache.
  761.         */       
  762.        
  763.         public bool GlobalAssemblyCache {
  764.             get { return nGlobalAssemblyCache(); }
  765.         }
  766.        
  767.         [ComVisible(false)]
  768.         public Int64 HostContext {
  769.             get { return GetHostContext(); }
  770.         }
  771.        
  772.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  773.         private extern Int64 GetHostContext();
  774.        
  775.         [ResourceExposure(ResourceScope.Machine)]
  776.         [ResourceConsumption(ResourceScope.Machine)]
  777.         static internal string VerifyCodeBase(string codebase)
  778.         {
  779.             if (codebase == null)
  780.                 return null;
  781.            
  782.             int len = codebase.Length;
  783.             if (len == 0)
  784.                 return null;
  785.            
  786.            
  787.             int j = codebase.IndexOf(':');
  788.             // Check to see if the url has a prefix
  789.             if ((j != -1) && (j + 2 < len) && ((codebase[j + 1] == '/') || (codebase[j + 1] == '\\')) && ((codebase[j + 2] == '/') || (codebase[j + 2] == '\\')))
  790.                 return codebase;
  791.             else if ((len > 2) && (codebase[0] == '\\') && (codebase[1] == '\\'))
  792.                 return "file://" + codebase;
  793.             else
  794.                 #if !PLATFORM_UNIX
  795.                 return "file:///" + Path.GetFullPathInternal(codebase);
  796.         }
  797.     }
  798. }
  799. #else

Developer Fusion