Assembly.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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.Collections.Generic;
  41. using System.Configuration.Assemblies;
  42. using Mono.Security;
  43. namespace System.Reflection {
  44. #pragma warning disable 659 // overrides Equals but not GetHashCode
  45. [ComVisible (true)]
  46. [ComDefaultInterfaceAttribute (typeof (_Assembly))]
  47. [Serializable]
  48. [ClassInterface(ClassInterfaceType.None)]
  49. #if NET_2_1
  50. public class Assembly : ICustomAttributeProvider, _Assembly {
  51. #else
  52. public class Assembly : ICustomAttributeProvider, _Assembly, IEvidenceFactory, ISerializable {
  53. #endif
  54. internal class ResolveEventHolder {
  55. public event ModuleResolveEventHandler ModuleResolve;
  56. }
  57. // Note: changes to fields must be reflected in _MonoReflectionAssembly struct (object-internals.h)
  58. #pragma warning disable 649
  59. private IntPtr _mono_assembly;
  60. #pragma warning restore 649
  61. private ResolveEventHolder resolve_event_holder;
  62. private Evidence _evidence;
  63. internal PermissionSet _minimum; // for SecurityAction.RequestMinimum
  64. internal PermissionSet _optional; // for SecurityAction.RequestOptional
  65. internal PermissionSet _refuse; // for SecurityAction.RequestRefuse
  66. private PermissionSet _granted; // for the resolved assembly granted permissions
  67. private PermissionSet _denied; // for the resolved assembly denied permissions
  68. private bool fromByteArray;
  69. private string assemblyName;
  70. internal Assembly ()
  71. {
  72. resolve_event_holder = new ResolveEventHolder ();
  73. }
  74. //
  75. // We can't store the event directly in this class, since the
  76. // compiler would silently insert the fields before _mono_assembly
  77. //
  78. public event ModuleResolveEventHandler ModuleResolve {
  79. [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
  80. add {
  81. resolve_event_holder.ModuleResolve += value;
  82. }
  83. [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
  84. remove {
  85. resolve_event_holder.ModuleResolve -= value;
  86. }
  87. }
  88. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  89. private extern string get_code_base (bool escaped);
  90. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  91. private extern string get_fullname ();
  92. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  93. private extern string get_location ();
  94. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  95. private extern string InternalImageRuntimeVersion ();
  96. // SECURITY: this should be the only caller to icall get_code_base
  97. private string GetCodeBase (bool escaped)
  98. {
  99. string cb = get_code_base (escaped);
  100. #if !NET_2_1
  101. if (SecurityManager.SecurityEnabled) {
  102. // we cannot divulge local file informations
  103. if (String.Compare ("FILE://", 0, cb, 0, 7, true, CultureInfo.InvariantCulture) == 0) {
  104. string file = cb.Substring (7);
  105. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, file).Demand ();
  106. }
  107. }
  108. #endif
  109. return cb;
  110. }
  111. public virtual string CodeBase {
  112. get { return GetCodeBase (false); }
  113. }
  114. public virtual string EscapedCodeBase {
  115. get { return GetCodeBase (true); }
  116. }
  117. public virtual string FullName {
  118. get {
  119. //
  120. // FIXME: This is wrong, but it gets us going
  121. // in the compiler for now
  122. //
  123. return ToString ();
  124. }
  125. }
  126. public virtual extern MethodInfo EntryPoint {
  127. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  128. get;
  129. }
  130. #if !MOONLIGHT
  131. public virtual Evidence Evidence {
  132. [SecurityPermission (SecurityAction.Demand, ControlEvidence = true)]
  133. get { return UnprotectedGetEvidence (); }
  134. }
  135. // note: the security runtime requires evidences but may be unable to do so...
  136. internal Evidence UnprotectedGetEvidence ()
  137. {
  138. // if the host (runtime) hasn't provided it's own evidence...
  139. if (_evidence == null) {
  140. // ... we will provide our own
  141. lock (this) {
  142. _evidence = Evidence.GetDefaultHostEvidence (this);
  143. }
  144. }
  145. return _evidence;
  146. }
  147. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  148. private extern bool get_global_assembly_cache ();
  149. public bool GlobalAssemblyCache {
  150. get {
  151. return get_global_assembly_cache ();
  152. }
  153. }
  154. #endif
  155. internal bool FromByteArray {
  156. set { fromByteArray = value; }
  157. }
  158. public virtual String Location {
  159. get {
  160. if (fromByteArray)
  161. return String.Empty;
  162. string loc = get_location ();
  163. #if !NET_2_1
  164. if ((loc != String.Empty) && SecurityManager.SecurityEnabled) {
  165. // we cannot divulge local file informations
  166. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, loc).Demand ();
  167. }
  168. #endif
  169. return loc;
  170. }
  171. }
  172. [ComVisible (false)]
  173. public virtual string ImageRuntimeVersion {
  174. get {
  175. return InternalImageRuntimeVersion ();
  176. }
  177. }
  178. [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
  179. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  180. {
  181. if (info == null)
  182. throw new ArgumentNullException ("info");
  183. UnitySerializationHolder.GetAssemblyData (this, info, context);
  184. }
  185. public virtual bool IsDefined (Type attributeType, bool inherit)
  186. {
  187. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  188. }
  189. public virtual object [] GetCustomAttributes (bool inherit)
  190. {
  191. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  192. }
  193. public virtual object [] GetCustomAttributes (Type attributeType, bool inherit)
  194. {
  195. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  196. }
  197. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  198. private extern object GetFilesInternal (String name, bool getResourceModules);
  199. public virtual FileStream[] GetFiles ()
  200. {
  201. return GetFiles (false);
  202. }
  203. public virtual FileStream [] GetFiles (bool getResourceModules)
  204. {
  205. string[] names = (string[]) GetFilesInternal (null, getResourceModules);
  206. if (names == null)
  207. return new FileStream [0];
  208. string location = Location;
  209. FileStream[] res;
  210. if (location != String.Empty) {
  211. res = new FileStream [names.Length + 1];
  212. res [0] = new FileStream (location, FileMode.Open, FileAccess.Read);
  213. for (int i = 0; i < names.Length; ++i)
  214. res [i + 1] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
  215. } else {
  216. res = new FileStream [names.Length];
  217. for (int i = 0; i < names.Length; ++i)
  218. res [i] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
  219. }
  220. return res;
  221. }
  222. public virtual FileStream GetFile (String name)
  223. {
  224. if (name == null)
  225. throw new ArgumentNullException (null, "Name cannot be null.");
  226. if (name.Length == 0)
  227. throw new ArgumentException ("Empty name is not valid");
  228. string filename = (string)GetFilesInternal (name, true);
  229. if (filename != null)
  230. return new FileStream (filename, FileMode.Open, FileAccess.Read);
  231. else
  232. return null;
  233. }
  234. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  235. internal extern IntPtr GetManifestResourceInternal (String name, out int size, out Module module);
  236. public virtual Stream GetManifestResourceStream (String name)
  237. {
  238. if (name == null)
  239. throw new ArgumentNullException ("name");
  240. if (name.Length == 0)
  241. throw new ArgumentException ("String cannot have zero length.",
  242. "name");
  243. ManifestResourceInfo info = GetManifestResourceInfo (name);
  244. if (info == null)
  245. return null;
  246. if (info.ReferencedAssembly != null)
  247. return info.ReferencedAssembly.GetManifestResourceStream (name);
  248. if ((info.FileName != null) && (info.ResourceLocation == 0)) {
  249. if (fromByteArray)
  250. throw new FileNotFoundException (info.FileName);
  251. string location = Path.GetDirectoryName (Location);
  252. string filename = Path.Combine (location, info.FileName);
  253. #if MOONLIGHT
  254. // we don't control the content of 'info.FileName' so we want to make sure we keep to ourselves
  255. filename = Path.GetFullPath (filename);
  256. if (!filename.StartsWith (location))
  257. throw new SecurityException ("non-rooted access to manifest resource");
  258. #endif
  259. return new FileStream (filename, FileMode.Open, FileAccess.Read);
  260. }
  261. int size;
  262. Module module;
  263. IntPtr data = GetManifestResourceInternal (name, out size, out module);
  264. if (data == (IntPtr) 0)
  265. return null;
  266. else {
  267. UnmanagedMemoryStream stream;
  268. unsafe {
  269. stream = new UnmanagedMemoryStream ((byte*) data, size);
  270. }
  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, true);
  369. }
  370. public Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
  371. {
  372. return GetSatelliteAssembly (culture, version, true);
  373. }
  374. internal Assembly GetSatelliteAssemblyNoThrow (CultureInfo culture, Version version)
  375. {
  376. return GetSatelliteAssembly (culture, version, false);
  377. }
  378. private Assembly GetSatelliteAssembly (CultureInfo culture, Version version, bool throwOnError)
  379. {
  380. if (culture == null)
  381. throw new ArgumentException ("culture");
  382. AssemblyName aname = GetName (true);
  383. if (version != null)
  384. aname.Version = version;
  385. aname.CultureInfo = culture;
  386. aname.Name = aname.Name + ".resources";
  387. Assembly assembly;
  388. try {
  389. assembly = AppDomain.CurrentDomain.LoadSatellite (aname, false);
  390. if (assembly != null)
  391. return assembly;
  392. } catch (FileNotFoundException) {
  393. assembly = null;
  394. // ignore
  395. }
  396. // Try the assembly directory
  397. string location = Path.GetDirectoryName (Location);
  398. string fullName = Path.Combine (location, Path.Combine (culture.Name, aname.Name + ".dll"));
  399. #if MOONLIGHT
  400. // it's unlikely that culture.Name or aname.Name could contain stuff like ".." but...
  401. fullName = Path.GetFullPath (fullName);
  402. if (!fullName.StartsWith (location)) {
  403. if (throwOnError)
  404. throw new SecurityException ("non-rooted access to satellite assembly");
  405. return null;
  406. }
  407. #endif
  408. if (!throwOnError && !File.Exists (fullName))
  409. return null;
  410. return LoadFrom (fullName);
  411. }
  412. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  413. private extern static Assembly LoadFrom (String assemblyFile, bool refonly);
  414. public static Assembly LoadFrom (String assemblyFile)
  415. {
  416. return LoadFrom (assemblyFile, false);
  417. }
  418. public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence)
  419. {
  420. Assembly a = LoadFrom (assemblyFile, false);
  421. #if !NET_2_1
  422. if ((a != null) && (securityEvidence != null)) {
  423. // merge evidence (i.e. replace defaults with provided evidences)
  424. a.Evidence.Merge (securityEvidence);
  425. }
  426. #endif
  427. return a;
  428. }
  429. [MonoTODO("This overload is not currently implemented")]
  430. // FIXME: What are we missing?
  431. public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
  432. {
  433. if (assemblyFile == null)
  434. throw new ArgumentNullException ("assemblyFile");
  435. if (assemblyFile == String.Empty)
  436. throw new ArgumentException ("Name can't be the empty string", "assemblyFile");
  437. throw new NotImplementedException ();
  438. }
  439. public static Assembly LoadFile (String path, Evidence securityEvidence)
  440. {
  441. if (path == null)
  442. throw new ArgumentNullException ("path");
  443. if (path == String.Empty)
  444. throw new ArgumentException ("Path can't be empty", "path");
  445. // FIXME: Make this do the right thing
  446. return LoadFrom (path, securityEvidence);
  447. }
  448. public static Assembly LoadFile (String path)
  449. {
  450. return LoadFile (path, null);
  451. }
  452. public static Assembly Load (String assemblyString)
  453. {
  454. return AppDomain.CurrentDomain.Load (assemblyString);
  455. }
  456. public static Assembly Load (String assemblyString, Evidence assemblySecurity)
  457. {
  458. return AppDomain.CurrentDomain.Load (assemblyString, assemblySecurity);
  459. }
  460. public static Assembly Load (AssemblyName assemblyRef)
  461. {
  462. return AppDomain.CurrentDomain.Load (assemblyRef);
  463. }
  464. public static Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
  465. {
  466. return AppDomain.CurrentDomain.Load (assemblyRef, assemblySecurity);
  467. }
  468. public static Assembly Load (Byte[] rawAssembly)
  469. {
  470. return AppDomain.CurrentDomain.Load (rawAssembly);
  471. }
  472. public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore)
  473. {
  474. return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
  475. }
  476. public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore,
  477. Evidence securityEvidence)
  478. {
  479. return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore, securityEvidence);
  480. }
  481. public static Assembly ReflectionOnlyLoad (byte[] rawAssembly)
  482. {
  483. return AppDomain.CurrentDomain.Load (rawAssembly, null, null, true);
  484. }
  485. public static Assembly ReflectionOnlyLoad (string assemblyString)
  486. {
  487. return AppDomain.CurrentDomain.Load (assemblyString, null, true);
  488. }
  489. public static Assembly ReflectionOnlyLoadFrom (string assemblyFile)
  490. {
  491. if (assemblyFile == null)
  492. throw new ArgumentNullException ("assemblyFile");
  493. return LoadFrom (assemblyFile, true);
  494. }
  495. public static Assembly LoadWithPartialName (string partialName)
  496. {
  497. return LoadWithPartialName (partialName, null);
  498. }
  499. [MonoTODO ("Not implemented")]
  500. public Module LoadModule (string moduleName, byte [] rawModule)
  501. {
  502. throw new NotImplementedException ();
  503. }
  504. [MonoTODO ("Not implemented")]
  505. public Module LoadModule (string moduleName, byte [] rawModule, byte [] rawSymbolStore)
  506. {
  507. throw new NotImplementedException ();
  508. }
  509. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  510. private static extern Assembly load_with_partial_name (string name, Evidence e);
  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. [MonoTODO ("Currently it always returns zero")]
  642. [ComVisible (false)]
  643. public long HostContext {
  644. get { return 0; }
  645. }
  646. [ComVisible (false)]
  647. public Module ManifestModule {
  648. get {
  649. return GetManifestModule ();
  650. }
  651. }
  652. internal virtual Module GetManifestModule () {
  653. return GetManifestModuleInternal ();
  654. }
  655. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  656. internal extern Module GetManifestModuleInternal ();
  657. [ComVisible (false)]
  658. public virtual extern bool ReflectionOnly {
  659. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  660. get;
  661. }
  662. public override bool Equals (object o)
  663. {
  664. if (((object) this) == o)
  665. return true;
  666. if (o == null)
  667. return false;
  668. Assembly other = (Assembly) o;
  669. return other._mono_assembly == _mono_assembly;
  670. }
  671. #if NET_4_0
  672. public virtual IList<CustomAttributeData> GetCustomAttributesData () {
  673. return CustomAttributeData.GetCustomAttributes (this);
  674. }
  675. #endif
  676. #if !MOONLIGHT
  677. // Code Access Security
  678. internal void Resolve ()
  679. {
  680. lock (this) {
  681. // FIXME: As we (currently) delay the resolution until the first CAS
  682. // Demand it's too late to evaluate the Minimum permission set as a
  683. // condition to load the assembly into the AppDomain
  684. LoadAssemblyPermissions ();
  685. Evidence e = new Evidence (UnprotectedGetEvidence ()); // we need a copy to add PRE
  686. e.AddHost (new PermissionRequestEvidence (_minimum, _optional, _refuse));
  687. _granted = SecurityManager.ResolvePolicy (e,
  688. _minimum, _optional, _refuse, out _denied);
  689. }
  690. }
  691. internal PermissionSet GrantedPermissionSet {
  692. get {
  693. if (_granted == null) {
  694. if (SecurityManager.ResolvingPolicyLevel != null) {
  695. if (SecurityManager.ResolvingPolicyLevel.IsFullTrustAssembly (this))
  696. return DefaultPolicies.FullTrust;
  697. else
  698. return null; // we can't resolve during resolution
  699. }
  700. Resolve ();
  701. }
  702. return _granted;
  703. }
  704. }
  705. internal PermissionSet DeniedPermissionSet {
  706. get {
  707. // yes we look for granted, as denied may be null
  708. if (_granted == null) {
  709. if (SecurityManager.ResolvingPolicyLevel != null) {
  710. if (SecurityManager.ResolvingPolicyLevel.IsFullTrustAssembly (this))
  711. return null;
  712. else
  713. return DefaultPolicies.FullTrust; // deny unrestricted
  714. }
  715. Resolve ();
  716. }
  717. return _denied;
  718. }
  719. }
  720. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  721. extern internal static bool LoadPermissions (Assembly a,
  722. ref IntPtr minimum, ref int minLength,
  723. ref IntPtr optional, ref int optLength,
  724. ref IntPtr refused, ref int refLength);
  725. // Support for SecurityAction.RequestMinimum, RequestOptional and RequestRefuse
  726. private void LoadAssemblyPermissions ()
  727. {
  728. IntPtr minimum = IntPtr.Zero, optional = IntPtr.Zero, refused = IntPtr.Zero;
  729. int minLength = 0, optLength = 0, refLength = 0;
  730. if (LoadPermissions (this, ref minimum, ref minLength, ref optional,
  731. ref optLength, ref refused, ref refLength)) {
  732. // Note: no need to cache these permission sets as they will only be created once
  733. // at assembly resolution time.
  734. if (minLength > 0) {
  735. byte[] data = new byte [minLength];
  736. Marshal.Copy (minimum, data, 0, minLength);
  737. _minimum = SecurityManager.Decode (data);
  738. }
  739. if (optLength > 0) {
  740. byte[] data = new byte [optLength];
  741. Marshal.Copy (optional, data, 0, optLength);
  742. _optional = SecurityManager.Decode (data);
  743. }
  744. if (refLength > 0) {
  745. byte[] data = new byte [refLength];
  746. Marshal.Copy (refused, data, 0, refLength);
  747. _refuse = SecurityManager.Decode (data);
  748. }
  749. }
  750. }
  751. #endif
  752. }
  753. }
  754. #pragma warning restore 659