Assembly.cs 25 KB

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