Assembly.cs 23 KB

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