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 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.
33 using System
.Configuration
.Assemblies
;
34 using System
.Globalization
;
35 using System
.Reflection
;
36 using System
.Runtime
.Serialization
;
37 using System
.Security
.Cryptography
;
39 using System
.Runtime
.InteropServices
;
40 using System
.Runtime
.CompilerServices
;
42 namespace System
.Reflection
{
45 // a. Uniform Resource Identifiers (URI): Generic Syntax
46 // http://www.ietf.org/rfc/rfc2396.txt
49 [MonoTODO ("Fix serialization compatibility with MS.NET")]
50 public sealed class AssemblyName
: ICloneable
, ISerializable
, IDeserializationCallback
{
53 int major
, minor
, build
, revision
;
54 CultureInfo cultureinfo
;
55 AssemblyNameFlags flags
;
56 AssemblyHashAlgorithm hashalg
;
57 StrongNameKeyPair keypair
;
60 AssemblyVersionCompatibility versioncompat
;
63 public AssemblyName ()
66 versioncompat
= AssemblyVersionCompatibility
.SameMachine
;
70 public AssemblyName (string assemblyName
)
76 internal AssemblyName (SerializationInfo si
, StreamingContext sc
)
78 name
= si
.GetString ("_Name");
79 codebase
= si
.GetString ("_CodeBase");
80 version
= (Version
)si
.GetValue ("_Version", typeof (Version
));
81 publicKey
= (byte[])si
.GetValue ("_PublicKey", typeof (byte[]));
82 keyToken
= (byte[])si
.GetValue ("_PublicToken", typeof (byte[]));
83 hashalg
= (AssemblyHashAlgorithm
)si
.GetValue ("_HashAlgorithm", typeof (AssemblyHashAlgorithm
));
84 keypair
= (StrongNameKeyPair
)si
.GetValue ("_StrongNameKeyPair", typeof (StrongNameKeyPair
));
85 versioncompat
= (AssemblyVersionCompatibility
)si
.GetValue ("_VersionCompatibility", typeof (AssemblyVersionCompatibility
));
86 flags
= (AssemblyNameFlags
)si
.GetValue ("_Flags", typeof (AssemblyNameFlags
));
87 int lcid
= si
.GetInt32 ("_CultureInfo");
88 if (lcid
!= -1) cultureinfo
= new CultureInfo (lcid
);
96 public string CodeBase
{
97 get { return codebase; }
98 set { codebase = value; }
101 [MonoTODO("RFC 2396")]
102 private string Escape (string url
)
104 // we already have code in mcs\class\System\System\Uri.cs
105 // but Uri class ins't part of corlib !
110 public string EscapedCodeBase
{
111 get { return Escape (codebase); }
114 public CultureInfo CultureInfo
{
115 get { return cultureinfo; }
116 set { cultureinfo = value; }
119 public AssemblyNameFlags Flags
{
120 get { return flags; }
121 set { flags = value; }
124 public string FullName
{
128 StringBuilder fname
= new StringBuilder ();
130 if (Version
.ToString () != "0.0.0.0") {
131 fname
.Append (", Version=");
132 fname
.Append (Version
.ToString ());
134 if (cultureinfo
!= null) {
135 fname
.Append (", Culture=");
136 if (cultureinfo
.LCID
== CultureInfo
.InvariantCulture
.LCID
)
137 fname
.Append ("neutral");
139 fname
.Append (cultureinfo
.Name
);
141 byte[] pub_tok
= GetPublicKeyToken ();
142 if (pub_tok
!= null) {
143 if (pub_tok
.Length
== 0)
144 fname
.Append (", PublicKeyToken=null");
146 fname
.Append (", PublicKeyToken=");
147 for (int i
= 0; i
< pub_tok
.Length
; i
++)
148 fname
.Append (pub_tok
[i
].ToString ("x2"));
151 return fname
.ToString ();
155 public AssemblyHashAlgorithm HashAlgorithm
{
156 get { return hashalg; }
157 set { hashalg = value; }
160 public StrongNameKeyPair KeyPair
{
161 get { return keypair; }
162 set { keypair = value; }
165 public Version Version
{
167 if (version
!= null) return version
;
172 version
= new Version (major
, minor
);
175 version
= new Version (major
, minor
, build
);
177 version
= new Version (major
, minor
, build
, revision
);
186 revision
= value.Revision
;
191 public AssemblyVersionCompatibility VersionCompatibility
{
192 get { return versioncompat; }
193 set { versioncompat = value; }
196 public override string ToString ()
198 string name
= FullName
;
199 return (name
!= null) ? name
: base.ToString ();
202 public byte[] GetPublicKey()
204 // to match MS implementation -- funny one
205 if (publicKey
!= null)
207 else if (name
== null)
213 public byte[] GetPublicKeyToken()
215 if (keyToken
!= null)
217 else if (publicKey
== null)
220 HashAlgorithm ha
= null;
222 case AssemblyHashAlgorithm
.MD5
:
226 // None default to SHA1
230 byte[] hash
= ha
.ComputeHash (publicKey
);
231 // we need the last 8 bytes in reverse order
232 keyToken
= new byte [8];
233 Array
.Copy (hash
, (hash
.Length
- 8), keyToken
, 0, 8);
234 Array
.Reverse (keyToken
, 0, 8);
239 public void SetPublicKey (byte[] publicKey
)
241 flags
= AssemblyNameFlags
.PublicKey
;
242 this.publicKey
= publicKey
;
245 public void SetPublicKeyToken (byte[] publicKeyToken
)
247 keyToken
= publicKeyToken
;
250 public void GetObjectData (SerializationInfo info
, StreamingContext context
)
252 info
.AddValue ("_Name", name
);
253 info
.AddValue ("_PublicKey", publicKey
);
254 info
.AddValue ("_PublicToken", keyToken
);
255 info
.AddValue ("_CultureInfo", cultureinfo
!= null ? cultureinfo
.LCID
: -1);
256 info
.AddValue ("_CodeBase", codebase
);
257 info
.AddValue ("_Version", Version
);
258 info
.AddValue ("_HashAlgorithm", hashalg
);
259 info
.AddValue ("_HashAlgorithmForControl", AssemblyHashAlgorithm
.None
);
260 info
.AddValue ("_StrongNameKeyPair", keypair
);
261 info
.AddValue ("_VersionCompatibility", versioncompat
);
262 info
.AddValue ("_Flags", flags
);
263 info
.AddValue ("_HashForControl", null);
266 public object Clone()
268 AssemblyName an
= new AssemblyName ();
270 an
.codebase
= codebase
;
274 an
.revision
= revision
;
275 an
.cultureinfo
= cultureinfo
;
277 an
.hashalg
= hashalg
;
278 an
.keypair
= keypair
;
279 an
.publicKey
= publicKey
;
280 an
.keyToken
= keyToken
;
281 an
.versioncompat
= versioncompat
;
285 public void OnDeserialization (object sender
)
290 public static AssemblyName
GetAssemblyName (string assemblyFile
)
292 if (assemblyFile
== null)
293 throw new ArgumentNullException ("assemblyFile");
295 AssemblyName aname
= new AssemblyName ();
296 Assembly
.InternalGetAssemblyName (System
.IO
.Path
.GetFullPath (assemblyFile
), aname
);