Assembly.cs 15 KB

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