AssemblyName.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. [ComDefaultInterfaceAttribute (typeof (_AssemblyName))]
  47. #endif
  48. [Serializable]
  49. [ClassInterfaceAttribute (ClassInterfaceType.None)]
  50. [MonoTODO ("Fix serialization compatibility with MS.NET")]
  51. public sealed class AssemblyName : ICloneable, ISerializable, IDeserializationCallback, _AssemblyName {
  52. #region Synch with object-internals.h
  53. string name;
  54. string codebase;
  55. int major, minor, build, revision;
  56. CultureInfo cultureinfo;
  57. AssemblyNameFlags flags;
  58. AssemblyHashAlgorithm hashalg;
  59. StrongNameKeyPair keypair;
  60. byte[] publicKey;
  61. byte[] keyToken;
  62. AssemblyVersionCompatibility versioncompat;
  63. Version version;
  64. #if NET_2_0
  65. ProcessorArchitecture processor_architecture;
  66. #else
  67. int processor_architecture;
  68. #endif
  69. #endregion
  70. public AssemblyName ()
  71. {
  72. // defaults
  73. versioncompat = AssemblyVersionCompatibility.SameMachine;
  74. }
  75. #if NET_2_0
  76. public AssemblyName (string assemblyName)
  77. {
  78. name = assemblyName;
  79. }
  80. [MonoTODO]
  81. public ProcessorArchitecture ProcessorArchitecture {
  82. get {
  83. return processor_architecture;
  84. }
  85. set {
  86. processor_architecture = value;
  87. }
  88. }
  89. #endif
  90. internal AssemblyName (SerializationInfo si, StreamingContext sc)
  91. {
  92. name = si.GetString ("_Name");
  93. codebase = si.GetString ("_CodeBase");
  94. version = (Version)si.GetValue ("_Version", typeof (Version));
  95. publicKey = (byte[])si.GetValue ("_PublicKey", typeof (byte[]));
  96. keyToken = (byte[])si.GetValue ("_PublicToken", typeof (byte[]));
  97. hashalg = (AssemblyHashAlgorithm)si.GetValue ("_HashAlgorithm", typeof (AssemblyHashAlgorithm));
  98. keypair = (StrongNameKeyPair)si.GetValue ("_StrongNameKeyPair", typeof (StrongNameKeyPair));
  99. versioncompat = (AssemblyVersionCompatibility)si.GetValue ("_VersionCompatibility", typeof (AssemblyVersionCompatibility));
  100. flags = (AssemblyNameFlags)si.GetValue ("_Flags", typeof (AssemblyNameFlags));
  101. int lcid = si.GetInt32 ("_CultureInfo");
  102. if (lcid != -1) cultureinfo = new CultureInfo (lcid);
  103. }
  104. public string Name {
  105. get { return name; }
  106. set { name = value; }
  107. }
  108. public string CodeBase {
  109. get { return codebase; }
  110. set { codebase = value; }
  111. }
  112. public string EscapedCodeBase {
  113. get {
  114. if (codebase == null)
  115. return null;
  116. return Uri.EscapeString (codebase, false, true, true);
  117. }
  118. }
  119. public CultureInfo CultureInfo {
  120. get { return cultureinfo; }
  121. set { cultureinfo = value; }
  122. }
  123. public AssemblyNameFlags Flags {
  124. get { return flags; }
  125. set { flags = value; }
  126. }
  127. public string FullName {
  128. get {
  129. if (name == null)
  130. return null;
  131. StringBuilder fname = new StringBuilder ();
  132. fname.Append (name);
  133. if (Version != null) {
  134. fname.Append (", Version=");
  135. fname.Append (Version.ToString ());
  136. }
  137. if (cultureinfo != null) {
  138. fname.Append (", Culture=");
  139. if (cultureinfo.LCID == CultureInfo.InvariantCulture.LCID)
  140. fname.Append ("neutral");
  141. else
  142. fname.Append (cultureinfo.Name);
  143. }
  144. byte[] pub_tok = GetPublicKeyToken ();
  145. if (pub_tok != null) {
  146. if (pub_tok.Length == 0)
  147. fname.Append (", PublicKeyToken=null");
  148. else {
  149. fname.Append (", PublicKeyToken=");
  150. for (int i = 0; i < pub_tok.Length; i++)
  151. fname.Append (pub_tok[i].ToString ("x2"));
  152. }
  153. }
  154. return fname.ToString ();
  155. }
  156. }
  157. public AssemblyHashAlgorithm HashAlgorithm {
  158. get { return hashalg; }
  159. set { hashalg = value; }
  160. }
  161. public StrongNameKeyPair KeyPair {
  162. get { return keypair; }
  163. set { keypair = value; }
  164. }
  165. public Version Version {
  166. get {
  167. return version;
  168. }
  169. set {
  170. version = value;
  171. if (value == null)
  172. major = minor = build = revision = 0;
  173. else {
  174. major = value.Major;
  175. minor = value.Minor;
  176. build = value.Build;
  177. revision = value.Revision;
  178. }
  179. }
  180. }
  181. public AssemblyVersionCompatibility VersionCompatibility {
  182. get { return versioncompat; }
  183. set { versioncompat = value; }
  184. }
  185. public override string ToString ()
  186. {
  187. string name = FullName;
  188. return (name != null) ? name : base.ToString ();
  189. }
  190. public byte[] GetPublicKey()
  191. {
  192. return publicKey;
  193. // FIXME: In some cases MS implementation returns
  194. // "new byte [0]" instead of null
  195. }
  196. public byte[] GetPublicKeyToken()
  197. {
  198. if (keyToken != null)
  199. return keyToken;
  200. else if (publicKey == null)
  201. return null;
  202. else {
  203. HashAlgorithm ha = null;
  204. switch (hashalg) {
  205. case AssemblyHashAlgorithm.MD5:
  206. ha = MD5.Create ();
  207. break;
  208. default:
  209. // None default to SHA1
  210. ha = SHA1.Create ();
  211. break;
  212. }
  213. byte[] hash = ha.ComputeHash (publicKey);
  214. // we need the last 8 bytes in reverse order
  215. keyToken = new byte [8];
  216. Array.Copy (hash, (hash.Length - 8), keyToken, 0, 8);
  217. Array.Reverse (keyToken, 0, 8);
  218. return keyToken;
  219. }
  220. }
  221. public void SetPublicKey (byte[] publicKey)
  222. {
  223. flags = AssemblyNameFlags.PublicKey;
  224. this.publicKey = publicKey;
  225. }
  226. public void SetPublicKeyToken (byte[] publicKeyToken)
  227. {
  228. keyToken = publicKeyToken;
  229. }
  230. [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
  231. public void GetObjectData (SerializationInfo info, StreamingContext context)
  232. {
  233. if (info == null)
  234. throw new ArgumentNullException ("info");
  235. info.AddValue ("_Name", name);
  236. info.AddValue ("_PublicKey", publicKey);
  237. info.AddValue ("_PublicToken", keyToken);
  238. info.AddValue ("_CultureInfo", cultureinfo != null ? cultureinfo.LCID : -1);
  239. info.AddValue ("_CodeBase", codebase);
  240. info.AddValue ("_Version", Version);
  241. info.AddValue ("_HashAlgorithm", hashalg);
  242. info.AddValue ("_HashAlgorithmForControl", AssemblyHashAlgorithm.None);
  243. info.AddValue ("_StrongNameKeyPair", keypair);
  244. info.AddValue ("_VersionCompatibility", versioncompat);
  245. info.AddValue ("_Flags", flags);
  246. info.AddValue ("_HashForControl", null);
  247. }
  248. public object Clone()
  249. {
  250. AssemblyName an = new AssemblyName ();
  251. an.name = name;
  252. an.codebase = codebase;
  253. an.major = major;
  254. an.minor = minor;
  255. an.build = build;
  256. an.revision = revision;
  257. an.version = version;
  258. an.cultureinfo = cultureinfo;
  259. an.flags = flags;
  260. an.hashalg = hashalg;
  261. an.keypair = keypair;
  262. an.publicKey = publicKey;
  263. an.keyToken = keyToken;
  264. an.versioncompat = versioncompat;
  265. return an;
  266. }
  267. public void OnDeserialization (object sender)
  268. {
  269. Version = version;
  270. }
  271. public static AssemblyName GetAssemblyName (string assemblyFile)
  272. {
  273. if (assemblyFile == null)
  274. throw new ArgumentNullException ("assemblyFile");
  275. AssemblyName aname = new AssemblyName ();
  276. Assembly.InternalGetAssemblyName (System.IO.Path.GetFullPath (assemblyFile), aname);
  277. return aname;
  278. }
  279. #if NET_1_1
  280. void _AssemblyName.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
  281. {
  282. throw new NotImplementedException ();
  283. }
  284. void _AssemblyName.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
  285. {
  286. throw new NotImplementedException ();
  287. }
  288. void _AssemblyName.GetTypeInfoCount (out uint pcTInfo)
  289. {
  290. throw new NotImplementedException ();
  291. }
  292. void _AssemblyName.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
  293. IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
  294. {
  295. throw new NotImplementedException ();
  296. }
  297. #endif
  298. }
  299. }