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
53 [ComDefaultInterfaceAttribute (typeof (_AssemblyName))]
55 [ClassInterfaceAttribute (ClassInterfaceType.None)]
56 public sealed class AssemblyName : ICloneable, ISerializable, IDeserializationCallback, _AssemblyName {
58 #pragma warning disable 169
59 #region Synch with object-internals.h
62 int major, minor, build, revision;
63 CultureInfo cultureinfo;
64 AssemblyNameFlags flags;
65 AssemblyHashAlgorithm hashalg;
66 StrongNameKeyPair keypair;
69 AssemblyVersionCompatibility versioncompat;
71 ProcessorArchitecture processor_architecture = ProcessorArchitecture.None;
73 #pragma warning restore 169
75 public AssemblyName ()
78 versioncompat = AssemblyVersionCompatibility.SameMachine;
81 [MethodImpl (MethodImplOptions.InternalCall)]
82 static extern bool ParseName (AssemblyName aname, string assemblyName);
84 public AssemblyName (string assemblyName)
86 if (assemblyName == null)
87 throw new ArgumentNullException ("assemblyName");
88 if (assemblyName.Length < 1)
89 throw new ArgumentException ("assemblyName cannot have zero length.");
91 if (!ParseName (this, assemblyName))
92 throw new FileLoadException ("The assembly name is invalid.");
95 [MonoLimitation ("Not used, as the values are too limited; Mono supports more")]
96 public ProcessorArchitecture ProcessorArchitecture {
98 return processor_architecture;
101 processor_architecture = value;
105 internal AssemblyName (SerializationInfo si, StreamingContext sc)
107 name = si.GetString ("_Name");
108 codebase = si.GetString ("_CodeBase");
109 version = (Version)si.GetValue ("_Version", typeof (Version));
110 publicKey = (byte[])si.GetValue ("_PublicKey", typeof (byte[]));
111 keyToken = (byte[])si.GetValue ("_PublicKeyToken", typeof (byte[]));
112 hashalg = (AssemblyHashAlgorithm)si.GetValue ("_HashAlgorithm", typeof (AssemblyHashAlgorithm));
113 keypair = (StrongNameKeyPair)si.GetValue ("_StrongNameKeyPair", typeof (StrongNameKeyPair));
114 versioncompat = (AssemblyVersionCompatibility)si.GetValue ("_VersionCompatibility", typeof (AssemblyVersionCompatibility));
115 flags = (AssemblyNameFlags)si.GetValue ("_Flags", typeof (AssemblyNameFlags));
116 int lcid = si.GetInt32 ("_CultureInfo");
117 if (lcid != -1) cultureinfo = new CultureInfo (lcid);
122 set { name = value; }
125 public string CodeBase {
126 get { return codebase; }
127 set { codebase = value; }
130 public string EscapedCodeBase {
132 if (codebase == null)
134 return Uri.EscapeString (codebase, false, true, true);
138 public CultureInfo CultureInfo {
139 get { return cultureinfo; }
140 set { cultureinfo = value; }
143 public AssemblyNameFlags Flags {
144 get { return flags; }
145 set { flags = value; }
148 public string FullName {
152 StringBuilder fname = new StringBuilder ();
154 if (Version != null) {
155 fname.Append (", Version=");
156 fname.Append (Version.ToString ());
158 if (cultureinfo != null) {
159 fname.Append (", Culture=");
160 if (cultureinfo.LCID == CultureInfo.InvariantCulture.LCID)
161 fname.Append ("neutral");
163 fname.Append (cultureinfo.Name);
165 byte [] pub_tok = InternalGetPublicKeyToken ();
166 if (pub_tok != null) {
167 if (pub_tok.Length == 0)
168 fname.Append (", PublicKeyToken=null");
170 fname.Append (", PublicKeyToken=");
171 for (int i = 0; i < pub_tok.Length; i++)
172 fname.Append (pub_tok[i].ToString ("x2"));
176 if ((Flags & AssemblyNameFlags.Retargetable) != 0)
177 fname.Append (", Retargetable=Yes");
179 return fname.ToString ();
183 public AssemblyHashAlgorithm HashAlgorithm {
184 get { return hashalg; }
185 set { hashalg = value; }
188 public StrongNameKeyPair KeyPair {
189 get { return keypair; }
190 set { keypair = value; }
193 public Version Version {
201 major = minor = build = revision = 0;
206 revision = value.Revision;
211 public AssemblyVersionCompatibility VersionCompatibility {
212 get { return versioncompat; }
213 set { versioncompat = value; }
216 public override string ToString ()
218 string name = FullName;
219 return (name != null) ? name : base.ToString ();
222 public byte[] GetPublicKey()
227 public byte[] GetPublicKeyToken ()
229 if (keyToken != null)
231 else if (publicKey == null)
234 if (publicKey.Length == 0)
237 if (!IsPublicKeyValid)
238 throw new SecurityException ("The public key is not valid.");
240 keyToken = ComputePublicKeyToken ();
245 private bool IsPublicKeyValid {
247 // check for ECMA key
248 if (publicKey.Length == 16) {
251 while (i < publicKey.Length)
252 sum += publicKey [i++];
257 switch (publicKey [0]) {
258 case 0x00: // public key inside a header
259 if (publicKey.Length > 12 && publicKey [12] == 0x06) {
261 CryptoConvert.FromCapiPublicKeyBlob (
264 } catch (CryptographicException) {
268 case 0x06: // public key
270 CryptoConvert.FromCapiPublicKeyBlob (publicKey);
272 } catch (CryptographicException) {
275 case 0x07: // private key
283 private byte [] InternalGetPublicKeyToken ()
285 if (keyToken != null)
288 if (publicKey == null)
291 if (publicKey.Length == 0)
294 if (!IsPublicKeyValid)
295 throw new SecurityException ("The public key is not valid.");
297 return ComputePublicKeyToken ();
300 private byte [] ComputePublicKeyToken ()
302 HashAlgorithm ha = SHA1.Create ();
303 byte [] hash = ha.ComputeHash (publicKey);
304 // we need the last 8 bytes in reverse order
305 byte [] token = new byte [8];
306 Array.Copy (hash, (hash.Length - 8), token, 0, 8);
307 Array.Reverse (token, 0, 8);
312 public static bool ReferenceMatchesDefinition (AssemblyName reference, AssemblyName definition)
314 if (reference == null)
315 throw new ArgumentNullException ("reference");
316 if (definition == null)
317 throw new ArgumentNullException ("definition");
318 if (reference.Name != definition.Name)
320 throw new NotImplementedException ();
323 public void SetPublicKey (byte[] publicKey)
325 if (publicKey == null)
326 flags ^= AssemblyNameFlags.PublicKey;
328 flags |= AssemblyNameFlags.PublicKey;
329 this.publicKey = publicKey;
332 public void SetPublicKeyToken (byte[] publicKeyToken)
334 keyToken = publicKeyToken;
337 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
338 public void GetObjectData (SerializationInfo info, StreamingContext context)
341 throw new ArgumentNullException ("info");
343 info.AddValue ("_Name", name);
344 info.AddValue ("_PublicKey", publicKey);
345 info.AddValue ("_PublicKeyToken", keyToken);
346 info.AddValue ("_CultureInfo", cultureinfo != null ? cultureinfo.LCID : -1);
347 info.AddValue ("_CodeBase", codebase);
348 info.AddValue ("_Version", Version);
349 info.AddValue ("_HashAlgorithm", hashalg);
350 info.AddValue ("_HashAlgorithmForControl", AssemblyHashAlgorithm.None);
351 info.AddValue ("_StrongNameKeyPair", keypair);
352 info.AddValue ("_VersionCompatibility", versioncompat);
353 info.AddValue ("_Flags", flags);
354 info.AddValue ("_HashForControl", null);
357 public object Clone()
359 AssemblyName an = new AssemblyName ();
361 an.codebase = codebase;
365 an.revision = revision;
366 an.version = version;
367 an.cultureinfo = cultureinfo;
369 an.hashalg = hashalg;
370 an.keypair = keypair;
371 an.publicKey = publicKey;
372 an.keyToken = keyToken;
373 an.versioncompat = versioncompat;
377 public void OnDeserialization (object sender)
382 public static AssemblyName GetAssemblyName (string assemblyFile)
384 if (assemblyFile == null)
385 throw new ArgumentNullException ("assemblyFile");
387 AssemblyName aname = new AssemblyName ();
388 Assembly.InternalGetAssemblyName (Path.GetFullPath (assemblyFile), aname);
392 void _AssemblyName.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
394 throw new NotImplementedException ();
397 void _AssemblyName.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
399 throw new NotImplementedException ();
402 void _AssemblyName.GetTypeInfoCount (out uint pcTInfo)
404 throw new NotImplementedException ();
407 void _AssemblyName.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
408 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
410 throw new NotImplementedException ();