2 // System.Reflection/AssemblyName.cs
5 // Paolo Molaro (lupus@ximian.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2001 Ximian, Inc. http://www.ximian.com
9 // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Configuration.Assemblies;
33 using System.Globalization;
34 using System.Runtime.Serialization;
35 using System.Security.Cryptography;
36 using System.Security.Permissions;
38 using System.Runtime.InteropServices;
39 using System.Runtime.CompilerServices;
43 namespace System.Reflection {
46 // a. Uniform Resource Identifiers (URI): Generic Syntax
47 // http://www.ietf.org/rfc/rfc2396.txt
50 [MonoTODO ("Fix serialization compatibility with MS.NET")]
51 public sealed class AssemblyName : ICloneable, ISerializable, IDeserializationCallback {
54 int major, minor, build, revision;
55 CultureInfo cultureinfo;
56 AssemblyNameFlags flags;
57 AssemblyHashAlgorithm hashalg;
58 StrongNameKeyPair keypair;
61 AssemblyVersionCompatibility versioncompat;
64 public AssemblyName ()
67 versioncompat = AssemblyVersionCompatibility.SameMachine;
71 public AssemblyName (string assemblyName)
77 internal AssemblyName (SerializationInfo si, StreamingContext sc)
79 name = si.GetString ("_Name");
80 codebase = si.GetString ("_CodeBase");
81 version = (Version)si.GetValue ("_Version", typeof (Version));
82 publicKey = (byte[])si.GetValue ("_PublicKey", typeof (byte[]));
83 keyToken = (byte[])si.GetValue ("_PublicToken", typeof (byte[]));
84 hashalg = (AssemblyHashAlgorithm)si.GetValue ("_HashAlgorithm", typeof (AssemblyHashAlgorithm));
85 keypair = (StrongNameKeyPair)si.GetValue ("_StrongNameKeyPair", typeof (StrongNameKeyPair));
86 versioncompat = (AssemblyVersionCompatibility)si.GetValue ("_VersionCompatibility", typeof (AssemblyVersionCompatibility));
87 flags = (AssemblyNameFlags)si.GetValue ("_Flags", typeof (AssemblyNameFlags));
88 int lcid = si.GetInt32 ("_CultureInfo");
89 if (lcid != -1) cultureinfo = new CultureInfo (lcid);
97 public string CodeBase {
98 get { return codebase; }
99 set { codebase = value; }
102 public string EscapedCodeBase {
104 if (codebase == null)
106 return Uri.EscapeString (codebase, false, true, true);
110 public CultureInfo CultureInfo {
111 get { return cultureinfo; }
112 set { cultureinfo = value; }
115 public AssemblyNameFlags Flags {
116 get { return flags; }
117 set { flags = value; }
120 public string FullName {
124 StringBuilder fname = new StringBuilder ();
126 if (Version.ToString () != "0.0.0.0") {
127 fname.Append (", Version=");
128 fname.Append (Version.ToString ());
130 if (cultureinfo != null) {
131 fname.Append (", Culture=");
132 if (cultureinfo.LCID == CultureInfo.InvariantCulture.LCID)
133 fname.Append ("neutral");
135 fname.Append (cultureinfo.Name);
137 byte[] pub_tok = GetPublicKeyToken ();
138 if (pub_tok != null) {
139 if (pub_tok.Length == 0)
140 fname.Append (", PublicKeyToken=null");
142 fname.Append (", PublicKeyToken=");
143 for (int i = 0; i < pub_tok.Length; i++)
144 fname.Append (pub_tok[i].ToString ("x2"));
147 return fname.ToString ();
151 public AssemblyHashAlgorithm HashAlgorithm {
152 get { return hashalg; }
153 set { hashalg = value; }
156 public StrongNameKeyPair KeyPair {
157 get { return keypair; }
158 set { keypair = value; }
161 public Version Version {
163 if (version != null) return version;
168 version = new Version (major, minor);
171 version = new Version (major, minor, build);
173 version = new Version (major, minor, build, revision);
182 revision = value.Revision;
187 public AssemblyVersionCompatibility VersionCompatibility {
188 get { return versioncompat; }
189 set { versioncompat = value; }
192 public override string ToString ()
194 string name = FullName;
195 return (name != null) ? name : base.ToString ();
198 public byte[] GetPublicKey()
200 // to match MS implementation -- funny one
201 if (publicKey != null)
203 else if (name == null)
209 public byte[] GetPublicKeyToken()
211 if (keyToken != null)
213 else if (publicKey == null)
216 HashAlgorithm ha = null;
218 case AssemblyHashAlgorithm.MD5:
222 // None default to SHA1
226 byte[] hash = ha.ComputeHash (publicKey);
227 // we need the last 8 bytes in reverse order
228 keyToken = new byte [8];
229 Array.Copy (hash, (hash.Length - 8), keyToken, 0, 8);
230 Array.Reverse (keyToken, 0, 8);
235 public void SetPublicKey (byte[] publicKey)
237 flags = AssemblyNameFlags.PublicKey;
238 this.publicKey = publicKey;
241 public void SetPublicKeyToken (byte[] publicKeyToken)
243 keyToken = publicKeyToken;
246 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
247 public void GetObjectData (SerializationInfo info, StreamingContext context)
250 throw new ArgumentNullException ("info");
252 info.AddValue ("_Name", name);
253 info.AddValue ("_PublicKey", publicKey);
254 info.AddValue ("_PublicToken", keyToken);
255 info.AddValue ("_CultureInfo", cultureinfo != null ? cultureinfo.LCID : -1);
256 info.AddValue ("_CodeBase", codebase);
257 info.AddValue ("_Version", Version);
258 info.AddValue ("_HashAlgorithm", hashalg);
259 info.AddValue ("_HashAlgorithmForControl", AssemblyHashAlgorithm.None);
260 info.AddValue ("_StrongNameKeyPair", keypair);
261 info.AddValue ("_VersionCompatibility", versioncompat);
262 info.AddValue ("_Flags", flags);
263 info.AddValue ("_HashForControl", null);
266 public object Clone()
268 AssemblyName an = new AssemblyName ();
270 an.codebase = codebase;
274 an.revision = revision;
275 an.cultureinfo = cultureinfo;
277 an.hashalg = hashalg;
278 an.keypair = keypair;
279 an.publicKey = publicKey;
280 an.keyToken = keyToken;
281 an.versioncompat = versioncompat;
285 public void OnDeserialization (object sender)
290 public static AssemblyName GetAssemblyName (string assemblyFile)
292 if (assemblyFile == null)
293 throw new ArgumentNullException ("assemblyFile");
295 AssemblyName aname = new AssemblyName ();
296 Assembly.InternalGetAssemblyName (System.IO.Path.GetFullPath (assemblyFile), aname);