Assembly.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. public virtual string CodeBase {
  36. get {
  37. return get_code_base ();
  38. }
  39. }
  40. internal virtual string CopiedCodeBase {
  41. get {
  42. return get_code_base ();
  43. }
  44. }
  45. [MonoTODO]
  46. public virtual string EscapedCodeBase {
  47. get {
  48. //FIXME: escape characters -> Uri
  49. return get_code_base ();
  50. }
  51. }
  52. public virtual string FullName {
  53. get {
  54. //
  55. // FIXME: This is wrong, but it gets us going
  56. // in the compiler for now
  57. //
  58. return GetName (false).ToString ();
  59. }
  60. }
  61. public virtual extern MethodInfo EntryPoint {
  62. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  63. get;
  64. }
  65. public virtual Evidence Evidence {
  66. get {
  67. return null;
  68. }
  69. }
  70. public bool GlobalAssemblyCache {
  71. get {
  72. //TODO: if we ever have a GAC, fix this.
  73. return false;
  74. }
  75. }
  76. public virtual String Location {
  77. get {
  78. return get_location ();
  79. }
  80. }
  81. #if NET_1_1
  82. public virtual string ImageRuntimeVersion {
  83. get {
  84. return InternalImageRuntimeVersion ();
  85. }
  86. }
  87. #endif
  88. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  89. {
  90. UnitySerializationHolder.GetAssemblyData (this, info, context);
  91. }
  92. public virtual bool IsDefined (Type attributeType, bool inherit)
  93. {
  94. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  95. }
  96. public virtual object [] GetCustomAttributes (bool inherit)
  97. {
  98. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  99. }
  100. public virtual object [] GetCustomAttributes (Type attributeType, bool inherit)
  101. {
  102. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  103. }
  104. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  105. private extern object GetFilesInternal (String name);
  106. public virtual FileStream[] GetFiles ()
  107. {
  108. string[] names = (string[]) GetFilesInternal (null);
  109. if (names == null)
  110. return new FileStream [0];
  111. FileStream[] res = new FileStream [names.Length];
  112. for (int i = 0; i < names.Length; ++i)
  113. res [i] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
  114. return res;
  115. }
  116. [MonoTODO]
  117. public virtual FileStream [] GetFiles (bool getResourceModules)
  118. {
  119. throw new NotImplementedException ();
  120. }
  121. public virtual FileStream GetFile (String name)
  122. {
  123. if (name == null)
  124. throw new ArgumentNullException ("name");
  125. string filename = (string)GetFilesInternal (name);
  126. if (filename != null)
  127. return new FileStream (filename, FileMode.Open, FileAccess.Read);
  128. else
  129. return null;
  130. }
  131. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  132. private extern IntPtr GetManifestResourceInternal (String name, out int size);
  133. public virtual Stream GetManifestResourceStream (String name)
  134. {
  135. if (name == null)
  136. throw new ArgumentNullException ("name");
  137. if (name == "")
  138. throw new ArgumentException ("name cannot have zero length.");
  139. ManifestResourceInfo info = GetManifestResourceInfo (name);
  140. if (info == null)
  141. return null;
  142. if (info.ReferencedAssembly != null)
  143. return info.ReferencedAssembly.GetManifestResourceStream (name);
  144. if ((info.FileName != null) && (info.ResourceLocation == 0)) {
  145. string filename = Path.Combine (Path.GetDirectoryName (Location),
  146. info.FileName);
  147. return new FileStream (filename, FileMode.Open, FileAccess.Read);
  148. }
  149. int size;
  150. IntPtr data = GetManifestResourceInternal (name, out size);
  151. if (data == (IntPtr) 0)
  152. return null;
  153. else
  154. return new IntPtrStream (data, size);
  155. }
  156. public virtual Stream GetManifestResourceStream (Type type, String name)
  157. {
  158. string ns;
  159. if (type != null)
  160. ns = type.Namespace;
  161. else
  162. ns = null;
  163. if ((ns == null) || (ns == ""))
  164. return GetManifestResourceStream (name);
  165. else
  166. return GetManifestResourceStream (ns + "." + name);
  167. }
  168. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  169. private extern Type[] GetTypes (bool exportedOnly);
  170. public virtual Type[] GetTypes ()
  171. {
  172. return GetTypes (false);
  173. }
  174. public virtual Type[] GetExportedTypes ()
  175. {
  176. return GetTypes (true);
  177. }
  178. public virtual Type GetType (String name, Boolean throwOnError)
  179. {
  180. return GetType (name, throwOnError, false);
  181. }
  182. public virtual Type GetType (String name) {
  183. return GetType (name, false, false);
  184. }
  185. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  186. internal extern Type InternalGetType (Module module, String name, Boolean throwOnError, Boolean ignoreCase);
  187. public Type GetType (string name, bool throwOnError, bool ignoreCase)
  188. {
  189. if (name == null)
  190. throw new ArgumentNullException (name);
  191. return InternalGetType (null, name, throwOnError, ignoreCase);
  192. }
  193. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  194. internal extern static void InternalGetAssemblyName (string assemblyFile, AssemblyName aname);
  195. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  196. static extern void FillName (Assembly ass, AssemblyName aname);
  197. public virtual AssemblyName GetName (Boolean copiedName)
  198. {
  199. AssemblyName aname = new AssemblyName ();
  200. FillName (this, aname);
  201. return aname;
  202. }
  203. public virtual AssemblyName GetName ()
  204. {
  205. return GetName (false);
  206. }
  207. public override String ToString ()
  208. {
  209. return GetName ().Name;
  210. }
  211. [MonoTODO]
  212. public static String CreateQualifiedName (String assemblyName, String typeName)
  213. {
  214. return typeName + "," + assemblyName;
  215. }
  216. public static Assembly GetAssembly (Type type)
  217. {
  218. if (type != null)
  219. return type.Assembly;
  220. throw new ArgumentNullException ("type");
  221. }
  222. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  223. public static extern Assembly GetEntryAssembly();
  224. public Assembly GetSatelliteAssembly (CultureInfo culture)
  225. {
  226. return GetSatelliteAssembly (culture, null);
  227. }
  228. public Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
  229. {
  230. if (culture == null)
  231. throw new ArgumentException ("culture");
  232. AssemblyName aname = GetName (true);
  233. if (version != null)
  234. aname.Version = version;
  235. aname.CultureInfo = culture;
  236. aname.Name = aname.Name + ".resources";
  237. return Load (aname);
  238. }
  239. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  240. public extern static Assembly LoadFrom (String assemblyFile);
  241. [MonoTODO]
  242. public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence)
  243. {
  244. // Evidence is ignored
  245. return LoadFrom (assemblyFile);
  246. }
  247. #if NET_1_1
  248. [MonoTODO]
  249. public static Assembly LoadFile (String path, Evidence securityEvidence) {
  250. if (path == null)
  251. throw new ArgumentNullException ("path");
  252. if (path == String.Empty)
  253. throw new ArgumentException ("Path can't be empty", "path");
  254. // FIXME: Make this do the right thing
  255. return LoadFrom (path, securityEvidence);
  256. }
  257. public static Assembly LoadFile (String path) {
  258. return LoadFile (path, null);
  259. }
  260. #endif
  261. public static Assembly Load (String assemblyString)
  262. {
  263. return AppDomain.CurrentDomain.Load (assemblyString);
  264. }
  265. public static Assembly Load (String assemblyString, Evidence assemblySecurity)
  266. {
  267. return AppDomain.CurrentDomain.Load (assemblyString, assemblySecurity);
  268. }
  269. public static Assembly Load (AssemblyName assemblyRef)
  270. {
  271. return AppDomain.CurrentDomain.Load (assemblyRef);
  272. }
  273. public static Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
  274. {
  275. return AppDomain.CurrentDomain.Load (assemblyRef, assemblySecurity);
  276. }
  277. public static Assembly Load (Byte[] rawAssembly)
  278. {
  279. return AppDomain.CurrentDomain.Load (rawAssembly);
  280. }
  281. public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore)
  282. {
  283. return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
  284. }
  285. public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore,
  286. Evidence securityEvidence)
  287. {
  288. return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore, securityEvidence);
  289. }
  290. public static Assembly LoadWithPartialName (string partialName)
  291. {
  292. return LoadWithPartialName (partialName, null);
  293. }
  294. [MonoTODO]
  295. public Module LoadModule (string moduleName, byte [] rawModule)
  296. {
  297. throw new NotImplementedException ();
  298. }
  299. [MonoTODO]
  300. public Module LoadModule (string moduleName, byte [] rawModule, byte [] rawSymbolStore)
  301. {
  302. throw new NotImplementedException ();
  303. }
  304. [MonoTODO]
  305. public static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence)
  306. {
  307. try {
  308. return AppDomain.CurrentDomain.Load (partialName, securityEvidence);
  309. }
  310. catch (Exception) {
  311. // According to MSDN, this should return null instead of
  312. // throwing an exception
  313. return null;
  314. }
  315. }
  316. public Object CreateInstance (String typeName)
  317. {
  318. return CreateInstance (typeName, false);
  319. }
  320. public Object CreateInstance (String typeName, Boolean ignoreCase)
  321. {
  322. Type t = GetType (typeName, false, ignoreCase);
  323. if (t == null)
  324. return null;
  325. return Activator.CreateInstance (t);
  326. }
  327. public Object CreateInstance (String typeName, Boolean ignoreCase,
  328. BindingFlags bindingAttr, Binder binder,
  329. Object[] args, CultureInfo culture,
  330. Object[] activationAttributes)
  331. {
  332. Type t = GetType (typeName, false, ignoreCase);
  333. if (t == null)
  334. return null;
  335. return Activator.CreateInstance (t, bindingAttr, binder, args, culture, activationAttributes);
  336. }
  337. public Module[] GetLoadedModules ()
  338. {
  339. return GetLoadedModules (false);
  340. }
  341. [MonoTODO]
  342. public Module[] GetLoadedModules (bool getResourceModules)
  343. {
  344. // Currently, the two sets of modules are equal
  345. return GetModules (getResourceModules);
  346. }
  347. public Module[] GetModules ()
  348. {
  349. return GetModules (false);
  350. }
  351. public Module GetModule (String name)
  352. {
  353. if (name == null)
  354. throw new ArgumentNullException ("name");
  355. if (name == "")
  356. throw new ArgumentException ("Name can't be empty");
  357. Module[] modules = GetModules (true);
  358. foreach (Module module in GetModules (true)) {
  359. if (module.ScopeName == name)
  360. return module;
  361. }
  362. return null;
  363. }
  364. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  365. internal extern Module[] GetModulesInternal ();
  366. public Module[] GetModules (bool getResourceModules) {
  367. Module[] modules = GetModulesInternal ();
  368. if (!getResourceModules) {
  369. ArrayList result = new ArrayList (modules.Length);
  370. foreach (Module m in modules)
  371. if (!m.IsResource ())
  372. result.Add (m);
  373. return (Module[])result.ToArray (typeof (Module));
  374. }
  375. else
  376. return modules;
  377. }
  378. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  379. internal extern string[] GetNamespaces ();
  380. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  381. public extern virtual String[] GetManifestResourceNames ();
  382. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  383. public extern static Assembly GetExecutingAssembly ();
  384. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  385. public extern static Assembly GetCallingAssembly ();
  386. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  387. public extern AssemblyName[] GetReferencedAssemblies ();
  388. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  389. private extern bool GetManifestResourceInfoInternal (String name, ManifestResourceInfo info);
  390. public virtual ManifestResourceInfo GetManifestResourceInfo (String resourceName)
  391. {
  392. if (resourceName == null)
  393. throw new ArgumentNullException ("resourceName");
  394. if (resourceName == "")
  395. throw new ArgumentException ("String cannot have zero length.");
  396. ManifestResourceInfo result = new ManifestResourceInfo ();
  397. bool found = GetManifestResourceInfoInternal (resourceName, result);
  398. if (found)
  399. return result;
  400. else
  401. return null;
  402. }
  403. //
  404. // The following functions are only for the Mono Debugger.
  405. //
  406. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  407. internal static extern MethodBase MonoDebugger_GetMethod (Assembly assembly, int token);
  408. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  409. internal static extern int MonoDebugger_GetMethodToken (Assembly assembly, MethodBase method);
  410. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  411. internal static extern Type MonoDebugger_GetLocalTypeFromSignature (Assembly assembly, byte[] signature);
  412. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  413. internal static extern Type MonoDebugger_GetType (Assembly assembly, int token);
  414. }
  415. }