Assembly.cs 25 KB

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