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;
43 namespace System.Reflection {
46 // a. Uniform Resource Identifiers (URI): Generic Syntax
47 // http://www.ietf.org/rfc/rfc2396.txt
51 [ComDefaultInterfaceAttribute (typeof (_AssemblyName))]
54 [ClassInterfaceAttribute (ClassInterfaceType.None)]
55 [MonoTODO ("Fix serialization compatibility with MS.NET")]
56 public sealed class AssemblyName : ICloneable, ISerializable, IDeserializationCallback, _AssemblyName {
58 #region Synch with object-internals.h
61 int major, minor, build, revision;
62 CultureInfo cultureinfo;
63 AssemblyNameFlags flags;
64 AssemblyHashAlgorithm hashalg;
65 StrongNameKeyPair keypair;
68 AssemblyVersionCompatibility versioncompat;
71 ProcessorArchitecture processor_architecture;
73 int processor_architecture;
77 public AssemblyName ()
80 versioncompat = AssemblyVersionCompatibility.SameMachine;
84 public AssemblyName (string assemblyName)
90 public ProcessorArchitecture ProcessorArchitecture {
92 return processor_architecture;
95 processor_architecture = value;
100 internal AssemblyName (SerializationInfo si, StreamingContext sc)
102 name = si.GetString ("_Name");
103 codebase = si.GetString ("_CodeBase");
104 version = (Version)si.GetValue ("_Version", typeof (Version));
105 publicKey = (byte[])si.GetValue ("_PublicKey", typeof (byte[]));
106 keyToken = (byte[])si.GetValue ("_PublicKeyToken", typeof (byte[]));
107 hashalg = (AssemblyHashAlgorithm)si.GetValue ("_HashAlgorithm", typeof (AssemblyHashAlgorithm));
108 keypair = (StrongNameKeyPair)si.GetValue ("_StrongNameKeyPair", typeof (StrongNameKeyPair));
109 versioncompat = (AssemblyVersionCompatibility)si.GetValue ("_VersionCompatibility", typeof (AssemblyVersionCompatibility));
110 flags = (AssemblyNameFlags)si.GetValue ("_Flags", typeof (AssemblyNameFlags));
111 int lcid = si.GetInt32 ("_CultureInfo");
112 if (lcid != -1) cultureinfo = new CultureInfo (lcid);
117 set { name = value; }
120 public string CodeBase {
121 get { return codebase; }
122 set { codebase = value; }
125 public string EscapedCodeBase {
127 if (codebase == null)
129 return Uri.EscapeString (codebase, false, true, true);
133 public CultureInfo CultureInfo {
134 get { return cultureinfo; }
135 set { cultureinfo = value; }
138 public AssemblyNameFlags Flags {
139 get { return flags; }
140 set { flags = value; }
143 public string FullName {
147 StringBuilder fname = new StringBuilder ();
149 if (Version != null) {
150 fname.Append (", Version=");
151 fname.Append (Version.ToString ());
153 if (cultureinfo != null) {
154 fname.Append (", Culture=");
155 if (cultureinfo.LCID == CultureInfo.InvariantCulture.LCID)
156 fname.Append ("neutral");
158 fname.Append (cultureinfo.Name);
160 byte[] pub_tok = GetPublicKeyToken ();
161 if (pub_tok != null) {
162 if (pub_tok.Length == 0)
163 fname.Append (", PublicKeyToken=null");
165 fname.Append (", PublicKeyToken=");
166 for (int i = 0; i < pub_tok.Length; i++)
167 fname.Append (pub_tok[i].ToString ("x2"));
170 return fname.ToString ();
174 public AssemblyHashAlgorithm HashAlgorithm {
175 get { return hashalg; }
176 set { hashalg = value; }
179 public StrongNameKeyPair KeyPair {
180 get { return keypair; }
181 set { keypair = value; }
184 public Version Version {
192 major = minor = build = revision = 0;
197 revision = value.Revision;
202 public AssemblyVersionCompatibility VersionCompatibility {
203 get { return versioncompat; }
204 set { versioncompat = value; }
207 public override string ToString ()
209 string name = FullName;
210 return (name != null) ? name : base.ToString ();
213 public byte[] GetPublicKey()
216 // FIXME: In some cases MS implementation returns
217 // "new byte [0]" instead of null
220 public byte[] GetPublicKeyToken()
222 if (keyToken != null)
224 else if (publicKey == null)
227 HashAlgorithm ha = null;
229 case AssemblyHashAlgorithm.MD5:
233 // None default to SHA1
237 byte[] hash = ha.ComputeHash (publicKey);
238 // we need the last 8 bytes in reverse order
239 keyToken = new byte [8];
240 Array.Copy (hash, (hash.Length - 8), keyToken, 0, 8);
241 Array.Reverse (keyToken, 0, 8);
246 public void SetPublicKey (byte[] publicKey)
248 flags = AssemblyNameFlags.PublicKey;
249 this.publicKey = publicKey;
252 public void SetPublicKeyToken (byte[] publicKeyToken)
254 keyToken = publicKeyToken;
257 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
258 public void GetObjectData (SerializationInfo info, StreamingContext context)
261 throw new ArgumentNullException ("info");
263 info.AddValue ("_Name", name);
264 info.AddValue ("_PublicKey", publicKey);
265 info.AddValue ("_PublicKeyToken", keyToken);
266 info.AddValue ("_CultureInfo", cultureinfo != null ? cultureinfo.LCID : -1);
267 info.AddValue ("_CodeBase", codebase);
268 info.AddValue ("_Version", Version);
269 info.AddValue ("_HashAlgorithm", hashalg);
270 info.AddValue ("_HashAlgorithmForControl", AssemblyHashAlgorithm.None);
271 info.AddValue ("_StrongNameKeyPair", keypair);
272 info.AddValue ("_VersionCompatibility", versioncompat);
273 info.AddValue ("_Flags", flags);
274 info.AddValue ("_HashForControl", null);
277 public object Clone()
279 AssemblyName an = new AssemblyName ();
281 an.codebase = codebase;
285 an.revision = revision;
286 an.version = version;
287 an.cultureinfo = cultureinfo;
289 an.hashalg = hashalg;
290 an.keypair = keypair;
291 an.publicKey = publicKey;
292 an.keyToken = keyToken;
293 an.versioncompat = versioncompat;
297 public void OnDeserialization (object sender)
302 public static AssemblyName GetAssemblyName (string assemblyFile)
304 if (assemblyFile == null)
305 throw new ArgumentNullException ("assemblyFile");
307 AssemblyName aname = new AssemblyName ();
308 Assembly.InternalGetAssemblyName (System.IO.Path.GetFullPath (assemblyFile), aname);
313 void _AssemblyName.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
315 throw new NotImplementedException ();
318 void _AssemblyName.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
320 throw new NotImplementedException ();
323 void _AssemblyName.GetTypeInfoCount (out uint pcTInfo)
325 throw new NotImplementedException ();
328 void _AssemblyName.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
329 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
331 throw new NotImplementedException ();