AssemblyName.cs 4.8 KB

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