Assembly.cs 26 KB

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