AssemblyName.cs 5.3 KB

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