Assembly.cs 15 KB

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