AssemblyName.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //
  2. // System.Reflection/AssemblyName.cs
  3. //
  4. // Authors:
  5. // Paolo Molaro ([email protected])
  6. // Sebastien Pouliot ([email protected])
  7. //
  8. // (C) 2001 Ximian, Inc. http://www.ximian.com
  9. // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  10. //
  11. //
  12. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System;
  34. using System.Configuration.Assemblies;
  35. using System.Globalization;
  36. using System.Reflection;
  37. using System.Runtime.Serialization;
  38. using System.Security.Cryptography;
  39. using System.Text;
  40. using System.Runtime.InteropServices;
  41. using System.Runtime.CompilerServices;
  42. namespace System.Reflection {
  43. // References:
  44. // a. Uniform Resource Identifiers (URI): Generic Syntax
  45. // http://www.ietf.org/rfc/rfc2396.txt
  46. [Serializable]
  47. [MonoTODO ("Fix serialization compatibility with MS.NET")]
  48. public sealed class AssemblyName : ICloneable, ISerializable, IDeserializationCallback {
  49. string name;
  50. string codebase;
  51. int major, minor, build, revision;
  52. CultureInfo cultureinfo;
  53. AssemblyNameFlags flags;
  54. AssemblyHashAlgorithm hashalg;
  55. StrongNameKeyPair keypair;
  56. byte[] publicKey;
  57. byte[] keyToken;
  58. AssemblyVersionCompatibility versioncompat;
  59. Version version;
  60. public AssemblyName ()
  61. {
  62. // defaults
  63. versioncompat = AssemblyVersionCompatibility.SameMachine;
  64. }
  65. internal AssemblyName (SerializationInfo si, StreamingContext sc)
  66. {
  67. name = si.GetString ("_Name");
  68. codebase = si.GetString ("_CodeBase");
  69. version = (Version)si.GetValue ("_Version", typeof (Version));
  70. publicKey = (byte[])si.GetValue ("_PublicKey", typeof (byte[]));
  71. keyToken = (byte[])si.GetValue ("_PublicToken", typeof (byte[]));
  72. hashalg = (AssemblyHashAlgorithm)si.GetValue ("_HashAlgorithm", typeof (AssemblyHashAlgorithm));
  73. keypair = (StrongNameKeyPair)si.GetValue ("_StrongNameKeyPair", typeof (StrongNameKeyPair));
  74. versioncompat = (AssemblyVersionCompatibility)si.GetValue ("_VersionCompatibility", typeof (AssemblyVersionCompatibility));
  75. flags = (AssemblyNameFlags)si.GetValue ("_Flags", typeof (AssemblyNameFlags));
  76. int lcid = si.GetInt32 ("_CultureInfo");
  77. if (lcid != -1) cultureinfo = new CultureInfo (lcid);
  78. }
  79. public string Name {
  80. get { return name; }
  81. set { name = value; }
  82. }
  83. public string CodeBase {
  84. get { return codebase; }
  85. set { codebase = value; }
  86. }
  87. [MonoTODO("RFC 2396")]
  88. private string Escape (string url)
  89. {
  90. // we already have code in mcs\class\System\System\Uri.cs
  91. // but Uri class ins't part of corlib !
  92. // TODO
  93. return url;
  94. }
  95. public string EscapedCodeBase {
  96. get { return Escape (codebase); }
  97. }
  98. public CultureInfo CultureInfo {
  99. get { return cultureinfo; }
  100. set { cultureinfo = value; }
  101. }
  102. public AssemblyNameFlags Flags {
  103. get { return flags; }
  104. set { flags = value; }
  105. }
  106. public string FullName {
  107. get {
  108. if (name == null)
  109. return null;
  110. StringBuilder fname = new StringBuilder ();
  111. fname.Append (name);
  112. fname.Append (", Version=");
  113. fname.Append (Version.ToString ());
  114. fname.Append (", Culture=");
  115. if ((cultureinfo == null) || (cultureinfo.LCID == CultureInfo.InvariantCulture.LCID))
  116. fname.Append ("neutral");
  117. else
  118. fname.Append (cultureinfo.Name);
  119. byte[] pub_tok = GetPublicKeyToken ();
  120. if (pub_tok == null || pub_tok.Length == 0)
  121. fname.Append (", PublicKeyToken=null");
  122. else {
  123. fname.Append (", PublicKeyToken=");
  124. for (int i = 0; i < pub_tok.Length; i++)
  125. fname.Append (pub_tok[i].ToString ("x2"));
  126. }
  127. return fname.ToString ();
  128. }
  129. }
  130. public AssemblyHashAlgorithm HashAlgorithm {
  131. get { return hashalg; }
  132. set { hashalg = value; }
  133. }
  134. public StrongNameKeyPair KeyPair {
  135. get { return keypair; }
  136. set { keypair = value; }
  137. }
  138. public Version Version {
  139. get {
  140. if (version != null) return version;
  141. if (name == null)
  142. return null;
  143. if (build == -1)
  144. version = new Version (major, minor);
  145. else
  146. if (revision == -1)
  147. version = new Version (major, minor, build);
  148. else
  149. version = new Version (major, minor, build, revision);
  150. return version;
  151. }
  152. set {
  153. major = value.Major;
  154. minor = value.Minor;
  155. build = value.Build;
  156. revision = value.Revision;
  157. version = value;
  158. }
  159. }
  160. public AssemblyVersionCompatibility VersionCompatibility {
  161. get { return versioncompat; }
  162. set { versioncompat = value; }
  163. }
  164. public override string ToString ()
  165. {
  166. string name = FullName;
  167. return (name != null) ? name : base.ToString ();
  168. }
  169. public byte[] GetPublicKey()
  170. {
  171. // to match MS implementation -- funny one
  172. if (publicKey != null)
  173. return publicKey;
  174. else if (name == null)
  175. return null;
  176. else
  177. return new byte [0];
  178. }
  179. public byte[] GetPublicKeyToken()
  180. {
  181. if (keyToken != null)
  182. return keyToken;
  183. else if (publicKey == null)
  184. return null;
  185. else {
  186. HashAlgorithm ha = null;
  187. switch (hashalg) {
  188. case AssemblyHashAlgorithm.MD5:
  189. ha = MD5.Create ();
  190. break;
  191. default:
  192. // None default to SHA1
  193. ha = SHA1.Create ();
  194. break;
  195. }
  196. byte[] hash = ha.ComputeHash (publicKey);
  197. // we need the last 8 bytes in reverse order
  198. keyToken = new byte [8];
  199. Array.Copy (hash, (hash.Length - 8), keyToken, 0, 8);
  200. Array.Reverse (keyToken, 0, 8);
  201. return keyToken;
  202. }
  203. }
  204. public void SetPublicKey (byte[] publicKey)
  205. {
  206. flags = AssemblyNameFlags.PublicKey;
  207. this.publicKey = publicKey;
  208. }
  209. public void SetPublicKeyToken (byte[] publicKeyToken)
  210. {
  211. keyToken = publicKeyToken;
  212. }
  213. public void GetObjectData (SerializationInfo info, StreamingContext context)
  214. {
  215. info.AddValue ("_Name", name);
  216. info.AddValue ("_PublicKey", publicKey);
  217. info.AddValue ("_PublicToken", keyToken);
  218. info.AddValue ("_CultureInfo", cultureinfo != null ? cultureinfo.LCID : -1);
  219. info.AddValue ("_CodeBase", codebase);
  220. info.AddValue ("_Version", Version);
  221. info.AddValue ("_HashAlgorithm", hashalg);
  222. info.AddValue ("_HashAlgorithmForControl", AssemblyHashAlgorithm.None);
  223. info.AddValue ("_StrongNameKeyPair", keypair);
  224. info.AddValue ("_VersionCompatibility", versioncompat);
  225. info.AddValue ("_Flags", flags);
  226. info.AddValue ("_HashForControl", null);
  227. }
  228. public object Clone()
  229. {
  230. AssemblyName an = new AssemblyName ();
  231. an.name = name;
  232. an.codebase = codebase;
  233. an.major = major;
  234. an.minor = minor;
  235. an.build = build;
  236. an.revision = revision;
  237. an.cultureinfo = cultureinfo;
  238. an.flags = flags;
  239. an.hashalg = hashalg;
  240. an.keypair = keypair;
  241. an.publicKey = publicKey;
  242. an.keyToken = keyToken;
  243. an.versioncompat = versioncompat;
  244. return an;
  245. }
  246. public void OnDeserialization (object sender)
  247. {
  248. Version = version;
  249. }
  250. public static AssemblyName GetAssemblyName (string assemblyFile)
  251. {
  252. if (assemblyFile == null)
  253. throw new ArgumentNullException ("assemblyFile");
  254. AssemblyName aname = new AssemblyName ();
  255. Assembly.InternalGetAssemblyName (System.IO.Path.GetFullPath (assemblyFile), aname);
  256. return aname;
  257. }
  258. }
  259. }