2003-10-17 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System.Reflection / AssemblyName.cs
1 //
2 // System.Reflection/AssemblyName.cs
3 //
4 // Authors:
5 //      Paolo Molaro (lupus@ximian.com)
6 //      Sebastien Pouliot (spouliot@motus.com)
7 //
8 // (C) 2001 Ximian, Inc.  http://www.ximian.com
9 // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
10 //
11
12 using System;
13 using System.Configuration.Assemblies;
14 using System.Globalization;
15 using System.Reflection;
16 using System.Runtime.Serialization;
17 using System.Security.Cryptography;
18 using System.Text;
19 using System.Runtime.InteropServices;
20
21 namespace System.Reflection {
22
23 // References:
24 // a.   Uniform Resource Identifiers (URI): Generic Syntax
25 //      http://www.ietf.org/rfc/rfc2396.txt
26
27         [Serializable]
28         public sealed class AssemblyName  : ICloneable, ISerializable, IDeserializationCallback {
29                 string name;
30                 string codebase;
31                 int major, minor, build, revision;
32                 CultureInfo cultureinfo;
33                 AssemblyNameFlags flags;
34                 AssemblyHashAlgorithm hashalg;
35                 StrongNameKeyPair keypair;
36                 byte[] publicKey;
37                 byte[] keyToken;
38                 AssemblyVersionCompatibility versioncompat;
39                 
40                 public AssemblyName ()
41                 {
42                         // defaults
43                         versioncompat = AssemblyVersionCompatibility.SameMachine;
44                 }
45
46                 internal AssemblyName (SerializationInfo si, StreamingContext sc)
47                 {
48                         name = si.GetString ("_Name");
49                         codebase = si.GetString ("_CodeBase");
50                         Version = (Version)si.GetValue ("_Version", typeof (Version));
51                 }
52
53                 public string Name {
54                         get { return name; }
55                         set { name = value; }
56                 }
57
58                 public string CodeBase {
59                         get { return codebase; }
60                         set { codebase = value; }
61                 }
62
63                 [MonoTODO("RFC 2396")]
64                 private string Escape (string url) 
65                 {
66                         // we already have code in mcs\class\System\System\Uri.cs
67                         // but Uri class ins't part of corlib !
68                         // TODO
69                         return url;
70                 }
71
72                 public string EscapedCodeBase {
73                         get { return Escape (codebase); }
74                 }
75
76                 public CultureInfo CultureInfo {
77                         get { return cultureinfo; }
78                         set { cultureinfo = value; }
79                 }
80
81                 public AssemblyNameFlags Flags {
82                         get { return flags; }
83                         set { flags = value; }
84                 }
85
86                 [MonoTODO("incomplete")]
87                 public string FullName {
88                         get {
89                                 if (name == null)
90                                         return null;
91                                 StringBuilder fname = new StringBuilder ();
92                                 fname.Append (name);
93                                 fname.Append (", Version=");
94                                 fname.Append (Version.ToString ());
95                                 fname.Append (", Culture=");
96                                 if ((cultureinfo == null) || (cultureinfo.LCID == CultureInfo.InvariantCulture.LCID))
97                                         fname.Append ("neutral");
98                                 else
99                                         fname.Append (cultureinfo.ToString ()); // ???
100                                 if (keypair == null)
101                                         fname.Append (", PublicKeyToken=null");
102                                 // TODO
103                                 return fname.ToString ();
104                         }
105                 }
106
107                 public AssemblyHashAlgorithm HashAlgorithm {
108                         get { return hashalg; }
109                         set { hashalg = value; }
110                 }
111
112                 public StrongNameKeyPair KeyPair {
113                         get { return keypair; }
114                         set { keypair = value; }
115                 }
116
117                 public Version Version {
118                         get {
119                                 if (name == null)
120                                         return null;
121                                 if (build == -1)
122                                         return new Version (major, minor);
123                                 else
124                                         if (revision == -1)
125                                                 return new Version (major, minor, build);
126                                 else
127                                         return new Version (major, minor, build, revision);
128                         }
129
130                         set {
131                                 major = value.Major;
132                                 minor = value.Minor;
133                                 build = value.Build;
134                                 revision = value.Revision;
135                         }
136                 }
137
138                 public AssemblyVersionCompatibility VersionCompatibility {
139                         get { return versioncompat; }
140                         set { versioncompat = value; }
141                 }
142                 
143                 public override string ToString ()
144                 {
145                         string name = FullName;
146                         return (name != null) ? name : base.ToString ();
147                 }
148
149                 public byte[] GetPublicKey() 
150                 {
151                         // to match MS implementation -- funny one
152                         if (publicKey != null)
153                                 return publicKey;
154                         else if (name == null)
155                                 return null;
156                         else
157                                 return new byte [0];
158                 }
159
160                 public byte[] GetPublicKeyToken() 
161                 {
162                         if (keyToken != null)
163                                 return keyToken;
164                         else if (publicKey == null)
165                                 return null;
166                         else {
167                                 HashAlgorithm ha = null;
168                                 switch (hashalg) {
169                                         case AssemblyHashAlgorithm.MD5:
170                                                 ha = MD5.Create ();
171                                                 break;
172                                         default:
173                                                 // None default to SHA1
174                                                 ha = SHA1.Create ();
175                                                 break;
176                                 }
177                                 byte[] hash = ha.ComputeHash (publicKey);
178                                 // we need the last 8 bytes in reverse order
179                                 keyToken = new byte [8];
180                                 Array.Copy (hash, (hash.Length - 8), keyToken, 0, 8);
181                                 Array.Reverse (keyToken, 0, 8);
182                                 return keyToken;
183                         }
184                 }
185
186                 public void SetPublicKey (byte[] publicKey) 
187                 {
188                         flags = AssemblyNameFlags.PublicKey;
189                         this.publicKey = publicKey;
190                 }
191
192                 public void SetPublicKeyToken (byte[] publicKeyToken) 
193                 {
194                         keyToken = publicKeyToken;
195                 }
196
197                 public void GetObjectData (SerializationInfo info, StreamingContext context)
198                 {
199                         info.AddValue ("_Name", name);
200                         info.AddValue ("_CodeBase", codebase);
201                         info.AddValue ("_Version", Version);
202                 }
203
204                 // required to implement ICloneable
205                 [MonoTODO()]
206                 public object Clone() 
207                 {
208                         return null;
209                 }
210
211                 // required to implement IDeserializationCallback
212                 [MonoTODO()]
213                 public void OnDeserialization (object sender) 
214                 {
215                 }
216
217                 public static AssemblyName GetAssemblyName (string assemblyFile) 
218                 {
219                         Assembly a = Assembly.LoadFrom (assemblyFile);
220                         return a.GetName ();
221                 }
222         }
223 }