AssemblyName.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  11. //
  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:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  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.
  30. //
  31. using System.Configuration.Assemblies;
  32. using System.Globalization;
  33. using System.Runtime.Serialization;
  34. using System.Security.Cryptography;
  35. using System.Security.Permissions;
  36. using System.Text;
  37. using System.Runtime.InteropServices;
  38. using System.Runtime.CompilerServices;
  39. using Mono.Security;
  40. namespace System.Reflection {
  41. // References:
  42. // a. Uniform Resource Identifiers (URI): Generic Syntax
  43. // http://www.ietf.org/rfc/rfc2396.txt
  44. #if NET_2_0
  45. [ComVisible (true)]
  46. #endif
  47. [Serializable]
  48. [MonoTODO ("Fix serialization compatibility with MS.NET")]
  49. public sealed class AssemblyName : ICloneable, ISerializable, IDeserializationCallback {
  50. string name;
  51. string codebase;
  52. int major, minor, build, revision;
  53. CultureInfo cultureinfo;
  54. AssemblyNameFlags flags;
  55. AssemblyHashAlgorithm hashalg;
  56. StrongNameKeyPair keypair;
  57. byte[] publicKey;
  58. byte[] keyToken;
  59. AssemblyVersionCompatibility versioncompat;
  60. Version version;
  61. public AssemblyName ()
  62. {
  63. // defaults
  64. versioncompat = AssemblyVersionCompatibility.SameMachine;
  65. }
  66. #if NET_2_0
  67. public AssemblyName (string assemblyName)
  68. {
  69. name = assemblyName;
  70. }
  71. #endif
  72. internal AssemblyName (SerializationInfo si, StreamingContext sc)
  73. {
  74. name = si.GetString ("_Name");
  75. codebase = si.GetString ("_CodeBase");
  76. version = (Version)si.GetValue ("_Version", typeof (Version));
  77. publicKey = (byte[])si.GetValue ("_PublicKey", typeof (byte[]));
  78. keyToken = (byte[])si.GetValue ("_PublicToken", typeof (byte[]));
  79. hashalg = (AssemblyHashAlgorithm)si.GetValue ("_HashAlgorithm", typeof (AssemblyHashAlgorithm));
  80. keypair = (StrongNameKeyPair)si.GetValue ("_StrongNameKeyPair", typeof (StrongNameKeyPair));
  81. versioncompat = (AssemblyVersionCompatibility)si.GetValue ("_VersionCompatibility", typeof (AssemblyVersionCompatibility));
  82. flags = (AssemblyNameFlags)si.GetValue ("_Flags", typeof (AssemblyNameFlags));
  83. int lcid = si.GetInt32 ("_CultureInfo");
  84. if (lcid != -1) cultureinfo = new CultureInfo (lcid);
  85. }
  86. public string Name {
  87. get { return name; }
  88. set { name = value; }
  89. }
  90. public string CodeBase {
  91. get { return codebase; }
  92. set { codebase = value; }
  93. }
  94. public string EscapedCodeBase {
  95. get {
  96. if (codebase == null)
  97. return null;
  98. return Uri.EscapeString (codebase, false, true, true);
  99. }
  100. }
  101. public CultureInfo CultureInfo {
  102. get { return cultureinfo; }
  103. set { cultureinfo = value; }
  104. }
  105. public AssemblyNameFlags Flags {
  106. get { return flags; }
  107. set { flags = value; }
  108. }
  109. public string FullName {
  110. get {
  111. if (name == null)
  112. return null;
  113. StringBuilder fname = new StringBuilder ();
  114. fname.Append (name);
  115. if (Version != null) {
  116. fname.Append (", Version=");
  117. fname.Append (Version.ToString ());
  118. }
  119. if (cultureinfo != null) {
  120. fname.Append (", Culture=");
  121. if (cultureinfo.LCID == CultureInfo.InvariantCulture.LCID)
  122. fname.Append ("neutral");
  123. else
  124. fname.Append (cultureinfo.Name);
  125. }
  126. byte[] pub_tok = GetPublicKeyToken ();
  127. if (pub_tok != null) {
  128. if (pub_tok.Length == 0)
  129. fname.Append (", PublicKeyToken=null");
  130. else {
  131. fname.Append (", PublicKeyToken=");
  132. for (int i = 0; i < pub_tok.Length; i++)
  133. fname.Append (pub_tok[i].ToString ("x2"));
  134. }
  135. }
  136. return fname.ToString ();
  137. }
  138. }
  139. public AssemblyHashAlgorithm HashAlgorithm {
  140. get { return hashalg; }
  141. set { hashalg = value; }
  142. }
  143. public StrongNameKeyPair KeyPair {
  144. get { return keypair; }
  145. set { keypair = value; }
  146. }
  147. public Version Version {
  148. get {
  149. return version;
  150. }
  151. set {
  152. version = value;
  153. if (value == null)
  154. major = minor = build = revision = 0;
  155. else {
  156. major = value.Major;
  157. minor = value.Minor;
  158. build = value.Build;
  159. revision = value.Revision;
  160. }
  161. }
  162. }
  163. public AssemblyVersionCompatibility VersionCompatibility {
  164. get { return versioncompat; }
  165. set { versioncompat = value; }
  166. }
  167. public override string ToString ()
  168. {
  169. string name = FullName;
  170. return (name != null) ? name : base.ToString ();
  171. }
  172. public byte[] GetPublicKey()
  173. {
  174. // to match MS implementation -- funny one
  175. if (publicKey != null)
  176. return publicKey;
  177. else if (name == null)
  178. return null;
  179. else
  180. return new byte [0];
  181. }
  182. public byte[] GetPublicKeyToken()
  183. {
  184. if (keyToken != null)
  185. return keyToken;
  186. else if (publicKey == null)
  187. return null;
  188. else {
  189. HashAlgorithm ha = null;
  190. switch (hashalg) {
  191. case AssemblyHashAlgorithm.MD5:
  192. ha = MD5.Create ();
  193. break;
  194. default:
  195. // None default to SHA1
  196. ha = SHA1.Create ();
  197. break;
  198. }
  199. byte[] hash = ha.ComputeHash (publicKey);
  200. // we need the last 8 bytes in reverse order
  201. keyToken = new byte [8];
  202. Array.Copy (hash, (hash.Length - 8), keyToken, 0, 8);
  203. Array.Reverse (keyToken, 0, 8);
  204. return keyToken;
  205. }
  206. }
  207. public void SetPublicKey (byte[] publicKey)
  208. {
  209. flags = AssemblyNameFlags.PublicKey;
  210. this.publicKey = publicKey;
  211. }
  212. public void SetPublicKeyToken (byte[] publicKeyToken)
  213. {
  214. keyToken = publicKeyToken;
  215. }
  216. [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
  217. public void GetObjectData (SerializationInfo info, StreamingContext context)
  218. {
  219. if (info == null)
  220. throw new ArgumentNullException ("info");
  221. info.AddValue ("_Name", name);
  222. info.AddValue ("_PublicKey", publicKey);
  223. info.AddValue ("_PublicToken", keyToken);
  224. info.AddValue ("_CultureInfo", cultureinfo != null ? cultureinfo.LCID : -1);
  225. info.AddValue ("_CodeBase", codebase);
  226. info.AddValue ("_Version", Version);
  227. info.AddValue ("_HashAlgorithm", hashalg);
  228. info.AddValue ("_HashAlgorithmForControl", AssemblyHashAlgorithm.None);
  229. info.AddValue ("_StrongNameKeyPair", keypair);
  230. info.AddValue ("_VersionCompatibility", versioncompat);
  231. info.AddValue ("_Flags", flags);
  232. info.AddValue ("_HashForControl", null);
  233. }
  234. public object Clone()
  235. {
  236. AssemblyName an = new AssemblyName ();
  237. an.name = name;
  238. an.codebase = codebase;
  239. an.major = major;
  240. an.minor = minor;
  241. an.build = build;
  242. an.revision = revision;
  243. an.version = version;
  244. an.cultureinfo = cultureinfo;
  245. an.flags = flags;
  246. an.hashalg = hashalg;
  247. an.keypair = keypair;
  248. an.publicKey = publicKey;
  249. an.keyToken = keyToken;
  250. an.versioncompat = versioncompat;
  251. return an;
  252. }
  253. public void OnDeserialization (object sender)
  254. {
  255. Version = version;
  256. }
  257. public static AssemblyName GetAssemblyName (string assemblyFile)
  258. {
  259. if (assemblyFile == null)
  260. throw new ArgumentNullException ("assemblyFile");
  261. AssemblyName aname = new AssemblyName ();
  262. Assembly.InternalGetAssemblyName (System.IO.Path.GetFullPath (assemblyFile), aname);
  263. return aname;
  264. }
  265. }
  266. }