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.Cryptography;
36 using System.Security.Permissions;
38 using System.Runtime.InteropServices;
39 using System.Runtime.CompilerServices;
44 namespace System.Reflection {
47 // a. Uniform Resource Identifiers (URI): Generic Syntax
48 // http://www.ietf.org/rfc/rfc2396.txt
52 [ComDefaultInterfaceAttribute (typeof (_AssemblyName))]
55 [ClassInterfaceAttribute (ClassInterfaceType.None)]
56 [MonoTODO ("Fix serialization compatibility with MS.NET")]
57 public sealed class AssemblyName : ICloneable, ISerializable, IDeserializationCallback, _AssemblyName {
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;
72 ProcessorArchitecture processor_architecture;
74 int processor_architecture;
78 public AssemblyName ()
81 versioncompat = AssemblyVersionCompatibility.SameMachine;
84 #if NET_2_0 || BOOTSTRAP_NET_2_0
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.");
102 public ProcessorArchitecture ProcessorArchitecture {
104 return processor_architecture;
107 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 ();
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 = GetPublicKeyToken ();
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"));
182 return fname.ToString ();
186 public AssemblyHashAlgorithm HashAlgorithm {
187 get { return hashalg; }
188 set { hashalg = value; }
191 public StrongNameKeyPair KeyPair {
192 get { return keypair; }
193 set { keypair = value; }
196 public Version Version {
204 major = minor = build = revision = 0;
209 revision = value.Revision;
214 public AssemblyVersionCompatibility VersionCompatibility {
215 get { return versioncompat; }
216 set { versioncompat = value; }
219 public override string ToString ()
221 string name = FullName;
222 return (name != null) ? name : base.ToString ();
225 public byte[] GetPublicKey()
228 // FIXME: In some cases MS implementation returns
229 // "new byte [0]" instead of null
232 public byte[] GetPublicKeyToken()
234 if (keyToken != null)
236 else if (publicKey == null)
239 HashAlgorithm ha = null;
241 case AssemblyHashAlgorithm.MD5:
245 // None default to SHA1
249 byte[] hash = ha.ComputeHash (publicKey);
250 // we need the last 8 bytes in reverse order
251 keyToken = new byte [8];
252 Array.Copy (hash, (hash.Length - 8), keyToken, 0, 8);
253 Array.Reverse (keyToken, 0, 8);
258 public void SetPublicKey (byte[] publicKey)
260 flags = AssemblyNameFlags.PublicKey;
261 this.publicKey = publicKey;
264 public void SetPublicKeyToken (byte[] publicKeyToken)
266 keyToken = publicKeyToken;
269 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
270 public void GetObjectData (SerializationInfo info, StreamingContext context)
273 throw new ArgumentNullException ("info");
275 info.AddValue ("_Name", name);
276 info.AddValue ("_PublicKey", publicKey);
277 info.AddValue ("_PublicKeyToken", keyToken);
278 info.AddValue ("_CultureInfo", cultureinfo != null ? cultureinfo.LCID : -1);
279 info.AddValue ("_CodeBase", codebase);
280 info.AddValue ("_Version", Version);
281 info.AddValue ("_HashAlgorithm", hashalg);
282 info.AddValue ("_HashAlgorithmForControl", AssemblyHashAlgorithm.None);
283 info.AddValue ("_StrongNameKeyPair", keypair);
284 info.AddValue ("_VersionCompatibility", versioncompat);
285 info.AddValue ("_Flags", flags);
286 info.AddValue ("_HashForControl", null);
289 public object Clone()
291 AssemblyName an = new AssemblyName ();
293 an.codebase = codebase;
297 an.revision = revision;
298 an.version = version;
299 an.cultureinfo = cultureinfo;
301 an.hashalg = hashalg;
302 an.keypair = keypair;
303 an.publicKey = publicKey;
304 an.keyToken = keyToken;
305 an.versioncompat = versioncompat;
309 public void OnDeserialization (object sender)
314 public static AssemblyName GetAssemblyName (string assemblyFile)
316 if (assemblyFile == null)
317 throw new ArgumentNullException ("assemblyFile");
319 AssemblyName aname = new AssemblyName ();
320 Assembly.InternalGetAssemblyName (Path.GetFullPath (assemblyFile), aname);
325 void _AssemblyName.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
327 throw new NotImplementedException ();
330 void _AssemblyName.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
332 throw new NotImplementedException ();
335 void _AssemblyName.GetTypeInfoCount (out uint pcTInfo)
337 throw new NotImplementedException ();
340 void _AssemblyName.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
341 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
343 throw new NotImplementedException ();