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 [StructLayout (LayoutKind.Sequential)]
57 public sealed class AssemblyName : ICloneable, ISerializable, IDeserializationCallback, _AssemblyName {
59 #pragma warning disable 169
60 #region Synch with object-internals.h
63 int major, minor, build, revision;
64 CultureInfo cultureinfo;
65 AssemblyNameFlags flags;
66 AssemblyHashAlgorithm hashalg;
67 StrongNameKeyPair keypair;
70 AssemblyVersionCompatibility versioncompat;
72 ProcessorArchitecture processor_architecture = ProcessorArchitecture.None;
74 #pragma warning restore 169
76 public AssemblyName ()
79 versioncompat = AssemblyVersionCompatibility.SameMachine;
82 [MethodImpl (MethodImplOptions.InternalCall)]
83 static extern bool ParseName (AssemblyName aname, string assemblyName);
85 public AssemblyName (string assemblyName)
87 if (assemblyName == null)
88 throw new ArgumentNullException ("assemblyName");
89 if (assemblyName.Length < 1)
90 throw new ArgumentException ("assemblyName cannot have zero length.");
92 if (!ParseName (this, assemblyName))
93 throw new FileLoadException ("The assembly name is invalid.");
96 [MonoLimitation ("Not used, as the values are too limited; Mono supports more")]
97 public ProcessorArchitecture ProcessorArchitecture {
99 return processor_architecture;
102 processor_architecture = value;
106 internal AssemblyName (SerializationInfo si, StreamingContext sc)
108 name = si.GetString ("_Name");
109 codebase = si.GetString ("_CodeBase");
110 version = (Version)si.GetValue ("_Version", typeof (Version));
111 publicKey = (byte[])si.GetValue ("_PublicKey", typeof (byte[]));
112 keyToken = (byte[])si.GetValue ("_PublicKeyToken", typeof (byte[]));
113 hashalg = (AssemblyHashAlgorithm)si.GetValue ("_HashAlgorithm", typeof (AssemblyHashAlgorithm));
114 keypair = (StrongNameKeyPair)si.GetValue ("_StrongNameKeyPair", typeof (StrongNameKeyPair));
115 versioncompat = (AssemblyVersionCompatibility)si.GetValue ("_VersionCompatibility", typeof (AssemblyVersionCompatibility));
116 flags = (AssemblyNameFlags)si.GetValue ("_Flags", typeof (AssemblyNameFlags));
117 int lcid = si.GetInt32 ("_CultureInfo");
118 if (lcid != -1) cultureinfo = new CultureInfo (lcid);
123 set { name = value; }
126 public string CodeBase {
127 get { return codebase; }
128 set { codebase = value; }
131 public string EscapedCodeBase {
133 if (codebase == null)
135 return Uri.EscapeString (codebase, false, true, true);
139 public CultureInfo CultureInfo {
140 get { return cultureinfo; }
141 set { cultureinfo = value; }
144 public AssemblyNameFlags Flags {
145 get { return flags; }
146 set { flags = value; }
149 public string FullName {
153 StringBuilder fname = new StringBuilder ();
154 if (Char.IsWhiteSpace (name [0]))
155 fname.Append ("\"" + name + "\"");
158 if (Version != null) {
159 fname.Append (", Version=");
160 fname.Append (Version.ToString ());
162 if (cultureinfo != null) {
163 fname.Append (", Culture=");
164 if (cultureinfo.LCID == CultureInfo.InvariantCulture.LCID)
165 fname.Append ("neutral");
167 fname.Append (cultureinfo.Name);
169 byte [] pub_tok = InternalGetPublicKeyToken ();
170 if (pub_tok != null) {
171 if (pub_tok.Length == 0)
172 fname.Append (", PublicKeyToken=null");
174 fname.Append (", PublicKeyToken=");
175 for (int i = 0; i < pub_tok.Length; i++)
176 fname.Append (pub_tok[i].ToString ("x2"));
180 if ((Flags & AssemblyNameFlags.Retargetable) != 0)
181 fname.Append (", Retargetable=Yes");
183 return fname.ToString ();
187 public AssemblyHashAlgorithm HashAlgorithm {
188 get { return hashalg; }
189 set { hashalg = value; }
192 public StrongNameKeyPair KeyPair {
193 get { return keypair; }
194 set { keypair = value; }
197 public Version Version {
205 major = minor = build = revision = 0;
210 revision = value.Revision;
215 public AssemblyVersionCompatibility VersionCompatibility {
216 get { return versioncompat; }
217 set { versioncompat = value; }
220 public override string ToString ()
222 string name = FullName;
223 return (name != null) ? name : base.ToString ();
226 public byte[] GetPublicKey()
231 public byte[] GetPublicKeyToken ()
233 if (keyToken != null)
235 else if (publicKey == null)
238 if (publicKey.Length == 0)
241 if (!IsPublicKeyValid)
242 throw new SecurityException ("The public key is not valid.");
244 keyToken = ComputePublicKeyToken ();
249 private bool IsPublicKeyValid {
251 // check for ECMA key
252 if (publicKey.Length == 16) {
255 while (i < publicKey.Length)
256 sum += publicKey [i++];
261 switch (publicKey [0]) {
262 case 0x00: // public key inside a header
263 if (publicKey.Length > 12 && publicKey [12] == 0x06) {
265 CryptoConvert.FromCapiPublicKeyBlob (
268 } catch (CryptographicException) {
272 case 0x06: // public key
274 CryptoConvert.FromCapiPublicKeyBlob (publicKey);
276 } catch (CryptographicException) {
279 case 0x07: // private key
287 private byte [] InternalGetPublicKeyToken ()
289 if (keyToken != null)
292 if (publicKey == null)
295 if (publicKey.Length == 0)
298 if (!IsPublicKeyValid)
299 throw new SecurityException ("The public key is not valid.");
301 return ComputePublicKeyToken ();
304 private byte [] ComputePublicKeyToken ()
306 HashAlgorithm ha = SHA1.Create ();
307 byte [] hash = ha.ComputeHash (publicKey);
308 // we need the last 8 bytes in reverse order
309 byte [] token = new byte [8];
310 Array.Copy (hash, (hash.Length - 8), token, 0, 8);
311 Array.Reverse (token, 0, 8);
316 public static bool ReferenceMatchesDefinition (AssemblyName reference, AssemblyName definition)
318 if (reference == null)
319 throw new ArgumentNullException ("reference");
320 if (definition == null)
321 throw new ArgumentNullException ("definition");
322 if (reference.Name != definition.Name)
324 throw new NotImplementedException ();
327 public void SetPublicKey (byte[] publicKey)
329 if (publicKey == null)
330 flags ^= AssemblyNameFlags.PublicKey;
332 flags |= AssemblyNameFlags.PublicKey;
333 this.publicKey = publicKey;
336 public void SetPublicKeyToken (byte[] publicKeyToken)
338 keyToken = publicKeyToken;
341 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
342 public void GetObjectData (SerializationInfo info, StreamingContext context)
345 throw new ArgumentNullException ("info");
347 info.AddValue ("_Name", name);
348 info.AddValue ("_PublicKey", publicKey);
349 info.AddValue ("_PublicKeyToken", keyToken);
350 info.AddValue ("_CultureInfo", cultureinfo != null ? cultureinfo.LCID : -1);
351 info.AddValue ("_CodeBase", codebase);
352 info.AddValue ("_Version", Version);
353 info.AddValue ("_HashAlgorithm", hashalg);
354 info.AddValue ("_HashAlgorithmForControl", AssemblyHashAlgorithm.None);
355 info.AddValue ("_StrongNameKeyPair", keypair);
356 info.AddValue ("_VersionCompatibility", versioncompat);
357 info.AddValue ("_Flags", flags);
358 info.AddValue ("_HashForControl", null);
361 public object Clone()
363 AssemblyName an = new AssemblyName ();
365 an.codebase = codebase;
369 an.revision = revision;
370 an.version = version;
371 an.cultureinfo = cultureinfo;
373 an.hashalg = hashalg;
374 an.keypair = keypair;
375 an.publicKey = publicKey;
376 an.keyToken = keyToken;
377 an.versioncompat = versioncompat;
381 public void OnDeserialization (object sender)
386 public static AssemblyName GetAssemblyName (string assemblyFile)
388 if (assemblyFile == null)
389 throw new ArgumentNullException ("assemblyFile");
391 AssemblyName aname = new AssemblyName ();
392 Assembly.InternalGetAssemblyName (Path.GetFullPath (assemblyFile), aname);
396 void _AssemblyName.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
398 throw new NotImplementedException ();
401 void _AssemblyName.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
403 throw new NotImplementedException ();
406 void _AssemblyName.GetTypeInfoCount (out uint pcTInfo)
408 throw new NotImplementedException ();
411 void _AssemblyName.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
412 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
414 throw new NotImplementedException ();