Assembly.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. //
  2. // System.Reflection/Assembly.cs
  3. //
  4. // Author:
  5. // Paolo Molaro ([email protected])
  6. //
  7. // (C) 2001 Ximian, Inc. http://www.ximian.com
  8. // Copyright (C) 2004 Novell (http://www.novell.com)
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.Security.Policy;
  34. using System.Runtime.Serialization;
  35. using System.Reflection.Emit;
  36. using System.IO;
  37. using System.Globalization;
  38. using System.Runtime.CompilerServices;
  39. using System.Runtime.InteropServices;
  40. using System.Collections;
  41. using System.Configuration.Assemblies;
  42. namespace System.Reflection {
  43. internal class ResolveEventHolder {
  44. public event ModuleResolveEventHandler ModuleResolve;
  45. }
  46. [Serializable]
  47. [ClassInterface(ClassInterfaceType.AutoDual)]
  48. public class Assembly : System.Reflection.ICustomAttributeProvider,
  49. System.Security.IEvidenceFactory, System.Runtime.Serialization.ISerializable {
  50. private IntPtr _mono_assembly;
  51. private ResolveEventHolder resolve_event_holder;
  52. internal Assembly () {
  53. resolve_event_holder = new ResolveEventHolder ();
  54. }
  55. //
  56. // We can't store the event directly in this class, since the
  57. // compile would silently insert the fields before _mono_assembly
  58. //
  59. public event ModuleResolveEventHandler ModuleResolve {
  60. add {
  61. resolve_event_holder.ModuleResolve -= value;
  62. }
  63. remove {
  64. resolve_event_holder.ModuleResolve -= value;
  65. }
  66. }
  67. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  68. private extern string get_code_base ();
  69. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  70. private extern string get_location ();
  71. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  72. private extern string InternalImageRuntimeVersion ();
  73. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  74. private extern bool get_global_assembly_cache ();
  75. public virtual string CodeBase {
  76. get {
  77. return get_code_base ();
  78. }
  79. }
  80. internal virtual string CopiedCodeBase {
  81. get {
  82. return get_code_base ();
  83. }
  84. }
  85. [MonoTODO]
  86. public virtual string EscapedCodeBase {
  87. get {
  88. //FIXME: escape characters -> Uri
  89. return get_code_base ();
  90. }
  91. }
  92. public virtual string FullName {
  93. get {
  94. //
  95. // FIXME: This is wrong, but it gets us going
  96. // in the compiler for now
  97. //
  98. return GetName (false).ToString ();
  99. }
  100. }
  101. public virtual extern MethodInfo EntryPoint {
  102. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  103. get;
  104. }
  105. [MonoTODO ("CAS related - post Mono 1.0 (see #53548)")]
  106. public virtual Evidence Evidence {
  107. get { return new Evidence (); }
  108. }
  109. public bool GlobalAssemblyCache {
  110. get {
  111. return get_global_assembly_cache ();
  112. }
  113. }
  114. public virtual String Location {
  115. get {
  116. return get_location ();
  117. }
  118. }
  119. #if NET_1_1
  120. [ComVisible (false)]
  121. public virtual string ImageRuntimeVersion {
  122. get {
  123. return InternalImageRuntimeVersion ();
  124. }
  125. }
  126. #endif
  127. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  128. {
  129. UnitySerializationHolder.GetAssemblyData (this, info, context);
  130. }
  131. public virtual bool IsDefined (Type attributeType, bool inherit)
  132. {
  133. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  134. }
  135. public virtual object [] GetCustomAttributes (bool inherit)
  136. {
  137. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  138. }
  139. public virtual object [] GetCustomAttributes (Type attributeType, bool inherit)
  140. {
  141. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  142. }
  143. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  144. private extern object GetFilesInternal (String name);
  145. public virtual FileStream[] GetFiles ()
  146. {
  147. string[] names = (string[]) GetFilesInternal (null);
  148. if (names == null)
  149. return new FileStream [0];
  150. FileStream[] res = new FileStream [names.Length];
  151. for (int i = 0; i < names.Length; ++i)
  152. res [i] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
  153. return res;
  154. }
  155. [MonoTODO]
  156. public virtual FileStream [] GetFiles (bool getResourceModules)
  157. {
  158. throw new NotImplementedException ();
  159. }
  160. public virtual FileStream GetFile (String name)
  161. {
  162. if (name == null)
  163. throw new ArgumentNullException ("name");
  164. string filename = (string)GetFilesInternal (name);
  165. if (filename != null)
  166. return new FileStream (filename, FileMode.Open, FileAccess.Read);
  167. else
  168. return null;
  169. }
  170. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  171. private extern IntPtr GetManifestResourceInternal (String name, out int size, out Module module);
  172. public virtual Stream GetManifestResourceStream (String name)
  173. {
  174. if (name == null)
  175. throw new ArgumentNullException ("name");
  176. if (name == "")
  177. throw new ArgumentException ("name cannot have zero length.");
  178. ManifestResourceInfo info = GetManifestResourceInfo (name);
  179. if (info == null)
  180. return null;
  181. if (info.ReferencedAssembly != null)
  182. return info.ReferencedAssembly.GetManifestResourceStream (name);
  183. if ((info.FileName != null) && (info.ResourceLocation == 0)) {
  184. string filename = Path.Combine (Path.GetDirectoryName (Location),
  185. info.FileName);
  186. return new FileStream (filename, FileMode.Open, FileAccess.Read);
  187. }
  188. int size;
  189. Module module;
  190. IntPtr data = GetManifestResourceInternal (name, out size, out module);
  191. if (data == (IntPtr) 0)
  192. return null;
  193. else {
  194. IntPtrStream stream = new IntPtrStream (data, size);
  195. /*
  196. * The returned pointer points inside metadata, so
  197. * we have to increase the refcount of the module, and decrease
  198. * it when the stream is finalized.
  199. */
  200. stream.Closed += new EventHandler (new ResourceCloseHandler (module).OnClose);
  201. return stream;
  202. }
  203. }
  204. public virtual Stream GetManifestResourceStream (Type type, String name)
  205. {
  206. string ns;
  207. if (type != null)
  208. ns = type.Namespace;
  209. else
  210. ns = null;
  211. if ((ns == null) || (ns == ""))
  212. return GetManifestResourceStream (name);
  213. else
  214. return GetManifestResourceStream (ns + "." + name);
  215. }
  216. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  217. private extern Type[] GetTypes (bool exportedOnly);
  218. public virtual Type[] GetTypes ()
  219. {
  220. return GetTypes (false);
  221. }
  222. public virtual Type[] GetExportedTypes ()
  223. {
  224. return GetTypes (true);
  225. }
  226. public virtual Type GetType (String name, Boolean throwOnError)
  227. {
  228. return GetType (name, throwOnError, false);
  229. }
  230. public virtual Type GetType (String name) {
  231. return GetType (name, false, false);
  232. }
  233. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  234. internal extern Type InternalGetType (Module module, String name, Boolean throwOnError, Boolean ignoreCase);
  235. public Type GetType (string name, bool throwOnError, bool ignoreCase)
  236. {
  237. if (name == null)
  238. throw new ArgumentNullException (name);
  239. return InternalGetType (null, name, throwOnError, ignoreCase);
  240. }
  241. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  242. internal extern static void InternalGetAssemblyName (string assemblyFile, AssemblyName aname);
  243. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  244. static extern void FillName (Assembly ass, AssemblyName aname);
  245. public virtual AssemblyName GetName (Boolean copiedName)
  246. {
  247. AssemblyName aname = new AssemblyName ();
  248. FillName (this, aname);
  249. return aname;
  250. }
  251. public virtual AssemblyName GetName ()
  252. {
  253. return GetName (false);
  254. }
  255. public override String ToString ()
  256. {
  257. return GetName ().ToString ();
  258. }
  259. public static String CreateQualifiedName (String assemblyName, String typeName)
  260. {
  261. return typeName + ", " + assemblyName;
  262. }
  263. public static Assembly GetAssembly (Type type)
  264. {
  265. if (type != null)
  266. return type.Assembly;
  267. throw new ArgumentNullException ("type");
  268. }
  269. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  270. public static extern Assembly GetEntryAssembly();
  271. public Assembly GetSatelliteAssembly (CultureInfo culture)
  272. {
  273. return GetSatelliteAssembly (culture, null);
  274. }
  275. public Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
  276. {
  277. if (culture == null)
  278. throw new ArgumentException ("culture");
  279. AssemblyName aname = GetName (true);
  280. if (version != null)
  281. aname.Version = version;
  282. aname.CultureInfo = culture;
  283. aname.Name = aname.Name + ".resources";
  284. return Load (aname);
  285. }
  286. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  287. public extern static Assembly LoadFrom (String assemblyFile);
  288. [MonoTODO]
  289. public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence)
  290. {
  291. // Evidence is ignored
  292. return LoadFrom (assemblyFile);
  293. }
  294. #if NET_1_1
  295. [MonoTODO]
  296. public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
  297. {
  298. if (assemblyFile == null)
  299. throw new ArgumentNullException ("assemblyFile");
  300. if (assemblyFile == String.Empty)
  301. throw new ArgumentException ("Name can't be the empty string", "assemblyFile");
  302. throw new NotImplementedException ();
  303. }
  304. [MonoTODO]
  305. public static Assembly LoadFile (String path, Evidence securityEvidence) {
  306. if (path == null)
  307. throw new ArgumentNullException ("path");
  308. if (path == String.Empty)
  309. throw new ArgumentException ("Path can't be empty", "path");
  310. // FIXME: Make this do the right thing
  311. return LoadFrom (path, securityEvidence);
  312. }
  313. public static Assembly LoadFile (String path) {
  314. return LoadFile (path, null);
  315. }
  316. #endif
  317. public static Assembly Load (String assemblyString)
  318. {
  319. return AppDomain.CurrentDomain.Load (assemblyString);
  320. }
  321. public static Assembly Load (String assemblyString, Evidence assemblySecurity)
  322. {
  323. return AppDomain.CurrentDomain.Load (assemblyString, assemblySecurity);
  324. }
  325. public static Assembly Load (AssemblyName assemblyRef)
  326. {
  327. return AppDomain.CurrentDomain.Load (assemblyRef);
  328. }
  329. public static Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
  330. {
  331. return AppDomain.CurrentDomain.Load (assemblyRef, assemblySecurity);
  332. }
  333. public static Assembly Load (Byte[] rawAssembly)
  334. {
  335. return AppDomain.CurrentDomain.Load (rawAssembly);
  336. }
  337. public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore)
  338. {
  339. return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
  340. }
  341. public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore,
  342. Evidence securityEvidence)
  343. {
  344. return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore, securityEvidence);
  345. }
  346. public static Assembly LoadWithPartialName (string partialName)
  347. {
  348. return LoadWithPartialName (partialName, null);
  349. }
  350. [MonoTODO]
  351. public Module LoadModule (string moduleName, byte [] rawModule)
  352. {
  353. throw new NotImplementedException ();
  354. }
  355. [MonoTODO]
  356. public Module LoadModule (string moduleName, byte [] rawModule, byte [] rawSymbolStore)
  357. {
  358. throw new NotImplementedException ();
  359. }
  360. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  361. private static extern Assembly load_with_partial_name (string name, Evidence e);
  362. /**
  363. * LAMESPEC: It is possible for this method to throw exceptions IF the name supplied
  364. * is a valid gac name and contains filesystem entry charachters at the end of the name
  365. * ie System/// will throw an exception. However ////System will not as that is canocolized
  366. * out of the name.
  367. */
  368. public static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence)
  369. {
  370. if (partialName == null)
  371. throw new NullReferenceException ();
  372. int ci = partialName.IndexOf (',');
  373. if (ci > 0)
  374. partialName = partialName.Substring (0, ci);
  375. return load_with_partial_name (partialName, securityEvidence);
  376. }
  377. public Object CreateInstance (String typeName)
  378. {
  379. return CreateInstance (typeName, false);
  380. }
  381. public Object CreateInstance (String typeName, Boolean ignoreCase)
  382. {
  383. Type t = GetType (typeName, false, ignoreCase);
  384. if (t == null)
  385. return null;
  386. return Activator.CreateInstance (t);
  387. }
  388. public Object CreateInstance (String typeName, Boolean ignoreCase,
  389. BindingFlags bindingAttr, Binder binder,
  390. Object[] args, CultureInfo culture,
  391. Object[] activationAttributes)
  392. {
  393. Type t = GetType (typeName, false, ignoreCase);
  394. if (t == null)
  395. return null;
  396. return Activator.CreateInstance (t, bindingAttr, binder, args, culture, activationAttributes);
  397. }
  398. public Module[] GetLoadedModules ()
  399. {
  400. return GetLoadedModules (false);
  401. }
  402. [MonoTODO]
  403. public Module[] GetLoadedModules (bool getResourceModules)
  404. {
  405. // Currently, the two sets of modules are equal
  406. return GetModules (getResourceModules);
  407. }
  408. public Module[] GetModules ()
  409. {
  410. return GetModules (false);
  411. }
  412. public Module GetModule (String name)
  413. {
  414. if (name == null)
  415. throw new ArgumentNullException ("name");
  416. if (name == "")
  417. throw new ArgumentException ("Name can't be empty");
  418. Module[] modules = GetModules (true);
  419. foreach (Module module in GetModules (true)) {
  420. if (module.ScopeName == name)
  421. return module;
  422. }
  423. return null;
  424. }
  425. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  426. internal extern Module[] GetModulesInternal ();
  427. public Module[] GetModules (bool getResourceModules) {
  428. Module[] modules = GetModulesInternal ();
  429. if (!getResourceModules) {
  430. ArrayList result = new ArrayList (modules.Length);
  431. foreach (Module m in modules)
  432. if (!m.IsResource ())
  433. result.Add (m);
  434. return (Module[])result.ToArray (typeof (Module));
  435. }
  436. else
  437. return modules;
  438. }
  439. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  440. internal extern string[] GetNamespaces ();
  441. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  442. public extern virtual String[] GetManifestResourceNames ();
  443. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  444. public extern static Assembly GetExecutingAssembly ();
  445. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  446. public extern static Assembly GetCallingAssembly ();
  447. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  448. public extern AssemblyName[] GetReferencedAssemblies ();
  449. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  450. private extern bool GetManifestResourceInfoInternal (String name, ManifestResourceInfo info);
  451. public virtual ManifestResourceInfo GetManifestResourceInfo (String resourceName)
  452. {
  453. if (resourceName == null)
  454. throw new ArgumentNullException ("resourceName");
  455. if (resourceName == "")
  456. throw new ArgumentException ("String cannot have zero length.");
  457. ManifestResourceInfo result = new ManifestResourceInfo ();
  458. bool found = GetManifestResourceInfoInternal (resourceName, result);
  459. if (found)
  460. return result;
  461. else
  462. return null;
  463. }
  464. private class ResourceCloseHandler {
  465. Module module;
  466. public ResourceCloseHandler (Module module) {
  467. this.module = module;
  468. }
  469. public void OnClose (object sender, EventArgs e) {
  470. // The module dtor will take care of things
  471. module = null;
  472. }
  473. }
  474. //
  475. // The following functions are only for the Mono Debugger.
  476. //
  477. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  478. internal static extern MethodBase MonoDebugger_GetMethod (Assembly assembly, int token);
  479. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  480. internal static extern int MonoDebugger_GetMethodToken (Assembly assembly, MethodBase method);
  481. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  482. internal static extern Type MonoDebugger_GetLocalTypeFromSignature (Assembly assembly, byte[] signature);
  483. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  484. internal static extern Type MonoDebugger_GetType (Assembly assembly, int token);
  485. }
  486. }