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

  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. ** File:    AssemblyName
  18. **
  19. **
  20. ** Purpose: Used for binding and retrieving info about an assembly
  21. **
  22. **
  23. ===========================================================*/
  24. namespace System.Reflection
  25. {
  26.     using System;
  27.     using System.IO;
  28.     using System.Configuration.Assemblies;
  29.     using System.Runtime.CompilerServices;
  30.     using CultureInfo = System.Globalization.CultureInfo;
  31.     using System.Runtime.Serialization;
  32.     using System.Security.Permissions;
  33.     using System.Runtime.InteropServices;
  34.     using System.Runtime.Versioning;
  35.    
  36.     [Serializable()]
  37.     [ClassInterface(ClassInterfaceType.None)]
  38.     [ComDefaultInterface(typeof(_AssemblyName))]
  39.     [System.Runtime.InteropServices.ComVisible(true)]
  40.     public sealed class AssemblyName : _AssemblyName, ICloneable, ISerializable, IDeserializationCallback
  41.     {
  42.         //
  43.         // READ ME
  44.         // If you modify any of these fields, you must also update the
  45.         // AssemblyBaseObject structure in object.h
  46.         //
  47.         private string _Name;
  48.         // Name
  49.         private byte[] _PublicKey;
  50.         private byte[] _PublicKeyToken;
  51.         private CultureInfo _CultureInfo;
  52.         private string _CodeBase;
  53.         // Potential location to get the file
  54.         private Version _Version;
  55.        
  56.         private StrongNameKeyPair _StrongNameKeyPair;
  57.         private SerializationInfo m_siInfo;
  58.         //A temporary variable which we need during deserialization.
  59.         private byte[] _HashForControl;
  60.         private AssemblyHashAlgorithm _HashAlgorithm;
  61.         private AssemblyHashAlgorithm _HashAlgorithmForControl;
  62.        
  63.         private AssemblyVersionCompatibility _VersionCompatibility;
  64.         private AssemblyNameFlags _Flags;
  65.        
  66.         public AssemblyName()
  67.         {
  68.             _HashAlgorithm = AssemblyHashAlgorithm.None;
  69.             _VersionCompatibility = AssemblyVersionCompatibility.SameMachine;
  70.             _Flags = AssemblyNameFlags.None;
  71.         }
  72.        
  73.         // Set and get the name of the assembly. If this is a weak Name
  74.         // then it optionally contains a site. For strong assembly names,
  75.         // the name partitions up the strong name's namespace
  76.         public string Name {
  77.             get { return _Name; }
  78.             set { _Name = value; }
  79.         }
  80.        
  81.         public Version Version {
  82.             get { return _Version; }
  83.             set { _Version = value; }
  84.         }
  85.        
  86.         // Locales, internally the LCID is used for the match.
  87.         public CultureInfo CultureInfo {
  88.             get { return _CultureInfo; }
  89.             set { _CultureInfo = value; }
  90.         }
  91.        
  92.         public string CodeBase {
  93.             [ResourceExposure(ResourceScope.Machine)]
  94.             get { return _CodeBase; }
  95.             set { _CodeBase = value; }
  96.         }
  97.        
  98.         public string EscapedCodeBase {
  99.             get {
  100.                
  101.                 if (_CodeBase == null)
  102.                     return null;
  103.                 else
  104.                     return EscapeCodeBase(_CodeBase);
  105.             }
  106.         }
  107.        
  108.         public ProcessorArchitecture ProcessorArchitecture {
  109.             get {
  110.                 int x = (((int)_Flags) & 112) >> 4;
  111.                 if (x > 4)
  112.                     x = 0;
  113.                 return (ProcessorArchitecture)x;
  114.             }
  115.             set {
  116.                 int x = ((int)value) & 7;
  117.                 if (x <= 4) {
  118.                     _Flags = (AssemblyNameFlags)((int)_Flags & 4294967055u);
  119.                     _Flags |= (AssemblyNameFlags)(x << 4);
  120.                 }
  121.             }
  122.         }
  123.        
  124.        
  125.        
  126.         // Make a copy of this assembly name.
  127.         public object Clone()
  128.         {
  129.             AssemblyName name = new AssemblyName();
  130.             name.Init(_Name, _PublicKey, _PublicKeyToken, _Version, _CultureInfo, _HashAlgorithm, _VersionCompatibility, _CodeBase, _Flags, _StrongNameKeyPair
  131.             );
  132.             name._HashForControl = _HashForControl;
  133.             name._HashAlgorithmForControl = _HashAlgorithmForControl;
  134.             return name;
  135.         }
  136.        
  137. /*
  138.         * Get the AssemblyName for a given file. This will only work
  139.         * if the file contains an assembly manifest. This method causes
  140.         * the file to be opened and closed.
  141.         */       
  142.         [ResourceExposure(ResourceScope.None)]
  143.         [ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
  144.         public static AssemblyName GetAssemblyName(string assemblyFile)
  145.         {
  146.             if (assemblyFile == null)
  147.                 throw new ArgumentNullException("assemblyFile");
  148.            
  149.             // Assembly.GetNameInternal() will not demand path discovery
  150.             // permission, so do that first.
  151.             string fullPath = Path.GetFullPathInternal(assemblyFile);
  152.             new FileIOPermission(FileIOPermissionAccess.PathDiscovery, fullPath).Demand();
  153.             return nGetFileInformation(fullPath);
  154.         }
  155.        
  156.         internal void SetHashControl(byte[] hash, AssemblyHashAlgorithm hashAlgorithm)
  157.         {
  158.             _HashForControl = hash;
  159.             _HashAlgorithmForControl = hashAlgorithm;
  160.         }
  161.        
  162.         // The public key that is used to verify an assemblies
  163.         // inclusion into the namespace. If the public key associated
  164.         // with the namespace cannot verify the assembly the assembly
  165.         // will fail to load.
  166.         public byte[] GetPublicKey()
  167.         {
  168.             return _PublicKey;
  169.         }
  170.        
  171.         public void SetPublicKey(byte[] publicKey)
  172.         {
  173.             _PublicKey = publicKey;
  174.            
  175.             if (publicKey == null)
  176.                 _Flags ^= AssemblyNameFlags.PublicKey;
  177.             else
  178.                 _Flags |= AssemblyNameFlags.PublicKey;
  179.         }
  180.        
  181.         // The compressed version of the public key formed from a truncated hash.
  182.         public byte[] GetPublicKeyToken()
  183.         {
  184.             if (_PublicKeyToken == null)
  185.                 _PublicKeyToken = nGetPublicKeyToken();
  186.             return _PublicKeyToken;
  187.         }
  188.        
  189.         public void SetPublicKeyToken(byte[] publicKeyToken)
  190.         {
  191.             _PublicKeyToken = publicKeyToken;
  192.         }
  193.        
  194.         // Flags modifying the name. So far the only flag is PublicKey, which
  195.         // indicates that a full public key and not the compressed version is
  196.         // present.
  197.         // Processor Architecture flags are set only through ProcessorArchitecture
  198.         // property and can't be set or retrieved directly
  199.         public AssemblyNameFlags Flags {
  200.             get { return (AssemblyNameFlags)((uint)_Flags & 4294967055u); }
  201.             set {
  202.                 _Flags &= unchecked((AssemblyNameFlags)240);
  203.                 _Flags |= (value & unchecked((AssemblyNameFlags)4294967055u));
  204.             }
  205.         }
  206.        
  207.         public AssemblyHashAlgorithm HashAlgorithm {
  208.             get { return _HashAlgorithm; }
  209.             set { _HashAlgorithm = value; }
  210.         }
  211.        
  212.         public AssemblyVersionCompatibility VersionCompatibility {
  213.             get { return _VersionCompatibility; }
  214.             set { _VersionCompatibility = value; }
  215.         }
  216.        
  217.         public StrongNameKeyPair KeyPair {
  218.             get { return _StrongNameKeyPair; }
  219.             set { _StrongNameKeyPair = value; }
  220.         }
  221.        
  222.         public string FullName {
  223.             get { return nToString(); }
  224.         }
  225.        
  226.         // Returns the stringized version of the assembly name.
  227.         public override string ToString()
  228.         {
  229.             string s = FullName;
  230.             if (s == null)
  231.                 return base.ToString();
  232.             else
  233.                 return s;
  234.         }
  235.        
  236.         public void GetObjectData(SerializationInfo info, StreamingContext context)
  237.         {
  238.             if (info == null)
  239.                 throw new ArgumentNullException("info");
  240.            
  241.             //Allocate the serialization info and serialize our static data.
  242.             info.AddValue("_Name", _Name);
  243.             info.AddValue("_PublicKey", _PublicKey, typeof(byte[]));
  244.             info.AddValue("_PublicKeyToken", _PublicKeyToken, typeof(byte[]));
  245.             info.AddValue("_CultureInfo", (_CultureInfo == null) ? -1 : _CultureInfo.LCID);
  246.             info.AddValue("_CodeBase", _CodeBase);
  247.             info.AddValue("_Version", _Version);
  248.             info.AddValue("_HashAlgorithm", _HashAlgorithm, typeof(AssemblyHashAlgorithm));
  249.             info.AddValue("_HashAlgorithmForControl", _HashAlgorithmForControl, typeof(AssemblyHashAlgorithm));
  250.             info.AddValue("_StrongNameKeyPair", _StrongNameKeyPair, typeof(StrongNameKeyPair));
  251.             info.AddValue("_VersionCompatibility", _VersionCompatibility, typeof(AssemblyVersionCompatibility));
  252.             info.AddValue("_Flags", _Flags, typeof(AssemblyNameFlags));
  253.             info.AddValue("_HashForControl", _HashForControl, typeof(byte[]));
  254.         }
  255.        
  256.         public void OnDeserialization(object sender)
  257.         {
  258.             // Deserialization has already been performed
  259.             if (m_siInfo == null)
  260.                 return;
  261.            
  262.             _Name = m_siInfo.GetString("_Name");
  263.             _PublicKey = (byte[])m_siInfo.GetValue("_PublicKey", typeof(byte[]));
  264.             _PublicKeyToken = (byte[])m_siInfo.GetValue("_PublicKeyToken", typeof(byte[]));
  265.            
  266.             int lcid = (int)m_siInfo.GetInt32("_CultureInfo");
  267.             if (lcid != -1)
  268.                 _CultureInfo = new CultureInfo(lcid);
  269.            
  270.             _CodeBase = m_siInfo.GetString("_CodeBase");
  271.             _Version = (Version)m_siInfo.GetValue("_Version", typeof(Version));
  272.             _HashAlgorithm = (AssemblyHashAlgorithm)m_siInfo.GetValue("_HashAlgorithm", typeof(AssemblyHashAlgorithm));
  273.             _StrongNameKeyPair = (StrongNameKeyPair)m_siInfo.GetValue("_StrongNameKeyPair", typeof(StrongNameKeyPair));
  274.             _VersionCompatibility = (AssemblyVersionCompatibility)m_siInfo.GetValue("_VersionCompatibility", typeof(AssemblyVersionCompatibility));
  275.             _Flags = (AssemblyNameFlags)m_siInfo.GetValue("_Flags", typeof(AssemblyNameFlags));
  276.            
  277.             try {
  278.                 _HashAlgorithmForControl = (AssemblyHashAlgorithm)m_siInfo.GetValue("_HashAlgorithmForControl", typeof(AssemblyHashAlgorithm));
  279.                 _HashForControl = (byte[])m_siInfo.GetValue("_HashForControl", typeof(byte[]));
  280.             }
  281.             catch (SerializationException) {
  282.                 // RTM did not have these defined
  283.                 _HashAlgorithmForControl = AssemblyHashAlgorithm.None;
  284.                 _HashForControl = null;
  285.             }
  286.            
  287.             m_siInfo = null;
  288.         }
  289.        
  290.         public AssemblyName(string assemblyName)
  291.         {
  292.             if (assemblyName == null)
  293.                 throw new ArgumentNullException("assemblyName");
  294.             if ((assemblyName.Length == 0) || (assemblyName[0] == '\0'))
  295.                 throw new ArgumentException(Environment.GetResourceString("Format_StringZeroLength"));
  296.            
  297.             _Name = assemblyName;
  298.             nInit();
  299.         }
  300.        
  301.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  302.         public static extern bool ReferenceMatchesDefinition(AssemblyName reference, AssemblyName definition);
  303.        
  304.        
  305.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  306.         internal extern int nInit(out Assembly assembly, bool forIntrospection, bool raiseResolveEvent);
  307.        
  308.         internal void nInit()
  309.         {
  310.             Assembly dummy = null;
  311.             nInit(out dummy, false, false);
  312.         }
  313.        
  314.         // Constructs a new AssemblyName during deserialization.
  315.         internal AssemblyName(SerializationInfo info, StreamingContext context)
  316.         {
  317.             //The graph is not valid until OnDeserialization() has been called.
  318.             m_siInfo = info;
  319.         }
  320.        
  321.        
  322.         internal void Init(string name, byte[] publicKey, byte[] publicKeyToken, Version version, CultureInfo cultureInfo, AssemblyHashAlgorithm hashAlgorithm, AssemblyVersionCompatibility versionCompatibility, string codeBase, AssemblyNameFlags flags, StrongNameKeyPair keyPair
  323.         // Null if ref, matching Assembly if def
  324.         )
  325.         {
  326.             _Name = name;
  327.            
  328.             if (publicKey != null) {
  329.                 _PublicKey = new byte[publicKey.Length];
  330.                 Array.Copy(publicKey, _PublicKey, publicKey.Length);
  331.             }
  332.            
  333.             if (publicKeyToken != null) {
  334.                 _PublicKeyToken = new byte[publicKeyToken.Length];
  335.                 Array.Copy(publicKeyToken, _PublicKeyToken, publicKeyToken.Length);
  336.             }
  337.            
  338.             if (version != null)
  339.                 _Version = (Version)version.Clone();
  340.            
  341.             _CultureInfo = cultureInfo;
  342.             _HashAlgorithm = hashAlgorithm;
  343.             _VersionCompatibility = versionCompatibility;
  344.             _CodeBase = codeBase;
  345.             _Flags = flags;
  346.             _StrongNameKeyPair = keyPair;
  347.         }
  348.        
  349.         void _AssemblyName.GetTypeInfoCount(out uint pcTInfo)
  350.         {
  351.             throw new NotImplementedException();
  352.         }
  353.        
  354.         void _AssemblyName.GetTypeInfo(uint iTInfo, uint lcid, IntPtr ppTInfo)
  355.         {
  356.             throw new NotImplementedException();
  357.         }
  358.        
  359.         void _AssemblyName.GetIDsOfNames(        [In()]
  360. ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
  361.         {
  362.             throw new NotImplementedException();
  363.         }
  364.        
  365.         void _AssemblyName.Invoke(uint dispIdMember,         [In()]
  366. ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
  367.         {
  368.             throw new NotImplementedException();
  369.         }
  370.        
  371.        
  372.         // This call opens and closes the file, but does not add the
  373.         // assembly to the domain.
  374.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  375.         static internal extern AssemblyName nGetFileInformation(string s);
  376.        
  377.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  378.         private extern string nToString();
  379.        
  380.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  381.         private extern byte[] nGetPublicKeyToken();
  382.        
  383.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  384.         static internal extern string EscapeCodeBase(string codeBase);
  385.     }
  386. }

Developer Fusion