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)]
58 public sealed class AssemblyName : ICloneable, ISerializable, IDeserializationCallback {
60 public sealed class AssemblyName : ICloneable, ISerializable, IDeserializationCallback, _AssemblyName {
62 #pragma warning disable 169
63 #region Synch with object-internals.h
66 int major, minor, build, revision;
67 CultureInfo cultureinfo;
68 AssemblyNameFlags flags;
69 AssemblyHashAlgorithm hashalg;
70 StrongNameKeyPair keypair;
73 AssemblyVersionCompatibility versioncompat;
75 ProcessorArchitecture processor_architecture = ProcessorArchitecture.None;
77 #pragma warning restore 169
79 AssemblyContentType contentType;
80 public AssemblyName ()
83 versioncompat = AssemblyVersionCompatibility.SameMachine;
86 [MethodImpl (MethodImplOptions.InternalCall)]
87 static extern bool ParseName (AssemblyName aname, string assemblyName);
89 public AssemblyName (string assemblyName)
91 if (assemblyName == null)
92 throw new ArgumentNullException ("assemblyName");
93 if (assemblyName.Length < 1)
94 throw new ArgumentException ("assemblyName cannot have zero length.");
96 if (!ParseName (this, assemblyName))
97 throw new FileLoadException ("The assembly name is invalid.");
100 [MonoLimitation ("Not used, as the values are too limited; Mono supports more")]
101 public ProcessorArchitecture ProcessorArchitecture {
103 return processor_architecture;
106 processor_architecture = value;
110 internal AssemblyName (SerializationInfo si, StreamingContext sc)
112 name = si.GetString ("_Name");
113 codebase = si.GetString ("_CodeBase");
114 version = (Version)si.GetValue ("_Version", typeof (Version));
115 publicKey = (byte[])si.GetValue ("_PublicKey", typeof (byte[]));
116 keyToken = (byte[])si.GetValue ("_PublicKeyToken", typeof (byte[]));
117 hashalg = (AssemblyHashAlgorithm)si.GetValue ("_HashAlgorithm", typeof (AssemblyHashAlgorithm));
118 keypair = (StrongNameKeyPair)si.GetValue ("_StrongNameKeyPair", typeof (StrongNameKeyPair));
119 versioncompat = (AssemblyVersionCompatibility)si.GetValue ("_VersionCompatibility", typeof (AssemblyVersionCompatibility));
120 flags = (AssemblyNameFlags)si.GetValue ("_Flags", typeof (AssemblyNameFlags));
121 int lcid = si.GetInt32 ("_CultureInfo");
122 if (lcid != -1) cultureinfo = new CultureInfo (lcid);
127 set { name = value; }
130 public string CodeBase {
131 get { return codebase; }
132 set { codebase = value; }
135 public string EscapedCodeBase {
137 if (codebase == null)
139 return Uri.EscapeString (codebase, false, true, true);
143 public CultureInfo CultureInfo {
144 get { return cultureinfo; }
145 set { cultureinfo = value; }
148 public AssemblyNameFlags Flags {
149 get { return flags; }
150 set { flags = value; }
153 public string FullName {
157 StringBuilder fname = new StringBuilder ();
158 if (Char.IsWhiteSpace (name [0]))
159 fname.Append ("\"" + name + "\"");
162 if (Version != null) {
163 fname.Append (", Version=");
164 fname.Append (Version.ToString ());
166 if (cultureinfo != null) {
167 fname.Append (", Culture=");
168 if (cultureinfo.LCID == CultureInfo.InvariantCulture.LCID)
169 fname.Append ("neutral");
171 fname.Append (cultureinfo.Name);
173 byte [] pub_tok = InternalGetPublicKeyToken ();
174 if (pub_tok != null) {
175 if (pub_tok.Length == 0)
176 fname.Append (", PublicKeyToken=null");
178 fname.Append (", PublicKeyToken=");
179 for (int i = 0; i < pub_tok.Length; i++)
180 fname.Append (pub_tok[i].ToString ("x2"));
184 if ((Flags & AssemblyNameFlags.Retargetable) != 0)
185 fname.Append (", Retargetable=Yes");
187 return fname.ToString ();
191 public AssemblyHashAlgorithm HashAlgorithm {
192 get { return hashalg; }
193 set { hashalg = value; }
196 public StrongNameKeyPair KeyPair {
197 get { return keypair; }
198 set { keypair = value; }
201 public Version Version {
209 major = minor = build = revision = 0;
214 revision = value.Revision;
219 public AssemblyVersionCompatibility VersionCompatibility {
220 get { return versioncompat; }
221 set { versioncompat = value; }
224 public override string ToString ()
226 string name = FullName;
227 return (name != null) ? name : base.ToString ();
230 public byte[] GetPublicKey()
235 public byte[] GetPublicKeyToken ()
237 if (keyToken != null)
239 if (publicKey == null)
242 if (publicKey.Length == 0)
243 return EmptyArray<byte>.Value;
245 if (!IsPublicKeyValid)
246 throw new SecurityException ("The public key is not valid.");
248 keyToken = ComputePublicKeyToken ();
252 private bool IsPublicKeyValid {
254 // check for ECMA key
255 if (publicKey.Length == 16) {
258 while (i < publicKey.Length)
259 sum += publicKey [i++];
264 switch (publicKey [0]) {
265 case 0x00: // public key inside a header
266 if (publicKey.Length > 12 && publicKey [12] == 0x06) {
271 CryptoConvert.FromCapiPublicKeyBlob (
274 } catch (CryptographicException) {
279 case 0x06: // public key
284 CryptoConvert.FromCapiPublicKeyBlob (publicKey);
286 } catch (CryptographicException) {
290 case 0x07: // private key
298 private byte [] InternalGetPublicKeyToken ()
300 if (keyToken != null)
303 if (publicKey == null)
306 if (publicKey.Length == 0)
307 return EmptyArray<byte>.Value;
309 if (!IsPublicKeyValid)
310 throw new SecurityException ("The public key is not valid.");
312 return ComputePublicKeyToken ();
315 [MethodImplAttribute (MethodImplOptions.InternalCall)]
316 extern unsafe static void get_public_token (byte* token, byte* pubkey, int len);
318 private unsafe byte [] ComputePublicKeyToken ()
320 byte [] token = new byte [8];
321 fixed (byte* pkt = token)
322 fixed (byte *pk = publicKey)
323 get_public_token (pkt, pk, publicKey.Length);
327 public static bool ReferenceMatchesDefinition (AssemblyName reference, AssemblyName definition)
329 if (reference == null)
330 throw new ArgumentNullException ("reference");
331 if (definition == null)
332 throw new ArgumentNullException ("definition");
334 // we only compare the simple assembly name to be consistent with MS .NET,
335 // which is the result of a bug in their implementation (see https://connect.microsoft.com/VisualStudio/feedback/details/752902)
336 return string.Equals (reference.Name, definition.Name, StringComparison.OrdinalIgnoreCase);
339 public void SetPublicKey (byte[] publicKey)
341 if (publicKey == null)
342 flags ^= AssemblyNameFlags.PublicKey;
344 flags |= AssemblyNameFlags.PublicKey;
345 this.publicKey = publicKey;
348 public void SetPublicKeyToken (byte[] publicKeyToken)
350 keyToken = publicKeyToken;
353 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
354 public void GetObjectData (SerializationInfo info, StreamingContext context)
357 throw new ArgumentNullException ("info");
359 info.AddValue ("_Name", name);
360 info.AddValue ("_PublicKey", publicKey);
361 info.AddValue ("_PublicKeyToken", keyToken);
362 info.AddValue ("_CultureInfo", cultureinfo != null ? cultureinfo.LCID : -1);
363 info.AddValue ("_CodeBase", codebase);
364 info.AddValue ("_Version", Version);
365 info.AddValue ("_HashAlgorithm", hashalg);
366 info.AddValue ("_HashAlgorithmForControl", AssemblyHashAlgorithm.None);
367 info.AddValue ("_StrongNameKeyPair", keypair);
368 info.AddValue ("_VersionCompatibility", versioncompat);
369 info.AddValue ("_Flags", flags);
370 info.AddValue ("_HashForControl", null);
373 public object Clone()
375 AssemblyName an = new AssemblyName ();
377 an.codebase = codebase;
381 an.revision = revision;
382 an.version = version;
383 an.cultureinfo = cultureinfo;
385 an.hashalg = hashalg;
386 an.keypair = keypair;
387 an.publicKey = publicKey;
388 an.keyToken = keyToken;
389 an.versioncompat = versioncompat;
390 an.processor_architecture = processor_architecture;
394 public void OnDeserialization (object sender)
399 public static AssemblyName GetAssemblyName (string assemblyFile)
401 if (assemblyFile == null)
402 throw new ArgumentNullException ("assemblyFile");
404 AssemblyName aname = new AssemblyName ();
405 Assembly.InternalGetAssemblyName (Path.GetFullPath (assemblyFile), aname);
410 void _AssemblyName.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
412 throw new NotImplementedException ();
415 void _AssemblyName.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
417 throw new NotImplementedException ();
420 void _AssemblyName.GetTypeInfoCount (out uint pcTInfo)
422 throw new NotImplementedException ();
425 void _AssemblyName.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
426 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
428 throw new NotImplementedException ();
432 public string CultureName {
434 if (cultureinfo == null)
436 if (cultureinfo.LCID == CultureInfo.InvariantCulture.LCID)
438 return cultureinfo.Name;
442 [ComVisibleAttribute(false)]
443 public AssemblyContentType ContentType {