Assembly.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. throw new NotImplementedException ();
  245. }
  246. public static Assembly Load (String assemblyString)
  247. {
  248. return AppDomain.CurrentDomain.Load (assemblyString);
  249. }
  250. public static Assembly Load (String assemblyString, Evidence assemblySecurity)
  251. {
  252. return AppDomain.CurrentDomain.Load (assemblyString, assemblySecurity);
  253. }
  254. public static Assembly Load (AssemblyName assemblyRef)
  255. {
  256. return AppDomain.CurrentDomain.Load (assemblyRef);
  257. }
  258. public static Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
  259. {
  260. return AppDomain.CurrentDomain.Load (assemblyRef, assemblySecurity);
  261. }
  262. public static Assembly Load (Byte[] rawAssembly)
  263. {
  264. return AppDomain.CurrentDomain.Load (rawAssembly);
  265. }
  266. public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore)
  267. {
  268. return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
  269. }
  270. public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore,
  271. Evidence securityEvidence)
  272. {
  273. return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore, securityEvidence);
  274. }
  275. public static Assembly LoadWithPartialName (string partialName)
  276. {
  277. return LoadWithPartialName (partialName, null);
  278. }
  279. [MonoTODO]
  280. public Module LoadModule (string moduleName, byte [] rawModule)
  281. {
  282. throw new NotImplementedException ();
  283. }
  284. [MonoTODO]
  285. public Module LoadModule (string moduleName, byte [] rawModule, byte [] rawSymbolStore)
  286. {
  287. throw new NotImplementedException ();
  288. }
  289. [MonoTODO]
  290. public static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence)
  291. {
  292. try {
  293. return AppDomain.CurrentDomain.Load (partialName, securityEvidence);
  294. }
  295. catch (Exception ex) {
  296. // According to MSDN, this should return null instead of
  297. // throwing an exception
  298. return null;
  299. }
  300. }
  301. public Object CreateInstance (String typeName)
  302. {
  303. return CreateInstance (typeName, false);
  304. }
  305. public Object CreateInstance (String typeName, Boolean ignoreCase)
  306. {
  307. Type t = GetType (typeName, false, ignoreCase);
  308. if (t == null)
  309. return null;
  310. return Activator.CreateInstance (t);
  311. }
  312. public Object CreateInstance (String typeName, Boolean ignoreCase,
  313. BindingFlags bindingAttr, Binder binder,
  314. Object[] args, CultureInfo culture,
  315. Object[] activationAttributes)
  316. {
  317. Type t = GetType (typeName, false, ignoreCase);
  318. if (t == null)
  319. return null;
  320. return Activator.CreateInstance (t, bindingAttr, binder, args, culture, activationAttributes);
  321. }
  322. public Module[] GetLoadedModules ()
  323. {
  324. return GetLoadedModules (false);
  325. }
  326. [MonoTODO]
  327. public Module[] GetLoadedModules (bool getResourceModules)
  328. {
  329. // Currently, the two sets of modules are equal
  330. return GetModules (getResourceModules);
  331. }
  332. public Module[] GetModules ()
  333. {
  334. return GetModules (false);
  335. }
  336. public Module GetModule (String name)
  337. {
  338. if (name == null)
  339. throw new ArgumentNullException ("name");
  340. if (name == "")
  341. throw new ArgumentException ("Name can't be empty");
  342. Module[] modules = GetModules (true);
  343. foreach (Module module in GetModules (true)) {
  344. if (module.ScopeName == name)
  345. return module;
  346. }
  347. return null;
  348. }
  349. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  350. internal extern Module[] GetModulesInternal ();
  351. public Module[] GetModules (bool getResourceModules) {
  352. Module[] modules = GetModulesInternal ();
  353. if (!getResourceModules) {
  354. ArrayList result = new ArrayList (modules.Length);
  355. foreach (Module m in modules)
  356. if (!m.IsResource ())
  357. result.Add (m);
  358. return (Module[])result.ToArray (typeof (Module));
  359. }
  360. else
  361. return modules;
  362. }
  363. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  364. internal extern string[] GetNamespaces ();
  365. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  366. public extern virtual String[] GetManifestResourceNames ();
  367. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  368. public extern static Assembly GetExecutingAssembly ();
  369. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  370. public extern static Assembly GetCallingAssembly ();
  371. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  372. public extern AssemblyName[] GetReferencedAssemblies ();
  373. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  374. private extern bool GetManifestResourceInfoInternal (String name, ManifestResourceInfo info);
  375. public virtual ManifestResourceInfo GetManifestResourceInfo (String resourceName)
  376. {
  377. if (resourceName == null)
  378. throw new ArgumentNullException ("resourceName");
  379. if (resourceName == "")
  380. throw new ArgumentException ("String cannot have zero length.");
  381. ManifestResourceInfo result = new ManifestResourceInfo ();
  382. bool found = GetManifestResourceInfoInternal (resourceName, result);
  383. if (found)
  384. return result;
  385. else
  386. return null;
  387. }
  388. //
  389. // The following functions are only for the Mono Debugger.
  390. //
  391. // They should be marked `internal', and extracted with GetMethod from the debugger.
  392. //
  393. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  394. public extern MethodBase MonoDebugger_GetMethod (int token);
  395. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  396. public extern int MonoDebugger_GetMethodToken (MethodBase method);
  397. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  398. public extern Type MonoDebugger_GetLocalTypeFromSignature (byte[] signature);
  399. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  400. public extern Type MonoDebugger_GetType (int token);
  401. }
  402. }