Assembly.cs 26 KB

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