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
80 AssemblyContentType contentType;
82 public AssemblyName ()
85 versioncompat = AssemblyVersionCompatibility.SameMachine;
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.");
102 [MonoLimitation ("Not used, as the values are too limited; Mono supports more")]
103 public ProcessorArchitecture ProcessorArchitecture {
105 return processor_architecture;
108 processor_architecture = value;
112 internal AssemblyName (SerializationInfo si, StreamingContext sc)
114 name = si.GetString ("_Name");
115 codebase = si.GetString ("_CodeBase");
116 version = (Version)si.GetValue ("_Version", typeof (Version));
117 publicKey = (byte[])si.GetValue ("_PublicKey", typeof (byte[]));
118 keyToken = (byte[])si.GetValue ("_PublicKeyToken", typeof (byte[]));
119 hashalg = (AssemblyHashAlgorithm)si.GetValue ("_HashAlgorithm", typeof (AssemblyHashAlgorithm));
120 keypair = (StrongNameKeyPair)si.GetValue ("_StrongNameKeyPair", typeof (StrongNameKeyPair));
121 versioncompat = (AssemblyVersionCompatibility)si.GetValue ("_VersionCompatibility", typeof (AssemblyVersionCompatibility));
122 flags = (AssemblyNameFlags)si.GetValue ("_Flags", typeof (AssemblyNameFlags));
123 int lcid = si.GetInt32 ("_CultureInfo");
124 if (lcid != -1) cultureinfo = new CultureInfo (lcid);
129 set { name = value; }
132 public string CodeBase {
133 get { return codebase; }
134 set { codebase = value; }
137 public string EscapedCodeBase {
139 if (codebase == null)
141 return Uri.EscapeString (codebase, false, true, true);
145 public CultureInfo CultureInfo {
146 get { return cultureinfo; }
147 set { cultureinfo = value; }
150 public AssemblyNameFlags Flags {
151 get { return flags; }
152 set { flags = value; }
155 public string FullName {
159 StringBuilder fname = new StringBuilder ();
160 if (Char.IsWhiteSpace (name [0]))
161 fname.Append ("\"" + name + "\"");
164 if (Version != null) {
165 fname.Append (", Version=");
166 fname.Append (Version.ToString ());
168 if (cultureinfo != null) {
169 fname.Append (", Culture=");
170 if (cultureinfo.LCID == CultureInfo.InvariantCulture.LCID)
171 fname.Append ("neutral");
173 fname.Append (cultureinfo.Name);
175 byte [] pub_tok = InternalGetPublicKeyToken ();
176 if (pub_tok != null) {
177 if (pub_tok.Length == 0)
178 fname.Append (", PublicKeyToken=null");
180 fname.Append (", PublicKeyToken=");
181 for (int i = 0; i < pub_tok.Length; i++)
182 fname.Append (pub_tok[i].ToString ("x2"));
186 if ((Flags & AssemblyNameFlags.Retargetable) != 0)
187 fname.Append (", Retargetable=Yes");
189 return fname.ToString ();
193 public AssemblyHashAlgorithm HashAlgorithm {
194 get { return hashalg; }
195 set { hashalg = value; }
198 public StrongNameKeyPair KeyPair {
199 get { return keypair; }
200 set { keypair = value; }
203 public Version Version {
211 major = minor = build = revision = 0;
216 revision = value.Revision;
221 public AssemblyVersionCompatibility VersionCompatibility {
222 get { return versioncompat; }
223 set { versioncompat = value; }
226 public override string ToString ()
228 string name = FullName;
229 return (name != null) ? name : base.ToString ();
232 public byte[] GetPublicKey()
237 public byte[] GetPublicKeyToken ()
239 if (keyToken != null)
241 if (publicKey == null)
244 if (publicKey.Length == 0)
245 return EmptyArray<byte>.Value;
247 if (!IsPublicKeyValid)
248 throw new SecurityException ("The public key is not valid.");
250 keyToken = ComputePublicKeyToken ();
254 private bool IsPublicKeyValid {
256 // check for ECMA key
257 if (publicKey.Length == 16) {
260 while (i < publicKey.Length)
261 sum += publicKey [i++];
266 switch (publicKey [0]) {
267 case 0x00: // public key inside a header
268 if (publicKey.Length > 12 && publicKey [12] == 0x06) {
273 CryptoConvert.FromCapiPublicKeyBlob (
276 } catch (CryptographicException) {
281 case 0x06: // public key
286 CryptoConvert.FromCapiPublicKeyBlob (publicKey);
288 } catch (CryptographicException) {
292 case 0x07: // private key
300 private byte [] InternalGetPublicKeyToken ()
302 if (keyToken != null)
305 if (publicKey == null)
308 if (publicKey.Length == 0)
309 return EmptyArray<byte>.Value;
311 if (!IsPublicKeyValid)
312 throw new SecurityException ("The public key is not valid.");
314 return ComputePublicKeyToken ();
317 [MethodImplAttribute (MethodImplOptions.InternalCall)]
318 extern unsafe static void get_public_token (byte* token, byte* pubkey, int len);
320 private unsafe byte [] ComputePublicKeyToken ()
322 byte [] token = new byte [8];
323 fixed (byte* pkt = token)
324 fixed (byte *pk = publicKey)
325 get_public_token (pkt, pk, publicKey.Length);
329 public static bool ReferenceMatchesDefinition (AssemblyName reference, AssemblyName definition)
331 if (reference == null)
332 throw new ArgumentNullException ("reference");
333 if (definition == null)
334 throw new ArgumentNullException ("definition");
336 // we only compare the simple assembly name to be consistent with MS .NET,
337 // which is the result of a bug in their implementation (see https://connect.microsoft.com/VisualStudio/feedback/details/752902)
338 return string.Equals (reference.Name, definition.Name, StringComparison.OrdinalIgnoreCase);
341 public void SetPublicKey (byte[] publicKey)
343 if (publicKey == null)
344 flags ^= AssemblyNameFlags.PublicKey;
346 flags |= AssemblyNameFlags.PublicKey;
347 this.publicKey = publicKey;
350 public void SetPublicKeyToken (byte[] publicKeyToken)
352 keyToken = publicKeyToken;
355 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
356 public void GetObjectData (SerializationInfo info, StreamingContext context)
359 throw new ArgumentNullException ("info");
361 info.AddValue ("_Name", name);
362 info.AddValue ("_PublicKey", publicKey);
363 info.AddValue ("_PublicKeyToken", keyToken);
364 info.AddValue ("_CultureInfo", cultureinfo != null ? cultureinfo.LCID : -1);
365 info.AddValue ("_CodeBase", codebase);
366 info.AddValue ("_Version", Version);
367 info.AddValue ("_HashAlgorithm", hashalg);
368 info.AddValue ("_HashAlgorithmForControl", AssemblyHashAlgorithm.None);
369 info.AddValue ("_StrongNameKeyPair", keypair);
370 info.AddValue ("_VersionCompatibility", versioncompat);
371 info.AddValue ("_Flags", flags);
372 info.AddValue ("_HashForControl", null);
375 public object Clone()
377 AssemblyName an = new AssemblyName ();
379 an.codebase = codebase;
383 an.revision = revision;
384 an.version = version;
385 an.cultureinfo = cultureinfo;
387 an.hashalg = hashalg;
388 an.keypair = keypair;
389 an.publicKey = publicKey;
390 an.keyToken = keyToken;
391 an.versioncompat = versioncompat;
392 an.processor_architecture = processor_architecture;
396 public void OnDeserialization (object sender)
401 public static AssemblyName GetAssemblyName (string assemblyFile)
403 if (assemblyFile == null)
404 throw new ArgumentNullException ("assemblyFile");
406 AssemblyName aname = new AssemblyName ();
407 Assembly.InternalGetAssemblyName (Path.GetFullPath (assemblyFile), aname);
412 void _AssemblyName.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
414 throw new NotImplementedException ();
417 void _AssemblyName.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
419 throw new NotImplementedException ();
422 void _AssemblyName.GetTypeInfoCount (out uint pcTInfo)
424 throw new NotImplementedException ();
427 void _AssemblyName.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
428 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
430 throw new NotImplementedException ();
435 public string CultureName {
437 if (cultureinfo == null)
439 if (cultureinfo.LCID == CultureInfo.InvariantCulture.LCID)
441 return cultureinfo.Name;
445 [ComVisibleAttribute(false)]
446 public AssemblyContentType ContentType {