Assembly.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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. 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 virtual 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. internal static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence, bool oldBehavior)
  458. {
  459. if (!oldBehavior)
  460. throw new NotImplementedException ();
  461. if (partialName == null)
  462. throw new NullReferenceException ();
  463. return load_with_partial_name (partialName, securityEvidence);
  464. }
  465. public Object CreateInstance (String typeName)
  466. {
  467. return CreateInstance (typeName, false);
  468. }
  469. public Object CreateInstance (String typeName, Boolean ignoreCase)
  470. {
  471. Type t = GetType (typeName, false, ignoreCase);
  472. if (t == null)
  473. return null;
  474. try {
  475. return Activator.CreateInstance (t);
  476. } catch (InvalidOperationException) {
  477. throw new ArgumentException ("It is illegal to invoke a method on a Type loaded via ReflectionOnly methods.");
  478. }
  479. }
  480. public Object CreateInstance (String typeName, Boolean ignoreCase,
  481. BindingFlags bindingAttr, Binder binder,
  482. Object[] args, CultureInfo culture,
  483. Object[] activationAttributes)
  484. {
  485. Type t = GetType (typeName, false, ignoreCase);
  486. if (t == null)
  487. return null;
  488. try {
  489. return Activator.CreateInstance (t, bindingAttr, binder, args, culture, activationAttributes);
  490. } catch (InvalidOperationException) {
  491. throw new ArgumentException ("It is illegal to invoke a method on a Type loaded via ReflectionOnly methods.");
  492. }
  493. }
  494. public Module[] GetLoadedModules ()
  495. {
  496. return GetLoadedModules (false);
  497. }
  498. [MonoTODO]
  499. public Module[] GetLoadedModules (bool getResourceModules)
  500. {
  501. // Currently, the two sets of modules are equal
  502. return GetModules (getResourceModules);
  503. }
  504. public Module[] GetModules ()
  505. {
  506. return GetModules (false);
  507. }
  508. public Module GetModule (String name)
  509. {
  510. if (name == null)
  511. throw new ArgumentNullException ("name");
  512. if (name == "")
  513. throw new ArgumentException ("Name can't be empty");
  514. Module[] modules = GetModules (true);
  515. foreach (Module module in modules) {
  516. if (module.ScopeName == name)
  517. return module;
  518. }
  519. return null;
  520. }
  521. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  522. internal extern Module[] GetModulesInternal ();
  523. public Module[] GetModules (bool getResourceModules) {
  524. Module[] modules = GetModulesInternal ();
  525. if (!getResourceModules) {
  526. ArrayList result = new ArrayList (modules.Length);
  527. foreach (Module m in modules)
  528. if (!m.IsResource ())
  529. result.Add (m);
  530. return (Module[])result.ToArray (typeof (Module));
  531. }
  532. else
  533. return modules;
  534. }
  535. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  536. internal extern string[] GetNamespaces ();
  537. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  538. public extern virtual String[] GetManifestResourceNames ();
  539. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  540. public extern static Assembly GetExecutingAssembly ();
  541. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  542. public extern static Assembly GetCallingAssembly ();
  543. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  544. public extern AssemblyName[] GetReferencedAssemblies ();
  545. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  546. private extern bool GetManifestResourceInfoInternal (String name, ManifestResourceInfo info);
  547. public virtual ManifestResourceInfo GetManifestResourceInfo (String resourceName)
  548. {
  549. if (resourceName == null)
  550. throw new ArgumentNullException ("resourceName");
  551. if (resourceName == "")
  552. throw new ArgumentException ("String cannot have zero length.");
  553. ManifestResourceInfo result = new ManifestResourceInfo ();
  554. bool found = GetManifestResourceInfoInternal (resourceName, result);
  555. if (found)
  556. return result;
  557. else
  558. return null;
  559. }
  560. private class ResourceCloseHandler {
  561. Module module;
  562. public ResourceCloseHandler (Module module) {
  563. this.module = module;
  564. }
  565. public void OnClose (object sender, EventArgs e) {
  566. // The module dtor will take care of things
  567. module = null;
  568. }
  569. }
  570. //
  571. // The following functions are only for the Mono Debugger.
  572. //
  573. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  574. internal static extern MethodBase MonoDebugger_GetMethod (Assembly assembly, int token);
  575. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  576. internal static extern int MonoDebugger_GetMethodToken (Assembly assembly, MethodBase method);
  577. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  578. internal static extern Type MonoDebugger_GetLocalTypeFromSignature (Assembly assembly, byte[] signature);
  579. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  580. internal static extern Type MonoDebugger_GetType (Assembly assembly, int token);
  581. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  582. internal static extern string MonoDebugger_CheckRuntimeVersion (string filename);
  583. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  584. internal static extern string MonoDebugger_GetMethodIndex (MethodBase method);
  585. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  586. internal static extern Type MonoDebugger_MakeArrayType (Type type, int rank);
  587. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  588. internal static extern int MonoDebugger_GetTypeToken (Type type);
  589. #if NET_2_0
  590. [MonoTODO]
  591. [ComVisible (false)]
  592. public long HostContext {
  593. get { return 0; }
  594. }
  595. [ComVisible (false)]
  596. [Obsolete ("Please use Assembly.ManifestModule.GetPEKind() instead - this will be removed before Whidbey ships.")]
  597. public ImageFileMachine ImageFileMachine {
  598. get {
  599. ImageFileMachine machine;
  600. PortableExecutableKinds kind;
  601. ModuleHandle handle = ManifestModule.ModuleHandle;
  602. handle.GetPEKind (out kind, out machine);
  603. return machine;
  604. }
  605. }
  606. [ComVisible (false)]
  607. public extern Module ManifestModule {
  608. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  609. get;
  610. }
  611. [ComVisible (false)]
  612. [Obsolete ("This method has been deprecated and will be removed before v2.0 RTM is released.")]
  613. public extern int MetadataToken {
  614. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  615. get;
  616. }
  617. [ComVisible (false)]
  618. [Obsolete ("Please use Assembly.ManifestModule.GetPEKind() instead - this will be removed before Whidbey ships.")]
  619. public PortableExecutableKinds PortableExecutableKinds {
  620. get {
  621. ImageFileMachine machine;
  622. PortableExecutableKinds kind;
  623. ModuleHandle handle = ManifestModule.ModuleHandle;
  624. handle.GetPEKind (out kind, out machine);
  625. return kind;
  626. }
  627. }
  628. [ComVisible (false)]
  629. public virtual extern bool ReflectionOnly {
  630. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  631. get;
  632. }
  633. #endif
  634. // Code Access Security
  635. internal void Resolve ()
  636. {
  637. lock (this) {
  638. // FIXME: As we (currently) delay the resolution until the first CAS
  639. // Demand it's too late to evaluate the Minimum permission set as a
  640. // condition to load the assembly into the AppDomain
  641. LoadAssemblyPermissions ();
  642. _granted = SecurityManager.ResolvePolicy (UnprotectedGetEvidence (),
  643. _minimum, _optional, _refuse, out _denied);
  644. }
  645. #if false
  646. Console.WriteLine ("*** ASSEMBLY RESOLVE INPUT ***");
  647. if (_minimum != null)
  648. Console.WriteLine ("Minimum: {0}", _minimum);
  649. if (_optional != null)
  650. Console.WriteLine ("Optional: {0}", _optional);
  651. if (_refuse != null)
  652. Console.WriteLine ("Refuse: {0}", _refuse);
  653. Console.WriteLine ("*** ASSEMBLY RESOLVE RESULTS ***");
  654. Console.WriteLine ("Granted: {0}", _granted);
  655. if (_denied != null)
  656. Console.WriteLine ("Denied: {0}", _denied);
  657. Console.WriteLine ("*** ASSEMBLY RESOLVE END ***");
  658. #endif
  659. }
  660. internal PermissionSet GrantedPermissionSet {
  661. get {
  662. if (_granted == null) {
  663. if (SecurityManager.ResolvingPolicyLevel != null) {
  664. if (SecurityManager.ResolvingPolicyLevel.IsFullTrustAssembly (this))
  665. return DefaultPolicies.FullTrust;
  666. else
  667. return null; // we can't resolve during resolution
  668. }
  669. Resolve ();
  670. }
  671. return _granted;
  672. }
  673. }
  674. internal PermissionSet DeniedPermissionSet {
  675. get {
  676. // yes we look for granted, as denied may be null
  677. if (_granted == null) {
  678. if (SecurityManager.ResolvingPolicyLevel != null) {
  679. if (SecurityManager.ResolvingPolicyLevel.IsFullTrustAssembly (this))
  680. return null;
  681. else
  682. return DefaultPolicies.FullTrust; // deny unrestricted
  683. }
  684. Resolve ();
  685. }
  686. return _denied;
  687. }
  688. }
  689. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  690. extern internal static bool LoadPermissions (Assembly a,
  691. ref IntPtr minimum, ref int minLength,
  692. ref IntPtr optional, ref int optLength,
  693. ref IntPtr refused, ref int refLength);
  694. // Support for SecurityAction.RequestMinimum, RequestOptional and RequestRefuse
  695. private void LoadAssemblyPermissions ()
  696. {
  697. IntPtr minimum = IntPtr.Zero, optional = IntPtr.Zero, refused = IntPtr.Zero;
  698. int minLength = 0, optLength = 0, refLength = 0;
  699. if (LoadPermissions (this, ref minimum, ref minLength, ref optional,
  700. ref optLength, ref refused, ref refLength)) {
  701. // Note: no need to cache these permission sets as they will only be created once
  702. // at assembly resolution time.
  703. if (minLength > 0) {
  704. byte[] data = new byte [minLength];
  705. Marshal.Copy (minimum, data, 0, minLength);
  706. _minimum = SecurityManager.Decode (data);
  707. }
  708. if (optLength > 0) {
  709. byte[] data = new byte [optLength];
  710. Marshal.Copy (optional, data, 0, optLength);
  711. _optional = SecurityManager.Decode (data);
  712. }
  713. if (refLength > 0) {
  714. byte[] data = new byte [refLength];
  715. Marshal.Copy (refused, data, 0, refLength);
  716. _refuse = SecurityManager.Decode (data);
  717. }
  718. }
  719. }
  720. }
  721. }