Assembly.cs 12 KB

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