2 // System.Reflection/AssemblyName.cs
5 // Paolo Molaro (lupus@ximian.com)
6 // Sebastien Pouliot (spouliot@motus.com)
8 // (C) 2001 Ximian, Inc. http://www.ximian.com
9 // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
13 using System.Configuration.Assemblies;
14 using System.Globalization;
15 using System.Reflection;
16 using System.Runtime.Serialization;
17 using System.Security.Cryptography;
19 using System.Runtime.InteropServices;
21 namespace System.Reflection {
24 // a. Uniform Resource Identifiers (URI): Generic Syntax
25 // http://www.ietf.org/rfc/rfc2396.txt
28 public sealed class AssemblyName : ICloneable, ISerializable, IDeserializationCallback {
31 int major, minor, build, revision;
32 CultureInfo cultureinfo;
33 AssemblyNameFlags flags;
34 AssemblyHashAlgorithm hashalg;
35 StrongNameKeyPair keypair;
38 AssemblyVersionCompatibility versioncompat;
40 public AssemblyName ()
43 versioncompat = AssemblyVersionCompatibility.SameMachine;
46 internal AssemblyName (SerializationInfo si, StreamingContext sc)
48 name = si.GetString ("_Name");
49 codebase = si.GetString ("_CodeBase");
50 Version = (Version)si.GetValue ("_Version", typeof (Version));
58 public string CodeBase {
59 get { return codebase; }
60 set { codebase = value; }
63 [MonoTODO("RFC 2396")]
64 private string Escape (string url)
66 // we already have code in mcs\class\System\System\Uri.cs
67 // but Uri class ins't part of corlib !
72 public string EscapedCodeBase {
73 get { return Escape (codebase); }
76 public CultureInfo CultureInfo {
77 get { return cultureinfo; }
78 set { cultureinfo = value; }
81 public AssemblyNameFlags Flags {
83 set { flags = value; }
86 [MonoTODO("incomplete")]
87 public string FullName {
91 StringBuilder fname = new StringBuilder ();
93 fname.Append (", Version=");
94 fname.Append (Version.ToString ());
95 fname.Append (", Culture=");
96 if ((cultureinfo == null) || (cultureinfo.LCID == CultureInfo.InvariantCulture.LCID))
97 fname.Append ("neutral");
99 fname.Append (cultureinfo.ToString ()); // ???
101 fname.Append (", PublicKeyToken=null");
103 return fname.ToString ();
107 public AssemblyHashAlgorithm HashAlgorithm {
108 get { return hashalg; }
109 set { hashalg = value; }
112 public StrongNameKeyPair KeyPair {
113 get { return keypair; }
114 set { keypair = value; }
117 public Version Version {
122 return new Version (major, minor);
125 return new Version (major, minor, build);
127 return new Version (major, minor, build, revision);
134 revision = value.Revision;
138 public AssemblyVersionCompatibility VersionCompatibility {
139 get { return versioncompat; }
140 set { versioncompat = value; }
143 public override string ToString ()
145 string name = FullName;
146 return (name != null) ? name : base.ToString ();
149 public byte[] GetPublicKey()
151 // to match MS implementation -- funny one
152 if (publicKey != null)
154 else if (name == null)
160 public byte[] GetPublicKeyToken()
162 if (keyToken != null)
164 else if (publicKey == null)
167 HashAlgorithm ha = null;
169 case AssemblyHashAlgorithm.MD5:
173 // None default to SHA1
177 byte[] hash = ha.ComputeHash (publicKey);
178 // we need the last 8 bytes in reverse order
179 keyToken = new byte [8];
180 Array.Copy (hash, (hash.Length - 8), keyToken, 0, 8);
181 Array.Reverse (keyToken, 0, 8);
186 public void SetPublicKey (byte[] publicKey)
188 flags = AssemblyNameFlags.PublicKey;
189 this.publicKey = publicKey;
192 public void SetPublicKeyToken (byte[] publicKeyToken)
194 keyToken = publicKeyToken;
197 public void GetObjectData (SerializationInfo info, StreamingContext context)
199 info.AddValue ("_Name", name);
200 info.AddValue ("_CodeBase", codebase);
201 info.AddValue ("_Version", Version);
204 // required to implement ICloneable
206 public object Clone()
211 // required to implement IDeserializationCallback
213 public void OnDeserialization (object sender)
217 public static AssemblyName GetAssemblyName (string assemblyFile)
219 Assembly a = Assembly.LoadFrom (assemblyFile);