Assembly.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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-2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Security;
  30. using System.Security.Policy;
  31. using System.Security.Permissions;
  32. using System.Runtime.Serialization;
  33. using System.Reflection;
  34. using System.Reflection.Emit;
  35. using System.IO;
  36. using System.Globalization;
  37. using System.Runtime.CompilerServices;
  38. using System.Runtime.InteropServices;
  39. using System.Collections;
  40. using System.Configuration.Assemblies;
  41. using Mono.Security;
  42. namespace System.Reflection {
  43. [ComVisible (true)]
  44. [ComDefaultInterfaceAttribute (typeof (_Assembly))]
  45. [Serializable]
  46. [ClassInterface(ClassInterfaceType.None)]
  47. #if NET_2_1
  48. public class Assembly : ICustomAttributeProvider, _Assembly {
  49. #else
  50. public class Assembly : ICustomAttributeProvider, _Assembly, IEvidenceFactory, ISerializable {
  51. #endif
  52. internal class ResolveEventHolder {
  53. public event ModuleResolveEventHandler ModuleResolve;
  54. }
  55. // Note: changes to fields must be reflected in _MonoReflectionAssembly struct (object-internals.h)
  56. #pragma warning disable 169
  57. private IntPtr _mono_assembly;
  58. #pragma warning restore 169
  59. private ResolveEventHolder resolve_event_holder;
  60. private Evidence _evidence;
  61. internal PermissionSet _minimum; // for SecurityAction.RequestMinimum
  62. internal PermissionSet _optional; // for SecurityAction.RequestOptional
  63. internal PermissionSet _refuse; // for SecurityAction.RequestRefuse
  64. private PermissionSet _granted; // for the resolved assembly granted permissions
  65. private PermissionSet _denied; // for the resolved assembly denied permissions
  66. private bool fromByteArray;
  67. private string assemblyName;
  68. internal Assembly ()
  69. {
  70. resolve_event_holder = new ResolveEventHolder ();
  71. }
  72. //
  73. // We can't store the event directly in this class, since the
  74. // compiler would silently insert the fields before _mono_assembly
  75. //
  76. public event ModuleResolveEventHandler ModuleResolve {
  77. [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
  78. add {
  79. resolve_event_holder.ModuleResolve += value;
  80. }
  81. [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
  82. remove {
  83. resolve_event_holder.ModuleResolve -= value;
  84. }
  85. }
  86. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  87. private extern string get_code_base (bool escaped);
  88. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  89. private extern string get_fullname ();
  90. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  91. private extern string get_location ();
  92. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  93. private extern string InternalImageRuntimeVersion ();
  94. // SECURITY: this should be the only caller to icall get_code_base
  95. private string GetCodeBase (bool escaped)
  96. {
  97. string cb = get_code_base (escaped);
  98. #if !NET_2_1
  99. if (SecurityManager.SecurityEnabled) {
  100. // we cannot divulge local file informations
  101. if (String.Compare ("FILE://", 0, cb, 0, 7, true, CultureInfo.InvariantCulture) == 0) {
  102. string file = cb.Substring (7);
  103. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, file).Demand ();
  104. }
  105. }
  106. #endif
  107. return cb;
  108. }
  109. public virtual string CodeBase {
  110. get { return GetCodeBase (false); }
  111. }
  112. public virtual string EscapedCodeBase {
  113. get { return GetCodeBase (true); }
  114. }
  115. public virtual string FullName {
  116. get {
  117. //
  118. // FIXME: This is wrong, but it gets us going
  119. // in the compiler for now
  120. //
  121. return ToString ();
  122. }
  123. }
  124. public virtual extern MethodInfo EntryPoint {
  125. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  126. get;
  127. }
  128. #if !NET_2_1 || MONOTOUCH
  129. public virtual Evidence Evidence {
  130. [SecurityPermission (SecurityAction.Demand, ControlEvidence = true)]
  131. get { return UnprotectedGetEvidence (); }
  132. }
  133. // note: the security runtime requires evidences but may be unable to do so...
  134. internal Evidence UnprotectedGetEvidence ()
  135. {
  136. // if the host (runtime) hasn't provided it's own evidence...
  137. if (_evidence == null) {
  138. // ... we will provide our own
  139. lock (this) {
  140. _evidence = Evidence.GetDefaultHostEvidence (this);
  141. }
  142. }
  143. return _evidence;
  144. }
  145. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  146. private extern bool get_global_assembly_cache ();
  147. public bool GlobalAssemblyCache {
  148. get {
  149. return get_global_assembly_cache ();
  150. }
  151. }
  152. #endif
  153. internal bool FromByteArray {
  154. set { fromByteArray = value; }
  155. }
  156. public virtual String Location {
  157. get {
  158. if (fromByteArray)
  159. return String.Empty;
  160. string loc = get_location ();
  161. #if !NET_2_1
  162. if ((loc != String.Empty) && SecurityManager.SecurityEnabled) {
  163. // we cannot divulge local file informations
  164. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, loc).Demand ();
  165. }
  166. #endif
  167. return loc;
  168. }
  169. }
  170. [ComVisible (false)]
  171. public virtual string ImageRuntimeVersion {
  172. get {
  173. return InternalImageRuntimeVersion ();
  174. }
  175. }
  176. [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
  177. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  178. {
  179. if (info == null)
  180. throw new ArgumentNullException ("info");
  181. UnitySerializationHolder.GetAssemblyData (this, info, context);
  182. }
  183. public virtual bool IsDefined (Type attributeType, bool inherit)
  184. {
  185. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  186. }
  187. public virtual object [] GetCustomAttributes (bool inherit)
  188. {
  189. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  190. }
  191. public virtual object [] GetCustomAttributes (Type attributeType, bool inherit)
  192. {
  193. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  194. }
  195. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  196. private extern object GetFilesInternal (String name, bool getResourceModules);
  197. public virtual FileStream[] GetFiles ()
  198. {
  199. return GetFiles (false);
  200. }
  201. public virtual FileStream [] GetFiles (bool getResourceModules)
  202. {
  203. string[] names = (string[]) GetFilesInternal (null, getResourceModules);
  204. if (names == null)
  205. return new FileStream [0];
  206. string location = Location;
  207. FileStream[] res;
  208. if (location != String.Empty) {
  209. res = new FileStream [names.Length + 1];
  210. res [0] = new FileStream (location, FileMode.Open, FileAccess.Read);
  211. for (int i = 0; i < names.Length; ++i)
  212. res [i + 1] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
  213. } else {
  214. res = new FileStream [names.Length];
  215. for (int i = 0; i < names.Length; ++i)
  216. res [i] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
  217. }
  218. return res;
  219. }
  220. public virtual FileStream GetFile (String name)
  221. {
  222. if (name == null)
  223. throw new ArgumentNullException (null, "Name cannot be null.");
  224. if (name.Length == 0)
  225. throw new ArgumentException ("Empty name is not valid");
  226. string filename = (string)GetFilesInternal (name, true);
  227. if (filename != null)
  228. return new FileStream (filename, FileMode.Open, FileAccess.Read);
  229. else
  230. return null;
  231. }
  232. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  233. internal extern IntPtr GetManifestResourceInternal (String name, out int size, out Module module);
  234. public virtual Stream GetManifestResourceStream (String name)
  235. {
  236. if (name == null)
  237. throw new ArgumentNullException ("name");
  238. if (name.Length == 0)
  239. throw new ArgumentException ("String cannot have zero length.",
  240. "name");
  241. ManifestResourceInfo info = GetManifestResourceInfo (name);
  242. if (info == null)
  243. return null;
  244. if (info.ReferencedAssembly != null)
  245. return info.ReferencedAssembly.GetManifestResourceStream (name);
  246. if ((info.FileName != null) && (info.ResourceLocation == 0)) {
  247. if (fromByteArray)
  248. throw new FileNotFoundException (info.FileName);
  249. string location = Path.GetDirectoryName (Location);
  250. string filename = Path.Combine (location, info.FileName);
  251. #if NET_2_1 && !MONOTOUCH
  252. // we don't control the content of 'info.FileName' so we want to make sure we keep to ourselves
  253. filename = Path.GetFullPath (filename);
  254. if (!filename.StartsWith (location))
  255. throw new SecurityException ("non-rooted access to manifest resource");
  256. #endif
  257. return new FileStream (filename, FileMode.Open, FileAccess.Read);
  258. }
  259. int size;
  260. Module module;
  261. IntPtr data = GetManifestResourceInternal (name, out size, out module);
  262. if (data == (IntPtr) 0)
  263. return null;
  264. else {
  265. UnmanagedMemoryStream stream;
  266. unsafe {
  267. stream = new UnmanagedMemoryStream ((byte*) data, size);
  268. }
  269. /*
  270. * The returned pointer points inside metadata, so
  271. * we have to increase the refcount of the module, and decrease
  272. * it when the stream is finalized.
  273. */
  274. stream.Closed += new EventHandler (new ResourceCloseHandler (module).OnClose);
  275. return stream;
  276. }
  277. }
  278. public virtual Stream GetManifestResourceStream (Type type, String name)
  279. {
  280. string ns;
  281. if (type != null) {
  282. ns = type.Namespace;
  283. } else {
  284. if (name == null)
  285. throw new ArgumentNullException ("type");
  286. ns = null;
  287. }
  288. if (ns == null || ns.Length == 0)
  289. return GetManifestResourceStream (name);
  290. else
  291. return GetManifestResourceStream (ns + "." + name);
  292. }
  293. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  294. internal virtual extern Type[] GetTypes (bool exportedOnly);
  295. public virtual Type[] GetTypes ()
  296. {
  297. return GetTypes (false);
  298. }
  299. public virtual Type[] GetExportedTypes ()
  300. {
  301. return GetTypes (true);
  302. }
  303. public virtual Type GetType (String name, Boolean throwOnError)
  304. {
  305. return GetType (name, throwOnError, false);
  306. }
  307. public virtual Type GetType (String name) {
  308. return GetType (name, false, false);
  309. }
  310. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  311. internal extern Type InternalGetType (Module module, String name, Boolean throwOnError, Boolean ignoreCase);
  312. public Type GetType (string name, bool throwOnError, bool ignoreCase)
  313. {
  314. if (name == null)
  315. throw new ArgumentNullException (name);
  316. if (name.Length == 0)
  317. throw new ArgumentException ("name", "Name cannot be empty");
  318. return InternalGetType (null, name, throwOnError, ignoreCase);
  319. }
  320. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  321. internal extern static void InternalGetAssemblyName (string assemblyFile, AssemblyName aname);
  322. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  323. static extern void FillName (Assembly ass, AssemblyName aname);
  324. [MonoTODO ("copiedName == true is not supported")]
  325. public virtual AssemblyName GetName (Boolean copiedName)
  326. {
  327. // CodeBase, which is restricted, will be copied into the AssemblyName object so...
  328. if (SecurityManager.SecurityEnabled) {
  329. GetCodeBase (true); // this will ensure the Demand is made
  330. }
  331. return UnprotectedGetName ();
  332. }
  333. public virtual AssemblyName GetName ()
  334. {
  335. return GetName (false);
  336. }
  337. // the security runtime requires access to the assemblyname (e.g. to get the strongname)
  338. internal virtual AssemblyName UnprotectedGetName ()
  339. {
  340. AssemblyName aname = new AssemblyName ();
  341. FillName (this, aname);
  342. return aname;
  343. }
  344. public override string ToString ()
  345. {
  346. // note: ToString work without requiring CodeBase (so no checks are needed)
  347. if (assemblyName != null)
  348. return assemblyName;
  349. assemblyName = get_fullname ();
  350. return assemblyName;
  351. }
  352. public static String CreateQualifiedName (String assemblyName, String typeName)
  353. {
  354. return typeName + ", " + assemblyName;
  355. }
  356. public static Assembly GetAssembly (Type type)
  357. {
  358. if (type != null)
  359. return type.Assembly;
  360. throw new ArgumentNullException ("type");
  361. }
  362. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  363. public static extern Assembly GetEntryAssembly();
  364. public Assembly GetSatelliteAssembly (CultureInfo culture)
  365. {
  366. return GetSatelliteAssembly (culture, null, true);
  367. }
  368. public Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
  369. {
  370. return GetSatelliteAssembly (culture, version, true);
  371. }
  372. internal Assembly GetSatelliteAssemblyNoThrow (CultureInfo culture, Version version)
  373. {
  374. return GetSatelliteAssembly (culture, version, false);
  375. }
  376. private Assembly GetSatelliteAssembly (CultureInfo culture, Version version, bool throwOnError)
  377. {
  378. if (culture == null)
  379. throw new ArgumentException ("culture");
  380. AssemblyName aname = GetName (true);
  381. if (version != null)
  382. aname.Version = version;
  383. aname.CultureInfo = culture;
  384. aname.Name = aname.Name + ".resources";
  385. Assembly assembly;
  386. try {
  387. assembly = AppDomain.CurrentDomain.LoadSatellite (aname);
  388. if (assembly != null)
  389. return assembly;
  390. } catch (FileNotFoundException) {
  391. assembly = null;
  392. // ignore
  393. }
  394. // Try the assembly directory
  395. string location = Path.GetDirectoryName (Location);
  396. string fullName = Path.Combine (location, Path.Combine (culture.Name, aname.Name + ".dll"));
  397. #if NET_2_1 && !MONOTOUCH
  398. // it's unlikely that culture.Name or aname.Name could contain stuff like ".." but...
  399. fullName = Path.GetFullPath (fullName);
  400. if (!fullName.StartsWith (location)) {
  401. if (throwOnError)
  402. throw new SecurityException ("non-rooted access to satellite assembly");
  403. return null;
  404. }
  405. #endif
  406. if (!throwOnError && !File.Exists (fullName))
  407. return null;
  408. return LoadFrom (fullName);
  409. }
  410. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  411. private extern static Assembly LoadFrom (String assemblyFile, bool refonly);
  412. public static Assembly LoadFrom (String assemblyFile)
  413. {
  414. return LoadFrom (assemblyFile, false);
  415. }
  416. public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence)
  417. {
  418. Assembly a = LoadFrom (assemblyFile, false);
  419. #if !NET_2_1
  420. if ((a != null) && (securityEvidence != null)) {
  421. // merge evidence (i.e. replace defaults with provided evidences)
  422. a.Evidence.Merge (securityEvidence);
  423. }
  424. #endif
  425. return a;
  426. }
  427. [MonoTODO("This overload is not currently implemented")]
  428. // FIXME: What are we missing?
  429. public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
  430. {
  431. if (assemblyFile == null)
  432. throw new ArgumentNullException ("assemblyFile");
  433. if (assemblyFile == String.Empty)
  434. throw new ArgumentException ("Name can't be the empty string", "assemblyFile");
  435. throw new NotImplementedException ();
  436. }
  437. public static Assembly LoadFile (String path, Evidence securityEvidence)
  438. {
  439. if (path == null)
  440. throw new ArgumentNullException ("path");
  441. if (path == String.Empty)
  442. throw new ArgumentException ("Path can't be empty", "path");
  443. // FIXME: Make this do the right thing
  444. return LoadFrom (path, securityEvidence);
  445. }
  446. public static Assembly LoadFile (String path)
  447. {
  448. return LoadFile (path, null);
  449. }
  450. public static Assembly Load (String assemblyString)
  451. {
  452. return AppDomain.CurrentDomain.Load (assemblyString);
  453. }
  454. public static Assembly Load (String assemblyString, Evidence assemblySecurity)
  455. {
  456. return AppDomain.CurrentDomain.Load (assemblyString, assemblySecurity);
  457. }
  458. public static Assembly Load (AssemblyName assemblyRef)
  459. {
  460. return AppDomain.CurrentDomain.Load (assemblyRef);
  461. }
  462. public static Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
  463. {
  464. return AppDomain.CurrentDomain.Load (assemblyRef, assemblySecurity);
  465. }
  466. public static Assembly Load (Byte[] rawAssembly)
  467. {
  468. return AppDomain.CurrentDomain.Load (rawAssembly);
  469. }
  470. public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore)
  471. {
  472. return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
  473. }
  474. public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore,
  475. Evidence securityEvidence)
  476. {
  477. return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore, securityEvidence);
  478. }
  479. public static Assembly ReflectionOnlyLoad (byte[] rawAssembly)
  480. {
  481. return AppDomain.CurrentDomain.Load (rawAssembly, null, null, true);
  482. }
  483. public static Assembly ReflectionOnlyLoad (string assemblyString)
  484. {
  485. return AppDomain.CurrentDomain.Load (assemblyString, null, true);
  486. }
  487. public static Assembly ReflectionOnlyLoadFrom (string assemblyFile)
  488. {
  489. if (assemblyFile == null)
  490. throw new ArgumentNullException ("assemblyFile");
  491. return LoadFrom (assemblyFile, true);
  492. }
  493. public static Assembly LoadWithPartialName (string partialName)
  494. {
  495. return LoadWithPartialName (partialName, null);
  496. }
  497. [MonoTODO ("Not implemented")]
  498. public Module LoadModule (string moduleName, byte [] rawModule)
  499. {
  500. throw new NotImplementedException ();
  501. }
  502. [MonoTODO ("Not implemented")]
  503. public Module LoadModule (string moduleName, byte [] rawModule, byte [] rawSymbolStore)
  504. {
  505. throw new NotImplementedException ();
  506. }
  507. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  508. private static extern Assembly load_with_partial_name (string name, Evidence e);
  509. public static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence)
  510. {
  511. return LoadWithPartialName (partialName, securityEvidence, true);
  512. }
  513. /**
  514. * LAMESPEC: It is possible for this method to throw exceptions IF the name supplied
  515. * is a valid gac name and contains filesystem entry charachters at the end of the name
  516. * ie System/// will throw an exception. However ////System will not as that is canocolized
  517. * out of the name.
  518. */
  519. // FIXME: LoadWithPartialName must look cache (no CAS) or read from disk (CAS)
  520. internal static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence, bool oldBehavior)
  521. {
  522. if (!oldBehavior)
  523. throw new NotImplementedException ();
  524. if (partialName == null)
  525. throw new NullReferenceException ();
  526. return load_with_partial_name (partialName, securityEvidence);
  527. }
  528. public Object CreateInstance (String typeName)
  529. {
  530. return CreateInstance (typeName, false);
  531. }
  532. public Object CreateInstance (String typeName, Boolean ignoreCase)
  533. {
  534. Type t = GetType (typeName, false, ignoreCase);
  535. if (t == null)
  536. return null;
  537. try {
  538. return Activator.CreateInstance (t);
  539. } catch (InvalidOperationException) {
  540. throw new ArgumentException ("It is illegal to invoke a method on a Type loaded via ReflectionOnly methods.");
  541. }
  542. }
  543. public Object CreateInstance (String typeName, Boolean ignoreCase,
  544. BindingFlags bindingAttr, Binder binder,
  545. Object[] args, CultureInfo culture,
  546. Object[] activationAttributes)
  547. {
  548. Type t = GetType (typeName, false, ignoreCase);
  549. if (t == null)
  550. return null;
  551. try {
  552. return Activator.CreateInstance (t, bindingAttr, binder, args, culture, activationAttributes);
  553. } catch (InvalidOperationException) {
  554. throw new ArgumentException ("It is illegal to invoke a method on a Type loaded via ReflectionOnly methods.");
  555. }
  556. }
  557. public Module[] GetLoadedModules ()
  558. {
  559. return GetLoadedModules (false);
  560. }
  561. // FIXME: Currently, the two sets of modules are equal
  562. public Module[] GetLoadedModules (bool getResourceModules)
  563. {
  564. return GetModules (getResourceModules);
  565. }
  566. public Module[] GetModules ()
  567. {
  568. return GetModules (false);
  569. }
  570. public Module GetModule (String name)
  571. {
  572. if (name == null)
  573. throw new ArgumentNullException ("name");
  574. if (name.Length == 0)
  575. throw new ArgumentException ("Name can't be empty");
  576. Module[] modules = GetModules (true);
  577. foreach (Module module in modules) {
  578. if (module.ScopeName == name)
  579. return module;
  580. }
  581. return null;
  582. }
  583. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  584. internal virtual extern Module[] GetModulesInternal ();
  585. public Module[] GetModules (bool getResourceModules) {
  586. Module[] modules = GetModulesInternal ();
  587. if (!getResourceModules) {
  588. ArrayList result = new ArrayList (modules.Length);
  589. foreach (Module m in modules)
  590. if (!m.IsResource ())
  591. result.Add (m);
  592. return (Module[])result.ToArray (typeof (Module));
  593. }
  594. else
  595. return modules;
  596. }
  597. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  598. internal extern string[] GetNamespaces ();
  599. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  600. public extern virtual String[] GetManifestResourceNames ();
  601. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  602. public extern static Assembly GetExecutingAssembly ();
  603. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  604. public extern static Assembly GetCallingAssembly ();
  605. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  606. public extern AssemblyName[] GetReferencedAssemblies ();
  607. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  608. private extern bool GetManifestResourceInfoInternal (String name, ManifestResourceInfo info);
  609. public virtual ManifestResourceInfo GetManifestResourceInfo (String resourceName)
  610. {
  611. if (resourceName == null)
  612. throw new ArgumentNullException ("resourceName");
  613. if (resourceName.Length == 0)
  614. throw new ArgumentException ("String cannot have zero length.");
  615. ManifestResourceInfo result = new ManifestResourceInfo ();
  616. bool found = GetManifestResourceInfoInternal (resourceName, result);
  617. if (found)
  618. return result;
  619. else
  620. return null;
  621. }
  622. private class ResourceCloseHandler {
  623. #pragma warning disable 169, 414
  624. Module module;
  625. #pragma warning restore 169, 414
  626. public ResourceCloseHandler (Module module) {
  627. this.module = module;
  628. }
  629. public void OnClose (object sender, EventArgs e) {
  630. // The module dtor will take care of things
  631. module = null;
  632. }
  633. }
  634. //
  635. // The following functions are only for the Mono Debugger.
  636. //
  637. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  638. internal static extern int MonoDebugger_GetMethodToken (MethodBase method);
  639. [MonoTODO ("Currently it always returns zero")]
  640. [ComVisible (false)]
  641. public long HostContext {
  642. get { return 0; }
  643. }
  644. [ComVisible (false)]
  645. public Module ManifestModule {
  646. get {
  647. return GetManifestModule ();
  648. }
  649. }
  650. internal virtual Module GetManifestModule () {
  651. return GetManifestModuleInternal ();
  652. }
  653. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  654. internal extern Module GetManifestModuleInternal ();
  655. [ComVisible (false)]
  656. public virtual extern bool ReflectionOnly {
  657. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  658. get;
  659. }
  660. public override bool Equals (object o)
  661. {
  662. if (((object) this) == o)
  663. return true;
  664. if (o == null)
  665. return false;
  666. Assembly other = (Assembly) o;
  667. return other._mono_assembly == _mono_assembly;
  668. }
  669. #if !NET_2_1 || MONOTOUCH
  670. // Code Access Security
  671. internal void Resolve ()
  672. {
  673. lock (this) {
  674. // FIXME: As we (currently) delay the resolution until the first CAS
  675. // Demand it's too late to evaluate the Minimum permission set as a
  676. // condition to load the assembly into the AppDomain
  677. LoadAssemblyPermissions ();
  678. Evidence e = new Evidence (UnprotectedGetEvidence ()); // we need a copy to add PRE
  679. e.AddHost (new PermissionRequestEvidence (_minimum, _optional, _refuse));
  680. _granted = SecurityManager.ResolvePolicy (e,
  681. _minimum, _optional, _refuse, out _denied);
  682. }
  683. }
  684. internal PermissionSet GrantedPermissionSet {
  685. get {
  686. if (_granted == null) {
  687. if (SecurityManager.ResolvingPolicyLevel != null) {
  688. if (SecurityManager.ResolvingPolicyLevel.IsFullTrustAssembly (this))
  689. return DefaultPolicies.FullTrust;
  690. else
  691. return null; // we can't resolve during resolution
  692. }
  693. Resolve ();
  694. }
  695. return _granted;
  696. }
  697. }
  698. internal PermissionSet DeniedPermissionSet {
  699. get {
  700. // yes we look for granted, as denied may be null
  701. if (_granted == null) {
  702. if (SecurityManager.ResolvingPolicyLevel != null) {
  703. if (SecurityManager.ResolvingPolicyLevel.IsFullTrustAssembly (this))
  704. return null;
  705. else
  706. return DefaultPolicies.FullTrust; // deny unrestricted
  707. }
  708. Resolve ();
  709. }
  710. return _denied;
  711. }
  712. }
  713. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  714. extern internal static bool LoadPermissions (Assembly a,
  715. ref IntPtr minimum, ref int minLength,
  716. ref IntPtr optional, ref int optLength,
  717. ref IntPtr refused, ref int refLength);
  718. // Support for SecurityAction.RequestMinimum, RequestOptional and RequestRefuse
  719. private void LoadAssemblyPermissions ()
  720. {
  721. IntPtr minimum = IntPtr.Zero, optional = IntPtr.Zero, refused = IntPtr.Zero;
  722. int minLength = 0, optLength = 0, refLength = 0;
  723. if (LoadPermissions (this, ref minimum, ref minLength, ref optional,
  724. ref optLength, ref refused, ref refLength)) {
  725. // Note: no need to cache these permission sets as they will only be created once
  726. // at assembly resolution time.
  727. if (minLength > 0) {
  728. byte[] data = new byte [minLength];
  729. Marshal.Copy (minimum, data, 0, minLength);
  730. _minimum = SecurityManager.Decode (data);
  731. }
  732. if (optLength > 0) {
  733. byte[] data = new byte [optLength];
  734. Marshal.Copy (optional, data, 0, optLength);
  735. _optional = SecurityManager.Decode (data);
  736. }
  737. if (refLength > 0) {
  738. byte[] data = new byte [refLength];
  739. Marshal.Copy (refused, data, 0, refLength);
  740. _refuse = SecurityManager.Decode (data);
  741. }
  742. }
  743. }
  744. #endif
  745. }
  746. }