AssemblyName.cs 9.5 KB

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