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;
36 using System.Security.Cryptography;
37 using System.Security.Permissions;
39 using System.Runtime.InteropServices;
40 using System.Runtime.CompilerServices;
44 using Mono.Security.Cryptography;
46 namespace System.Reflection {
49 // a. Uniform Resource Identifiers (URI): Generic Syntax
50 // http://www.ietf.org/rfc/rfc2396.txt
54 [ComDefaultInterfaceAttribute (typeof (_AssemblyName))]
57 [ClassInterfaceAttribute (ClassInterfaceType.None)]
58 public sealed class AssemblyName : ICloneable, ISerializable, IDeserializationCallback, _AssemblyName {
60 #pragma warning disable 169
61 #region Synch with object-internals.h
64 int major, minor, build, revision;
65 CultureInfo cultureinfo;
66 AssemblyNameFlags flags;
67 AssemblyHashAlgorithm hashalg;
68 StrongNameKeyPair keypair;
71 AssemblyVersionCompatibility versioncompat;
74 ProcessorArchitecture processor_architecture = ProcessorArchitecture.None;
76 int processor_architecture;
79 #pragma warning restore 169
81 public AssemblyName ()
84 versioncompat = AssemblyVersionCompatibility.SameMachine;
87 #if NET_2_0 || BOOTSTRAP_NET_2_0
88 [MethodImpl (MethodImplOptions.InternalCall)]
89 static extern bool ParseName (AssemblyName aname, string assemblyName);
91 public AssemblyName (string assemblyName)
93 if (assemblyName == null)
94 throw new ArgumentNullException ("assemblyName");
95 if (assemblyName.Length < 1)
96 throw new ArgumentException ("assemblyName cannot have zero length.");
98 if (!ParseName (this, assemblyName))
99 throw new FileLoadException ("The assembly name is invalid.");
104 [MonoTODO ("Not used, as the values are too limited; Mono supports more")]
105 public ProcessorArchitecture ProcessorArchitecture {
107 return processor_architecture;
110 processor_architecture = value;
115 internal AssemblyName (SerializationInfo si, StreamingContext sc)
117 name = si.GetString ("_Name");
118 codebase = si.GetString ("_CodeBase");
119 version = (Version)si.GetValue ("_Version", typeof (Version));
120 publicKey = (byte[])si.GetValue ("_PublicKey", typeof (byte[]));
121 keyToken = (byte[])si.GetValue ("_PublicKeyToken", typeof (byte[]));
122 hashalg = (AssemblyHashAlgorithm)si.GetValue ("_HashAlgorithm", typeof (AssemblyHashAlgorithm));
123 keypair = (StrongNameKeyPair)si.GetValue ("_StrongNameKeyPair", typeof (StrongNameKeyPair));
124 versioncompat = (AssemblyVersionCompatibility)si.GetValue ("_VersionCompatibility", typeof (AssemblyVersionCompatibility));
125 flags = (AssemblyNameFlags)si.GetValue ("_Flags", typeof (AssemblyNameFlags));
126 int lcid = si.GetInt32 ("_CultureInfo");
127 if (lcid != -1) cultureinfo = new CultureInfo (lcid);
132 set { name = value; }
135 public string CodeBase {
136 get { return codebase; }
137 set { codebase = value; }
140 public string EscapedCodeBase {
142 if (codebase == null)
144 return Uri.EscapeString (codebase, false, true, true);
148 public CultureInfo CultureInfo {
149 get { return cultureinfo; }
150 set { cultureinfo = value; }
153 public AssemblyNameFlags Flags {
154 get { return flags; }
155 set { flags = value; }
158 public string FullName {
166 StringBuilder fname = new StringBuilder ();
168 if (Version != null) {
169 fname.Append (", Version=");
170 fname.Append (Version.ToString ());
172 if (cultureinfo != null) {
173 fname.Append (", Culture=");
174 if (cultureinfo.LCID == CultureInfo.InvariantCulture.LCID)
175 fname.Append ("neutral");
177 fname.Append (cultureinfo.Name);
179 byte [] pub_tok = InternalGetPublicKeyToken ();
180 if (pub_tok != null) {
181 if (pub_tok.Length == 0)
182 fname.Append (", PublicKeyToken=null");
184 fname.Append (", PublicKeyToken=");
185 for (int i = 0; i < pub_tok.Length; i++)
186 fname.Append (pub_tok[i].ToString ("x2"));
190 if ((Flags & AssemblyNameFlags.Retargetable) != 0)
191 fname.Append (", Retargetable=Yes");
193 return fname.ToString ();
197 public AssemblyHashAlgorithm HashAlgorithm {
198 get { return hashalg; }
199 set { hashalg = value; }
202 public StrongNameKeyPair KeyPair {
203 get { return keypair; }
204 set { keypair = value; }
207 public Version Version {
215 major = minor = build = revision = 0;
220 revision = value.Revision;
225 public AssemblyVersionCompatibility VersionCompatibility {
226 get { return versioncompat; }
227 set { versioncompat = value; }
230 public override string ToString ()
232 string name = FullName;
233 return (name != null) ? name : base.ToString ();
236 public byte[] GetPublicKey()
241 public byte[] GetPublicKeyToken ()
243 if (keyToken != null)
245 else if (publicKey == null)
249 if (publicKey.Length == 0)
252 if (!IsPublicKeyValid)
253 throw new SecurityException ("The public key is not valid.");
255 keyToken = ComputePublicKeyToken ();
258 if (publicKey.Length == 0)
261 keyToken = ComputePublicKeyToken ();
268 private bool IsPublicKeyValid {
270 // check for ECMA key
271 if (publicKey.Length == 16) {
274 while (i < publicKey.Length)
275 sum += publicKey [i++];
280 switch (publicKey [0]) {
281 case 0x00: // public key inside a header
282 if (publicKey.Length > 12 && publicKey [12] == 0x06) {
284 CryptoConvert.FromCapiPublicKeyBlob (
287 } catch (CryptographicException) {
291 case 0x06: // public key
293 CryptoConvert.FromCapiPublicKeyBlob (publicKey);
295 } catch (CryptographicException) {
298 case 0x07: // private key
307 private byte [] InternalGetPublicKeyToken ()
310 if (keyToken != null)
313 if (publicKey == null)
316 if (publicKey.Length == 0)
319 if (!IsPublicKeyValid)
320 throw new SecurityException ("The public key is not valid.");
322 return ComputePublicKeyToken ();
324 if ((Flags & AssemblyNameFlags.PublicKey) != 0) {
325 if (publicKey == null)
327 if (publicKey.Length == 0)
331 if (keyToken != null && publicKey == null)
334 if (publicKey == null)
337 if (publicKey.Length == 0)
340 if (keyToken != null && keyToken.Length == 0)
341 return ComputePublicKeyToken ();
343 keyToken = ComputePublicKeyToken ();
348 private byte [] ComputePublicKeyToken ()
350 HashAlgorithm ha = SHA1.Create ();
351 byte [] hash = ha.ComputeHash (publicKey);
352 // we need the last 8 bytes in reverse order
353 byte [] token = new byte [8];
354 Array.Copy (hash, (hash.Length - 8), token, 0, 8);
355 Array.Reverse (token, 0, 8);
361 public static bool ReferenceMatchesDefinition (AssemblyName reference, AssemblyName definition)
363 if (reference == null)
364 throw new ArgumentNullException ("reference");
365 if (definition == null)
366 throw new ArgumentNullException ("definition");
367 if (reference.Name != definition.Name)
369 throw new NotImplementedException ();
373 public void SetPublicKey (byte[] publicKey)
376 if (publicKey == null)
377 flags ^= AssemblyNameFlags.PublicKey;
379 flags |= AssemblyNameFlags.PublicKey;
381 flags |= AssemblyNameFlags.PublicKey;
383 this.publicKey = publicKey;
386 public void SetPublicKeyToken (byte[] publicKeyToken)
388 keyToken = publicKeyToken;
391 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
392 public void GetObjectData (SerializationInfo info, StreamingContext context)
395 throw new ArgumentNullException ("info");
397 info.AddValue ("_Name", name);
398 info.AddValue ("_PublicKey", publicKey);
399 info.AddValue ("_PublicKeyToken", keyToken);
400 info.AddValue ("_CultureInfo", cultureinfo != null ? cultureinfo.LCID : -1);
401 info.AddValue ("_CodeBase", codebase);
402 info.AddValue ("_Version", Version);
403 info.AddValue ("_HashAlgorithm", hashalg);
404 info.AddValue ("_HashAlgorithmForControl", AssemblyHashAlgorithm.None);
405 info.AddValue ("_StrongNameKeyPair", keypair);
406 info.AddValue ("_VersionCompatibility", versioncompat);
407 info.AddValue ("_Flags", flags);
408 info.AddValue ("_HashForControl", null);
411 public object Clone()
413 AssemblyName an = new AssemblyName ();
415 an.codebase = codebase;
419 an.revision = revision;
420 an.version = version;
421 an.cultureinfo = cultureinfo;
423 an.hashalg = hashalg;
424 an.keypair = keypair;
425 an.publicKey = publicKey;
426 an.keyToken = keyToken;
427 an.versioncompat = versioncompat;
431 public void OnDeserialization (object sender)
436 public static AssemblyName GetAssemblyName (string assemblyFile)
438 if (assemblyFile == null)
439 throw new ArgumentNullException ("assemblyFile");
441 AssemblyName aname = new AssemblyName ();
442 Assembly.InternalGetAssemblyName (Path.GetFullPath (assemblyFile), aname);
447 void _AssemblyName.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
449 throw new NotImplementedException ();
452 void _AssemblyName.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
454 throw new NotImplementedException ();
457 void _AssemblyName.GetTypeInfoCount (out uint pcTInfo)
459 throw new NotImplementedException ();
462 void _AssemblyName.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
463 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
465 throw new NotImplementedException ();