Assembly.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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, bool getResourceModules);
  160. public virtual FileStream[] GetFiles ()
  161. {
  162. return GetFiles (false);
  163. }
  164. public virtual FileStream [] GetFiles (bool getResourceModules)
  165. {
  166. string[] names = (string[]) GetFilesInternal (null, getResourceModules);
  167. if (names == null)
  168. return new FileStream [0];
  169. FileStream[] res = new FileStream [names.Length];
  170. for (int i = 0; i < names.Length; ++i)
  171. res [i] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
  172. return res;
  173. }
  174. public virtual FileStream GetFile (String name)
  175. {
  176. if (name == null)
  177. throw new ArgumentNullException ("name");
  178. if (name.Length == 0)
  179. throw new ArgumentException ("name");
  180. string filename = (string)GetFilesInternal (name, true);
  181. if (filename != null)
  182. return new FileStream (filename, FileMode.Open, FileAccess.Read);
  183. else
  184. return null;
  185. }
  186. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  187. private extern IntPtr GetManifestResourceInternal (String name, out int size, out Module module);
  188. public virtual Stream GetManifestResourceStream (String name)
  189. {
  190. if (name == null)
  191. throw new ArgumentNullException ("name");
  192. if (name == "")
  193. throw new ArgumentException ("name cannot have zero length.");
  194. ManifestResourceInfo info = GetManifestResourceInfo (name);
  195. if (info == null)
  196. return null;
  197. if (info.ReferencedAssembly != null)
  198. return info.ReferencedAssembly.GetManifestResourceStream (name);
  199. if ((info.FileName != null) && (info.ResourceLocation == 0)) {
  200. string filename = Path.Combine (Path.GetDirectoryName (Location),
  201. info.FileName);
  202. return new FileStream (filename, FileMode.Open, FileAccess.Read);
  203. }
  204. int size;
  205. Module module;
  206. IntPtr data = GetManifestResourceInternal (name, out size, out module);
  207. if (data == (IntPtr) 0)
  208. return null;
  209. else {
  210. IntPtrStream stream = new IntPtrStream (data, size);
  211. /*
  212. * The returned pointer points inside metadata, so
  213. * we have to increase the refcount of the module, and decrease
  214. * it when the stream is finalized.
  215. */
  216. stream.Closed += new EventHandler (new ResourceCloseHandler (module).OnClose);
  217. return stream;
  218. }
  219. }
  220. public virtual Stream GetManifestResourceStream (Type type, String name)
  221. {
  222. string ns;
  223. if (type != null)
  224. ns = type.Namespace;
  225. else
  226. ns = null;
  227. if ((ns == null) || (ns == ""))
  228. return GetManifestResourceStream (name);
  229. else
  230. return GetManifestResourceStream (ns + "." + name);
  231. }
  232. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  233. private extern Type[] GetTypes (bool exportedOnly);
  234. public virtual Type[] GetTypes ()
  235. {
  236. return GetTypes (false);
  237. }
  238. public virtual Type[] GetExportedTypes ()
  239. {
  240. return GetTypes (true);
  241. }
  242. public virtual Type GetType (String name, Boolean throwOnError)
  243. {
  244. return GetType (name, throwOnError, false);
  245. }
  246. public virtual Type GetType (String name) {
  247. return GetType (name, false, false);
  248. }
  249. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  250. internal extern Type InternalGetType (Module module, String name, Boolean throwOnError, Boolean ignoreCase);
  251. public Type GetType (string name, bool throwOnError, bool ignoreCase)
  252. {
  253. if (name == null)
  254. throw new ArgumentNullException (name);
  255. return InternalGetType (null, name, throwOnError, ignoreCase);
  256. }
  257. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  258. internal extern static void InternalGetAssemblyName (string assemblyFile, AssemblyName aname);
  259. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  260. static extern void FillName (Assembly ass, AssemblyName aname);
  261. [MonoTODO ("true == not supported")]
  262. public virtual AssemblyName GetName (Boolean copiedName)
  263. {
  264. AssemblyName aname = new AssemblyName ();
  265. FillName (this, aname);
  266. return aname;
  267. }
  268. public virtual AssemblyName GetName ()
  269. {
  270. return GetName (false);
  271. }
  272. public override String ToString ()
  273. {
  274. return GetName ().ToString ();
  275. }
  276. public static String CreateQualifiedName (String assemblyName, String typeName)
  277. {
  278. return typeName + ", " + assemblyName;
  279. }
  280. public static Assembly GetAssembly (Type type)
  281. {
  282. if (type != null)
  283. return type.Assembly;
  284. throw new ArgumentNullException ("type");
  285. }
  286. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  287. public static extern Assembly GetEntryAssembly();
  288. public Assembly GetSatelliteAssembly (CultureInfo culture)
  289. {
  290. return GetSatelliteAssembly (culture, null);
  291. }
  292. public Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
  293. {
  294. if (culture == null)
  295. throw new ArgumentException ("culture");
  296. AssemblyName aname = GetName (true);
  297. if (version != null)
  298. aname.Version = version;
  299. aname.CultureInfo = culture;
  300. aname.Name = aname.Name + ".resources";
  301. return Load (aname);
  302. }
  303. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  304. public extern static Assembly LoadFrom (String assemblyFile);
  305. public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence)
  306. {
  307. Assembly a = LoadFrom (assemblyFile);
  308. if ((a != null) && (securityEvidence != null)) {
  309. // merge evidence (i.e. replace defaults with provided evidences)
  310. a.Evidence.Merge (securityEvidence);
  311. }
  312. return a;
  313. }
  314. #if NET_1_1
  315. [MonoTODO]
  316. public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
  317. {
  318. if (assemblyFile == null)
  319. throw new ArgumentNullException ("assemblyFile");
  320. if (assemblyFile == String.Empty)
  321. throw new ArgumentException ("Name can't be the empty string", "assemblyFile");
  322. throw new NotImplementedException ();
  323. }
  324. [MonoTODO]
  325. public static Assembly LoadFile (String path, Evidence securityEvidence) {
  326. if (path == null)
  327. throw new ArgumentNullException ("path");
  328. if (path == String.Empty)
  329. throw new ArgumentException ("Path can't be empty", "path");
  330. // FIXME: Make this do the right thing
  331. return LoadFrom (path, securityEvidence);
  332. }
  333. public static Assembly LoadFile (String path) {
  334. return LoadFile (path, null);
  335. }
  336. #endif
  337. public static Assembly Load (String assemblyString)
  338. {
  339. return AppDomain.CurrentDomain.Load (assemblyString);
  340. }
  341. public static Assembly Load (String assemblyString, Evidence assemblySecurity)
  342. {
  343. return AppDomain.CurrentDomain.Load (assemblyString, assemblySecurity);
  344. }
  345. public static Assembly Load (AssemblyName assemblyRef)
  346. {
  347. return AppDomain.CurrentDomain.Load (assemblyRef);
  348. }
  349. public static Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
  350. {
  351. return AppDomain.CurrentDomain.Load (assemblyRef, assemblySecurity);
  352. }
  353. public static Assembly Load (Byte[] rawAssembly)
  354. {
  355. return AppDomain.CurrentDomain.Load (rawAssembly);
  356. }
  357. public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore)
  358. {
  359. return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
  360. }
  361. public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore,
  362. Evidence securityEvidence)
  363. {
  364. return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore, securityEvidence);
  365. }
  366. #if NET_2_0
  367. [MonoTODO]
  368. public static Assembly ReflectionOnlyLoad (byte[] rawAssembly)
  369. {
  370. throw new NotImplementedException ();
  371. }
  372. [MonoTODO]
  373. public static Assembly ReflectionOnlyLoad (string assemblyString) {
  374. throw new NotImplementedException ();
  375. }
  376. [MonoTODO]
  377. public static Assembly ReflectionOnlyLoadFrom (string assemblyFile) {
  378. throw new NotImplementedException ();
  379. }
  380. #endif
  381. #if NET_2_0
  382. [Obsolete ("")]
  383. #endif
  384. public static Assembly LoadWithPartialName (string partialName)
  385. {
  386. return LoadWithPartialName (partialName, null);
  387. }
  388. [MonoTODO]
  389. public Module LoadModule (string moduleName, byte [] rawModule)
  390. {
  391. throw new NotImplementedException ();
  392. }
  393. [MonoTODO]
  394. public Module LoadModule (string moduleName, byte [] rawModule, byte [] rawSymbolStore)
  395. {
  396. throw new NotImplementedException ();
  397. }
  398. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  399. private static extern Assembly load_with_partial_name (string name, Evidence e);
  400. #if NET_2_0
  401. [Obsolete ("")]
  402. #endif
  403. public static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence)
  404. {
  405. return LoadWithPartialName (partialName, securityEvidence, true);
  406. }
  407. /**
  408. * LAMESPEC: It is possible for this method to throw exceptions IF the name supplied
  409. * is a valid gac name and contains filesystem entry charachters at the end of the name
  410. * ie System/// will throw an exception. However ////System will not as that is canocolized
  411. * out of the name.
  412. */
  413. #if NET_2_0
  414. [Obsolete ("")]
  415. [ComVisible (false)]
  416. [MonoTODO]
  417. public
  418. #else
  419. internal
  420. #endif
  421. static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence, bool oldBehavior)
  422. {
  423. if (!oldBehavior)
  424. throw new NotImplementedException ();
  425. if (partialName == null)
  426. throw new NullReferenceException ();
  427. int ci = partialName.IndexOf (',');
  428. if (ci > 0)
  429. partialName = partialName.Substring (0, ci);
  430. return load_with_partial_name (partialName, securityEvidence);
  431. }
  432. public Object CreateInstance (String typeName)
  433. {
  434. return CreateInstance (typeName, false);
  435. }
  436. public Object CreateInstance (String typeName, Boolean ignoreCase)
  437. {
  438. Type t = GetType (typeName, false, ignoreCase);
  439. if (t == null)
  440. return null;
  441. return Activator.CreateInstance (t);
  442. }
  443. public Object CreateInstance (String typeName, Boolean ignoreCase,
  444. BindingFlags bindingAttr, Binder binder,
  445. Object[] args, CultureInfo culture,
  446. Object[] activationAttributes)
  447. {
  448. Type t = GetType (typeName, false, ignoreCase);
  449. if (t == null)
  450. return null;
  451. return Activator.CreateInstance (t, bindingAttr, binder, args, culture, activationAttributes);
  452. }
  453. public Module[] GetLoadedModules ()
  454. {
  455. return GetLoadedModules (false);
  456. }
  457. [MonoTODO]
  458. public Module[] GetLoadedModules (bool getResourceModules)
  459. {
  460. // Currently, the two sets of modules are equal
  461. return GetModules (getResourceModules);
  462. }
  463. public Module[] GetModules ()
  464. {
  465. return GetModules (false);
  466. }
  467. public Module GetModule (String name)
  468. {
  469. if (name == null)
  470. throw new ArgumentNullException ("name");
  471. if (name == "")
  472. throw new ArgumentException ("Name can't be empty");
  473. Module[] modules = GetModules (true);
  474. foreach (Module module in modules) {
  475. if (module.ScopeName == name)
  476. return module;
  477. }
  478. return null;
  479. }
  480. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  481. internal extern Module[] GetModulesInternal ();
  482. public Module[] GetModules (bool getResourceModules) {
  483. Module[] modules = GetModulesInternal ();
  484. if (!getResourceModules) {
  485. ArrayList result = new ArrayList (modules.Length);
  486. foreach (Module m in modules)
  487. if (!m.IsResource ())
  488. result.Add (m);
  489. return (Module[])result.ToArray (typeof (Module));
  490. }
  491. else
  492. return modules;
  493. }
  494. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  495. internal extern string[] GetNamespaces ();
  496. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  497. public extern virtual String[] GetManifestResourceNames ();
  498. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  499. public extern static Assembly GetExecutingAssembly ();
  500. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  501. public extern static Assembly GetCallingAssembly ();
  502. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  503. public extern AssemblyName[] GetReferencedAssemblies ();
  504. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  505. private extern bool GetManifestResourceInfoInternal (String name, ManifestResourceInfo info);
  506. public virtual ManifestResourceInfo GetManifestResourceInfo (String resourceName)
  507. {
  508. if (resourceName == null)
  509. throw new ArgumentNullException ("resourceName");
  510. if (resourceName == "")
  511. throw new ArgumentException ("String cannot have zero length.");
  512. ManifestResourceInfo result = new ManifestResourceInfo ();
  513. bool found = GetManifestResourceInfoInternal (resourceName, result);
  514. if (found)
  515. return result;
  516. else
  517. return null;
  518. }
  519. private class ResourceCloseHandler {
  520. Module module;
  521. public ResourceCloseHandler (Module module) {
  522. this.module = module;
  523. }
  524. public void OnClose (object sender, EventArgs e) {
  525. // The module dtor will take care of things
  526. module = null;
  527. }
  528. }
  529. //
  530. // The following functions are only for the Mono Debugger.
  531. //
  532. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  533. internal static extern MethodBase MonoDebugger_GetMethod (Assembly assembly, int token);
  534. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  535. internal static extern int MonoDebugger_GetMethodToken (Assembly assembly, MethodBase method);
  536. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  537. internal static extern Type MonoDebugger_GetLocalTypeFromSignature (Assembly assembly, byte[] signature);
  538. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  539. internal static extern Type MonoDebugger_GetType (Assembly assembly, int token);
  540. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  541. internal static extern string MonoDebugger_CheckRuntimeVersion (string filename);
  542. #if NET_2_0
  543. [MonoTODO]
  544. [ComVisible (false)]
  545. public long HostContext {
  546. get { return 0; }
  547. }
  548. [ComVisible (false)]
  549. public ImageFileMachine ImageFileMachine {
  550. get {
  551. ImageFileMachine machine;
  552. PortableExecutableKind kind;
  553. ModuleHandle handle = ManifestModule.ModuleHandle;
  554. handle.GetPEKind (out kind, out machine);
  555. return machine;
  556. }
  557. }
  558. [ComVisible (false)]
  559. public extern Module ManifestModule {
  560. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  561. get;
  562. }
  563. [ComVisible (false)]
  564. public extern int MetadataToken {
  565. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  566. get;
  567. }
  568. [ComVisible (false)]
  569. public PortableExecutableKind PortableExecutableKind {
  570. get {
  571. ImageFileMachine machine;
  572. PortableExecutableKind kind;
  573. ModuleHandle handle = ManifestModule.ModuleHandle;
  574. handle.GetPEKind (out kind, out machine);
  575. return kind;
  576. }
  577. }
  578. [MonoTODO ("see ReflectionOnlyLoad")]
  579. [ComVisible (false)]
  580. public virtual bool ReflectionOnly {
  581. get { return false; }
  582. }
  583. #endif
  584. // Code Access Security
  585. internal void Resolve ()
  586. {
  587. lock (this) {
  588. // FIXME: As we (currently) delay the resolution until the first CAS
  589. // Demand it's too late to evaluate the Minimum permission set as a
  590. // condition to load the assembly into the AppDomain
  591. LoadAssemblyPermissions ();
  592. _granted = SecurityManager.ResolvePolicy (Evidence, _minimum, _optional,
  593. _refuse, out _denied);
  594. }
  595. #if false
  596. Console.WriteLine ("*** ASSEMBLY RESOLVE INPUT ***");
  597. if (_minimum != null)
  598. Console.WriteLine ("Minimum: {0}", _minimum);
  599. if (_optional != null)
  600. Console.WriteLine ("Optional: {0}", _optional);
  601. if (_refuse != null)
  602. Console.WriteLine ("Refuse: {0}", _refuse);
  603. Console.WriteLine ("*** ASSEMBLY RESOLVE RESULTS ***");
  604. Console.WriteLine ("Granted: {0}", _granted);
  605. if (_denied != null)
  606. Console.WriteLine ("Denied: {0}", _denied);
  607. Console.WriteLine ("*** ASSEMBLY RESOLVE END ***");
  608. #endif
  609. }
  610. internal PermissionSet GrantedPermissionSet {
  611. get {
  612. if (_granted == null) {
  613. Resolve ();
  614. }
  615. return _granted;
  616. }
  617. }
  618. internal PermissionSet DeniedPermissionSet {
  619. get {
  620. // yes we look for granted, as denied may be null
  621. if (_granted == null) {
  622. Resolve ();
  623. }
  624. return _denied;
  625. }
  626. }
  627. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  628. extern internal static bool LoadPermissions (Assembly a,
  629. ref IntPtr minimum, ref int minLength,
  630. ref IntPtr optional, ref int optLength,
  631. ref IntPtr refused, ref int refLength);
  632. // Support for SecurityAction.RequestMinimum, RequestOptional and RequestRefuse
  633. private void LoadAssemblyPermissions ()
  634. {
  635. IntPtr minimum = IntPtr.Zero, optional = IntPtr.Zero, refused = IntPtr.Zero;
  636. int minLength = 0, optLength = 0, refLength = 0;
  637. if (LoadPermissions (this, ref minimum, ref minLength, ref optional,
  638. ref optLength, ref refused, ref refLength)) {
  639. // Note: no need to cache these permission sets as they will only be created once
  640. // at assembly resolution time.
  641. if (minLength > 0) {
  642. byte[] data = new byte [minLength];
  643. Marshal.Copy (minimum, data, 0, minLength);
  644. _minimum = SecurityManager.Decode (data);
  645. }
  646. if (optLength > 0) {
  647. byte[] data = new byte [optLength];
  648. Marshal.Copy (optional, data, 0, optLength);
  649. _optional = SecurityManager.Decode (data);
  650. }
  651. if (refLength > 0) {
  652. byte[] data = new byte [refLength];
  653. Marshal.Copy (refused, data, 0, refLength);
  654. _refuse = SecurityManager.Decode (data);
  655. }
  656. }
  657. }
  658. }
  659. }