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 public AssemblyName ()
82 versioncompat = AssemblyVersionCompatibility.SameMachine;
85 [MethodImpl (MethodImplOptions.InternalCall)]
86 static extern bool ParseName (AssemblyName aname, string assemblyName);
88 public AssemblyName (string assemblyName)
90 if (assemblyName == null)
91 throw new ArgumentNullException ("assemblyName");
92 if (assemblyName.Length < 1)
93 throw new ArgumentException ("assemblyName cannot have zero length.");
95 if (!ParseName (this, assemblyName))
96 throw new FileLoadException ("The assembly name is invalid.");
99 [MonoLimitation ("Not used, as the values are too limited; Mono supports more")]
100 public ProcessorArchitecture ProcessorArchitecture {
102 return processor_architecture;
105 processor_architecture = value;
109 internal AssemblyName (SerializationInfo si, StreamingContext sc)
111 name = si.GetString ("_Name");
112 codebase = si.GetString ("_CodeBase");
113 version = (Version)si.GetValue ("_Version", typeof (Version));
114 publicKey = (byte[])si.GetValue ("_PublicKey", typeof (byte[]));
115 keyToken = (byte[])si.GetValue ("_PublicKeyToken", typeof (byte[]));
116 hashalg = (AssemblyHashAlgorithm)si.GetValue ("_HashAlgorithm", typeof (AssemblyHashAlgorithm));
117 keypair = (StrongNameKeyPair)si.GetValue ("_StrongNameKeyPair", typeof (StrongNameKeyPair));
118 versioncompat = (AssemblyVersionCompatibility)si.GetValue ("_VersionCompatibility", typeof (AssemblyVersionCompatibility));
119 flags = (AssemblyNameFlags)si.GetValue ("_Flags", typeof (AssemblyNameFlags));
120 int lcid = si.GetInt32 ("_CultureInfo");
121 if (lcid != -1) cultureinfo = new CultureInfo (lcid);
126 set { name = value; }
129 public string CodeBase {
130 get { return codebase; }
131 set { codebase = value; }
134 public string EscapedCodeBase {
136 if (codebase == null)
138 return Uri.EscapeString (codebase, false, true, true);
142 public CultureInfo CultureInfo {
143 get { return cultureinfo; }
144 set { cultureinfo = value; }
147 public AssemblyNameFlags Flags {
148 get { return flags; }
149 set { flags = value; }
152 public string FullName {
156 StringBuilder fname = new StringBuilder ();
157 if (Char.IsWhiteSpace (name [0]))
158 fname.Append ("\"" + name + "\"");
161 if (Version != null) {
162 fname.Append (", Version=");
163 fname.Append (Version.ToString ());
165 if (cultureinfo != null) {
166 fname.Append (", Culture=");
167 if (cultureinfo.LCID == CultureInfo.InvariantCulture.LCID)
168 fname.Append ("neutral");
170 fname.Append (cultureinfo.Name);
172 byte [] pub_tok = InternalGetPublicKeyToken ();
173 if (pub_tok != null) {
174 if (pub_tok.Length == 0)
175 fname.Append (", PublicKeyToken=null");
177 fname.Append (", PublicKeyToken=");
178 for (int i = 0; i < pub_tok.Length; i++)
179 fname.Append (pub_tok[i].ToString ("x2"));
183 if ((Flags & AssemblyNameFlags.Retargetable) != 0)
184 fname.Append (", Retargetable=Yes");
186 return fname.ToString ();
190 public AssemblyHashAlgorithm HashAlgorithm {
191 get { return hashalg; }
192 set { hashalg = value; }
195 public StrongNameKeyPair KeyPair {
196 get { return keypair; }
197 set { keypair = value; }
200 public Version Version {
208 major = minor = build = revision = 0;
213 revision = value.Revision;
218 public AssemblyVersionCompatibility VersionCompatibility {
219 get { return versioncompat; }
220 set { versioncompat = value; }
223 public override string ToString ()
225 string name = FullName;
226 return (name != null) ? name : base.ToString ();
229 public byte[] GetPublicKey()
234 public byte[] GetPublicKeyToken ()
236 if (keyToken != null)
238 if (publicKey == null)
241 if (publicKey.Length == 0)
242 return EmptyArray<byte>.Value;
244 if (!IsPublicKeyValid)
245 throw new SecurityException ("The public key is not valid.");
247 keyToken = ComputePublicKeyToken ();
251 private bool IsPublicKeyValid {
253 // check for ECMA key
254 if (publicKey.Length == 16) {
257 while (i < publicKey.Length)
258 sum += publicKey [i++];
263 switch (publicKey [0]) {
264 case 0x00: // public key inside a header
265 if (publicKey.Length > 12 && publicKey [12] == 0x06) {
270 CryptoConvert.FromCapiPublicKeyBlob (
273 } catch (CryptographicException) {
278 case 0x06: // public key
283 CryptoConvert.FromCapiPublicKeyBlob (publicKey);
285 } catch (CryptographicException) {
289 case 0x07: // private key
297 private byte [] InternalGetPublicKeyToken ()
299 if (keyToken != null)
302 if (publicKey == null)
305 if (publicKey.Length == 0)
306 return EmptyArray<byte>.Value;
308 if (!IsPublicKeyValid)
309 throw new SecurityException ("The public key is not valid.");
311 return ComputePublicKeyToken ();
314 [MethodImplAttribute (MethodImplOptions.InternalCall)]
315 extern unsafe static void get_public_token (byte* token, byte* pubkey, int len);
317 private unsafe byte [] ComputePublicKeyToken ()
319 byte [] token = new byte [8];
320 fixed (byte* pkt = token)
321 fixed (byte *pk = publicKey)
322 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");
333 if (reference.Name != definition.Name)
335 throw new NotImplementedException ();
338 public void SetPublicKey (byte[] publicKey)
340 if (publicKey == null)
341 flags ^= AssemblyNameFlags.PublicKey;
343 flags |= AssemblyNameFlags.PublicKey;
344 this.publicKey = publicKey;
347 public void SetPublicKeyToken (byte[] publicKeyToken)
349 keyToken = publicKeyToken;
352 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
353 public void GetObjectData (SerializationInfo info, StreamingContext context)
356 throw new ArgumentNullException ("info");
358 info.AddValue ("_Name", name);
359 info.AddValue ("_PublicKey", publicKey);
360 info.AddValue ("_PublicKeyToken", keyToken);
361 info.AddValue ("_CultureInfo", cultureinfo != null ? cultureinfo.LCID : -1);
362 info.AddValue ("_CodeBase", codebase);
363 info.AddValue ("_Version", Version);
364 info.AddValue ("_HashAlgorithm", hashalg);
365 info.AddValue ("_HashAlgorithmForControl", AssemblyHashAlgorithm.None);
366 info.AddValue ("_StrongNameKeyPair", keypair);
367 info.AddValue ("_VersionCompatibility", versioncompat);
368 info.AddValue ("_Flags", flags);
369 info.AddValue ("_HashForControl", null);
372 public object Clone()
374 AssemblyName an = new AssemblyName ();
376 an.codebase = codebase;
380 an.revision = revision;
381 an.version = version;
382 an.cultureinfo = cultureinfo;
384 an.hashalg = hashalg;
385 an.keypair = keypair;
386 an.publicKey = publicKey;
387 an.keyToken = keyToken;
388 an.versioncompat = versioncompat;
392 public void OnDeserialization (object sender)
397 public static AssemblyName GetAssemblyName (string assemblyFile)
399 if (assemblyFile == null)
400 throw new ArgumentNullException ("assemblyFile");
402 AssemblyName aname = new AssemblyName ();
403 Assembly.InternalGetAssemblyName (Path.GetFullPath (assemblyFile), aname);
408 void _AssemblyName.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
410 throw new NotImplementedException ();
413 void _AssemblyName.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
415 throw new NotImplementedException ();
418 void _AssemblyName.GetTypeInfoCount (out uint pcTInfo)
420 throw new NotImplementedException ();
423 void _AssemblyName.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
424 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
426 throw new NotImplementedException ();
431 public string CultureName {
433 if (cultureinfo == null)
435 else if (cultureinfo.LCID == CultureInfo.InvariantCulture.LCID)
438 return cultureinfo.Name;
442 [ComVisibleAttribute(false)]
443 public AssemblyContentType ContentType {
444 get { return AssemblyContentType.Default; }
446 if (value != AssemblyContentType.Default)
447 throw new InvalidOperationException ();