Assembly.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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 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;
  30. using System.Security;
  31. using System.Security.Policy;
  32. using System.Security.Permissions;
  33. using System.Runtime.Serialization;
  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. namespace System.Reflection {
  42. internal class ResolveEventHolder {
  43. public event ModuleResolveEventHandler ModuleResolve;
  44. }
  45. [Serializable]
  46. [ClassInterface(ClassInterfaceType.AutoDual)]
  47. public class Assembly : System.Reflection.ICustomAttributeProvider,
  48. System.Security.IEvidenceFactory, System.Runtime.Serialization.ISerializable {
  49. // Note: changes to fields must be reflected in _MonoReflectionAssembly struct (object-internals.h)
  50. private IntPtr _mono_assembly;
  51. private ResolveEventHolder resolve_event_holder;
  52. private Evidence _evidence;
  53. internal PermissionSet _minimum; // for SecurityAction.RequestMinimum
  54. internal PermissionSet _optional; // for SecurityAction.RequestOptional
  55. internal PermissionSet _refuse; // for SecurityAction.RequestRefuse
  56. private PermissionSet _granted; // for the resolved assembly granted permissions
  57. private PermissionSet _denied; // for the resolved assembly denied permissions
  58. internal Assembly ()
  59. {
  60. resolve_event_holder = new ResolveEventHolder ();
  61. }
  62. //
  63. // We can't store the event directly in this class, since the
  64. // compile would silently insert the fields before _mono_assembly
  65. //
  66. public event ModuleResolveEventHandler ModuleResolve {
  67. add {
  68. resolve_event_holder.ModuleResolve -= value;
  69. }
  70. remove {
  71. resolve_event_holder.ModuleResolve -= value;
  72. }
  73. }
  74. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  75. private extern string get_code_base ();
  76. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  77. private extern string get_location ();
  78. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  79. private extern string InternalImageRuntimeVersion ();
  80. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  81. private extern bool get_global_assembly_cache ();
  82. public virtual string CodeBase {
  83. get {
  84. return get_code_base ();
  85. }
  86. }
  87. internal virtual string CopiedCodeBase {
  88. get {
  89. return get_code_base ();
  90. }
  91. }
  92. [MonoTODO]
  93. public virtual string EscapedCodeBase {
  94. get {
  95. //FIXME: escape characters -> Uri
  96. return get_code_base ();
  97. }
  98. }
  99. public virtual string FullName {
  100. get {
  101. //
  102. // FIXME: This is wrong, but it gets us going
  103. // in the compiler for now
  104. //
  105. return GetName (false).ToString ();
  106. }
  107. }
  108. public virtual extern MethodInfo EntryPoint {
  109. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  110. get;
  111. }
  112. public virtual Evidence Evidence {
  113. get {
  114. // if the host (runtime) hasn't provided it's own evidence...
  115. if (_evidence == null) {
  116. // ... we will provide our own
  117. lock (this) {
  118. _evidence = Evidence.GetDefaultHostEvidence (this);
  119. }
  120. }
  121. return _evidence;
  122. }
  123. }
  124. public bool GlobalAssemblyCache {
  125. get {
  126. return get_global_assembly_cache ();
  127. }
  128. }
  129. public virtual String Location {
  130. get {
  131. return get_location ();
  132. }
  133. }
  134. #if NET_1_1
  135. [ComVisible (false)]
  136. public virtual string ImageRuntimeVersion {
  137. get {
  138. return InternalImageRuntimeVersion ();
  139. }
  140. }
  141. #endif
  142. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  143. {
  144. UnitySerializationHolder.GetAssemblyData (this, info, context);
  145. }
  146. public virtual bool IsDefined (Type attributeType, bool inherit)
  147. {
  148. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  149. }
  150. public virtual object [] GetCustomAttributes (bool inherit)
  151. {
  152. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  153. }
  154. public virtual object [] GetCustomAttributes (Type attributeType, bool inherit)
  155. {
  156. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  157. }
  158. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  159. private extern object GetFilesInternal (String name);
  160. public virtual FileStream[] GetFiles ()
  161. {
  162. string[] names = (string[]) GetFilesInternal (null);
  163. if (names == null)
  164. return new FileStream [0];
  165. FileStream[] res = new FileStream [names.Length];
  166. for (int i = 0; i < names.Length; ++i)
  167. res [i] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
  168. return res;
  169. }
  170. [MonoTODO ("true == not implemented")]
  171. public virtual FileStream [] GetFiles (bool getResourceModules)
  172. {
  173. if (!getResourceModules)
  174. return GetFiles ();
  175. throw new NotImplementedException ();
  176. }
  177. public virtual FileStream GetFile (String name)
  178. {
  179. if (name == null)
  180. throw new ArgumentNullException ("name");
  181. if (name.Length == 0)
  182. throw new ArgumentException ("name");
  183. string filename = (string)GetFilesInternal (name);
  184. if (filename != null)
  185. return new FileStream (filename, FileMode.Open, FileAccess.Read);
  186. else
  187. return null;
  188. }
  189. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  190. private extern IntPtr GetManifestResourceInternal (String name, out int size, out Module module);
  191. public virtual Stream GetManifestResourceStream (String name)
  192. {
  193. if (name == null)
  194. throw new ArgumentNullException ("name");
  195. if (name == "")
  196. throw new ArgumentException ("name cannot have zero length.");
  197. ManifestResourceInfo info = GetManifestResourceInfo (name);
  198. if (info == null)
  199. return null;
  200. if (info.ReferencedAssembly != null)
  201. return info.ReferencedAssembly.GetManifestResourceStream (name);
  202. if ((info.FileName != null) && (info.ResourceLocation == 0)) {
  203. string filename = Path.Combine (Path.GetDirectoryName (Location),
  204. info.FileName);
  205. return new FileStream (filename, FileMode.Open, FileAccess.Read);
  206. }
  207. int size;
  208. Module module;
  209. IntPtr data = GetManifestResourceInternal (name, out size, out module);
  210. if (data == (IntPtr) 0)
  211. return null;
  212. else {
  213. IntPtrStream stream = new IntPtrStream (data, size);
  214. /*
  215. * The returned pointer points inside metadata, so
  216. * we have to increase the refcount of the module, and decrease
  217. * it when the stream is finalized.
  218. */
  219. stream.Closed += new EventHandler (new ResourceCloseHandler (module).OnClose);
  220. return stream;
  221. }
  222. }
  223. public virtual Stream GetManifestResourceStream (Type type, String name)
  224. {
  225. string ns;
  226. if (type != null)
  227. ns = type.Namespace;
  228. else
  229. ns = null;
  230. if ((ns == null) || (ns == ""))
  231. return GetManifestResourceStream (name);
  232. else
  233. return GetManifestResourceStream (ns + "." + name);
  234. }
  235. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  236. private extern Type[] GetTypes (bool exportedOnly);
  237. public virtual Type[] GetTypes ()
  238. {
  239. return GetTypes (false);
  240. }
  241. public virtual Type[] GetExportedTypes ()
  242. {
  243. return GetTypes (true);
  244. }
  245. public virtual Type GetType (String name, Boolean throwOnError)
  246. {
  247. return GetType (name, throwOnError, false);
  248. }
  249. public virtual Type GetType (String name) {
  250. return GetType (name, false, false);
  251. }
  252. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  253. internal extern Type InternalGetType (Module module, String name, Boolean throwOnError, Boolean ignoreCase);
  254. public Type GetType (string name, bool throwOnError, bool ignoreCase)
  255. {
  256. if (name == null)
  257. throw new ArgumentNullException (name);
  258. return InternalGetType (null, name, throwOnError, ignoreCase);
  259. }
  260. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  261. internal extern static void InternalGetAssemblyName (string assemblyFile, AssemblyName aname);
  262. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  263. static extern void FillName (Assembly ass, AssemblyName aname);
  264. [MonoTODO ("true == not supported")]
  265. public virtual AssemblyName GetName (Boolean copiedName)
  266. {
  267. AssemblyName aname = new AssemblyName ();
  268. FillName (this, aname);
  269. return aname;
  270. }
  271. public virtual AssemblyName GetName ()
  272. {
  273. return GetName (false);
  274. }
  275. public override String ToString ()
  276. {
  277. return GetName ().ToString ();
  278. }
  279. public static String CreateQualifiedName (String assemblyName, String typeName)
  280. {
  281. return typeName + ", " + assemblyName;
  282. }
  283. public static Assembly GetAssembly (Type type)
  284. {
  285. if (type != null)
  286. return type.Assembly;
  287. throw new ArgumentNullException ("type");
  288. }
  289. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  290. public static extern Assembly GetEntryAssembly();
  291. public Assembly GetSatelliteAssembly (CultureInfo culture)
  292. {
  293. return GetSatelliteAssembly (culture, null);
  294. }
  295. public Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
  296. {
  297. if (culture == null)
  298. throw new ArgumentException ("culture");
  299. AssemblyName aname = GetName (true);
  300. if (version != null)
  301. aname.Version = version;
  302. aname.CultureInfo = culture;
  303. aname.Name = aname.Name + ".resources";
  304. return Load (aname);
  305. }
  306. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  307. public extern static Assembly LoadFrom (String assemblyFile);
  308. public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence)
  309. {
  310. Assembly a = LoadFrom (assemblyFile);
  311. if ((a != null) && (securityEvidence != null)) {
  312. // merge evidence (i.e. replace defaults with provided evidences)
  313. a.Evidence.Merge (securityEvidence);
  314. }
  315. return a;
  316. }
  317. #if NET_1_1
  318. [MonoTODO]
  319. public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
  320. {
  321. if (assemblyFile == null)
  322. throw new ArgumentNullException ("assemblyFile");
  323. if (assemblyFile == String.Empty)
  324. throw new ArgumentException ("Name can't be the empty string", "assemblyFile");
  325. throw new NotImplementedException ();
  326. }
  327. [MonoTODO]
  328. public static Assembly LoadFile (String path, Evidence securityEvidence) {
  329. if (path == null)
  330. throw new ArgumentNullException ("path");
  331. if (path == String.Empty)
  332. throw new ArgumentException ("Path can't be empty", "path");
  333. // FIXME: Make this do the right thing
  334. return LoadFrom (path, securityEvidence);
  335. }
  336. public static Assembly LoadFile (String path) {
  337. return LoadFile (path, null);
  338. }
  339. #endif
  340. public static Assembly Load (String assemblyString)
  341. {
  342. return AppDomain.CurrentDomain.Load (assemblyString);
  343. }
  344. public static Assembly Load (String assemblyString, Evidence assemblySecurity)
  345. {
  346. return AppDomain.CurrentDomain.Load (assemblyString, assemblySecurity);
  347. }
  348. public static Assembly Load (AssemblyName assemblyRef)
  349. {
  350. return AppDomain.CurrentDomain.Load (assemblyRef);
  351. }
  352. public static Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
  353. {
  354. return AppDomain.CurrentDomain.Load (assemblyRef, assemblySecurity);
  355. }
  356. public static Assembly Load (Byte[] rawAssembly)
  357. {
  358. return AppDomain.CurrentDomain.Load (rawAssembly);
  359. }
  360. public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore)
  361. {
  362. return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
  363. }
  364. public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore,
  365. Evidence securityEvidence)
  366. {
  367. return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore, securityEvidence);
  368. }
  369. #if NET_2_0
  370. [Obsolete ("")]
  371. #endif
  372. public static Assembly LoadWithPartialName (string partialName)
  373. {
  374. return LoadWithPartialName (partialName, null);
  375. }
  376. [MonoTODO]
  377. public Module LoadModule (string moduleName, byte [] rawModule)
  378. {
  379. throw new NotImplementedException ();
  380. }
  381. [MonoTODO]
  382. public Module LoadModule (string moduleName, byte [] rawModule, byte [] rawSymbolStore)
  383. {
  384. throw new NotImplementedException ();
  385. }
  386. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  387. private static extern Assembly load_with_partial_name (string name, Evidence e);
  388. /**
  389. * LAMESPEC: It is possible for this method to throw exceptions IF the name supplied
  390. * is a valid gac name and contains filesystem entry charachters at the end of the name
  391. * ie System/// will throw an exception. However ////System will not as that is canocolized
  392. * out of the name.
  393. */
  394. #if NET_2_0
  395. [Obsolete ("")]
  396. #endif
  397. public static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence)
  398. {
  399. if (partialName == null)
  400. throw new NullReferenceException ();
  401. int ci = partialName.IndexOf (',');
  402. if (ci > 0)
  403. partialName = partialName.Substring (0, ci);
  404. return load_with_partial_name (partialName, securityEvidence);
  405. }
  406. public Object CreateInstance (String typeName)
  407. {
  408. return CreateInstance (typeName, false);
  409. }
  410. public Object CreateInstance (String typeName, Boolean ignoreCase)
  411. {
  412. Type t = GetType (typeName, false, ignoreCase);
  413. if (t == null)
  414. return null;
  415. return Activator.CreateInstance (t);
  416. }
  417. public Object CreateInstance (String typeName, Boolean ignoreCase,
  418. BindingFlags bindingAttr, Binder binder,
  419. Object[] args, CultureInfo culture,
  420. Object[] activationAttributes)
  421. {
  422. Type t = GetType (typeName, false, ignoreCase);
  423. if (t == null)
  424. return null;
  425. return Activator.CreateInstance (t, bindingAttr, binder, args, culture, activationAttributes);
  426. }
  427. public Module[] GetLoadedModules ()
  428. {
  429. return GetLoadedModules (false);
  430. }
  431. [MonoTODO]
  432. public Module[] GetLoadedModules (bool getResourceModules)
  433. {
  434. // Currently, the two sets of modules are equal
  435. return GetModules (getResourceModules);
  436. }
  437. public Module[] GetModules ()
  438. {
  439. return GetModules (false);
  440. }
  441. public Module GetModule (String name)
  442. {
  443. if (name == null)
  444. throw new ArgumentNullException ("name");
  445. if (name == "")
  446. throw new ArgumentException ("Name can't be empty");
  447. Module[] modules = GetModules (true);
  448. foreach (Module module in modules) {
  449. if (module.ScopeName == name)
  450. return module;
  451. }
  452. return null;
  453. }
  454. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  455. internal extern Module[] GetModulesInternal ();
  456. public Module[] GetModules (bool getResourceModules) {
  457. Module[] modules = GetModulesInternal ();
  458. if (!getResourceModules) {
  459. ArrayList result = new ArrayList (modules.Length);
  460. foreach (Module m in modules)
  461. if (!m.IsResource ())
  462. result.Add (m);
  463. return (Module[])result.ToArray (typeof (Module));
  464. }
  465. else
  466. return modules;
  467. }
  468. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  469. internal extern string[] GetNamespaces ();
  470. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  471. public extern virtual String[] GetManifestResourceNames ();
  472. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  473. public extern static Assembly GetExecutingAssembly ();
  474. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  475. public extern static Assembly GetCallingAssembly ();
  476. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  477. public extern AssemblyName[] GetReferencedAssemblies ();
  478. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  479. private extern bool GetManifestResourceInfoInternal (String name, ManifestResourceInfo info);
  480. public virtual ManifestResourceInfo GetManifestResourceInfo (String resourceName)
  481. {
  482. if (resourceName == null)
  483. throw new ArgumentNullException ("resourceName");
  484. if (resourceName == "")
  485. throw new ArgumentException ("String cannot have zero length.");
  486. ManifestResourceInfo result = new ManifestResourceInfo ();
  487. bool found = GetManifestResourceInfoInternal (resourceName, result);
  488. if (found)
  489. return result;
  490. else
  491. return null;
  492. }
  493. private class ResourceCloseHandler {
  494. Module module;
  495. public ResourceCloseHandler (Module module) {
  496. this.module = module;
  497. }
  498. public void OnClose (object sender, EventArgs e) {
  499. // The module dtor will take care of things
  500. module = null;
  501. }
  502. }
  503. //
  504. // The following functions are only for the Mono Debugger.
  505. //
  506. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  507. internal static extern MethodBase MonoDebugger_GetMethod (Assembly assembly, int token);
  508. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  509. internal static extern int MonoDebugger_GetMethodToken (Assembly assembly, MethodBase method);
  510. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  511. internal static extern Type MonoDebugger_GetLocalTypeFromSignature (Assembly assembly, byte[] signature);
  512. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  513. internal static extern Type MonoDebugger_GetType (Assembly assembly, int token);
  514. #if NET_2_0
  515. [MonoTODO]
  516. [ComVisible (false)]
  517. public long HostContext {
  518. get { return 0; }
  519. }
  520. [MonoTODO ("choice is rather limited")]
  521. [ComVisible (false)]
  522. public ImageFileMachine ImageFileMachine {
  523. get { return ImageFileMachine.I386; }
  524. }
  525. [MonoTODO]
  526. [ComVisible (false)]
  527. public Module ManifestModule {
  528. get { return null; }
  529. }
  530. [MonoTODO]
  531. [ComVisible (false)]
  532. public int MetadataToken {
  533. get { return 0; }
  534. }
  535. [MonoTODO]
  536. [ComVisible (false)]
  537. public PortableExecutableKind PortableExecutableKind {
  538. get { return PortableExecutableKind.ILOnly; }
  539. }
  540. [MonoTODO ("see ReflectionOnlyLoad")]
  541. [ComVisible (false)]
  542. public virtual bool ReflectionOnly {
  543. get { return false; }
  544. }
  545. #endif
  546. // Code Access Security
  547. internal void Resolve ()
  548. {
  549. lock (this) {
  550. _granted = SecurityManager.ResolvePolicy (Evidence, _minimum, _optional,
  551. _refuse, out _denied);
  552. }
  553. #if false
  554. Console.WriteLine ("Granted: {0}", _granted);
  555. if (_denied != null)
  556. Console.WriteLine ("Denied: {0}", _denied);
  557. #endif
  558. }
  559. internal PermissionSet GrantedPermissionSet {
  560. get {
  561. if (_granted == null) {
  562. Resolve ();
  563. }
  564. return _granted;
  565. }
  566. }
  567. internal PermissionSet DeniedPermissionSet {
  568. get {
  569. // yes we look for granted, as denied may be null
  570. if (_granted == null) {
  571. Resolve ();
  572. }
  573. return _denied;
  574. }
  575. }
  576. // Result isn't affected by overrides (like Assert, Deny and PermitOnly)
  577. internal bool Demand (IPermission p)
  578. {
  579. Type t = p.GetType ();
  580. // have we been explicitely denied this permission ?
  581. if (_denied != null) {
  582. IPermission denied = _denied.GetPermission (t);
  583. if (denied != null) {
  584. if (p.IsSubsetOf (denied))
  585. return false;
  586. }
  587. }
  588. // is it part of the optional permissions requested by the assembly ?
  589. if (_optional != null) {
  590. IPermission optional = _optional.GetPermission (t);
  591. if (optional != null) {
  592. // there is! so we can only request a subset of it
  593. if (!p.IsSubsetOf (optional))
  594. return false;
  595. }
  596. }
  597. // don't check IUnrestrictedPermission if we have "Full Trust"
  598. // note: that won't work for code identity permissions (e.g. Zone)
  599. if ((p is IUnrestrictedPermission) && GrantedPermissionSet.IsUnrestricted ())
  600. return true;
  601. // finally does the resolved policy allow this requested permission ?
  602. IPermission granted = GrantedPermissionSet.GetPermission (t);
  603. if (granted != null) {
  604. if (!p.IsSubsetOf (granted))
  605. return false;
  606. }
  607. return true;
  608. }
  609. // Result isn't affected by overrides (like Assert, Deny and PermitOnly)
  610. internal bool Demand (PermissionSet ps)
  611. {
  612. // have we been explicitely denied this permission ?
  613. if (DeniedPermissionSet != null) {
  614. if (ps.IsSubsetOf (DeniedPermissionSet)) {
  615. return false;
  616. }
  617. }
  618. // is it part of the optional permissions requested by the assembly ?
  619. if (_optional != null) {
  620. // there is! so we can only request a subset of it
  621. if (!ps.IsSubsetOf (_optional)) {
  622. return false;
  623. }
  624. }
  625. return ps.IsSubsetOf (GrantedPermissionSet);
  626. }
  627. }
  628. }