Assembly.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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;
  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.Collections.Generic;
  41. using System.Configuration.Assemblies;
  42. using Mono.Security;
  43. namespace System.Reflection {
  44. #pragma warning disable 659 // overrides Equals but not GetHashCode
  45. [ComVisible (true)]
  46. [ComDefaultInterfaceAttribute (typeof (_Assembly))]
  47. [Serializable]
  48. [ClassInterface(ClassInterfaceType.None)]
  49. #if MOBILE
  50. public partial class Assembly : ICustomAttributeProvider, _Assembly {
  51. #elif MOONLIGHT
  52. public abstract class Assembly : ICustomAttributeProvider, _Assembly {
  53. #elif NET_4_0
  54. public abstract class Assembly : ICustomAttributeProvider, _Assembly, IEvidenceFactory, ISerializable {
  55. #else
  56. public partial class Assembly : ICustomAttributeProvider, _Assembly, IEvidenceFactory, ISerializable {
  57. #endif
  58. internal class ResolveEventHolder {
  59. public event ModuleResolveEventHandler ModuleResolve;
  60. }
  61. // Note: changes to fields must be reflected in _MonoReflectionAssembly struct (object-internals.h)
  62. #pragma warning disable 649
  63. private IntPtr _mono_assembly;
  64. #pragma warning restore 649
  65. private ResolveEventHolder resolve_event_holder;
  66. private Evidence _evidence;
  67. internal PermissionSet _minimum; // for SecurityAction.RequestMinimum
  68. internal PermissionSet _optional; // for SecurityAction.RequestOptional
  69. internal PermissionSet _refuse; // for SecurityAction.RequestRefuse
  70. private PermissionSet _granted; // for the resolved assembly granted permissions
  71. private PermissionSet _denied; // for the resolved assembly denied permissions
  72. private bool fromByteArray;
  73. private string assemblyName;
  74. #if NET_4_0 || MOONLIGHT
  75. protected
  76. #else
  77. internal
  78. #endif
  79. Assembly ()
  80. {
  81. resolve_event_holder = new ResolveEventHolder ();
  82. }
  83. //
  84. // We can't store the event directly in this class, since the
  85. // compiler would silently insert the fields before _mono_assembly
  86. //
  87. public event ModuleResolveEventHandler ModuleResolve {
  88. [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
  89. add {
  90. resolve_event_holder.ModuleResolve += value;
  91. }
  92. [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
  93. remove {
  94. resolve_event_holder.ModuleResolve -= value;
  95. }
  96. }
  97. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  98. private extern string get_code_base (bool escaped);
  99. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  100. private extern string get_fullname ();
  101. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  102. private extern string get_location ();
  103. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  104. private extern string InternalImageRuntimeVersion ();
  105. // SECURITY: this should be the only caller to icall get_code_base
  106. private string GetCodeBase (bool escaped)
  107. {
  108. string cb = get_code_base (escaped);
  109. #if !NET_2_1
  110. if (SecurityManager.SecurityEnabled) {
  111. // we cannot divulge local file informations
  112. if (String.Compare ("FILE://", 0, cb, 0, 7, true, CultureInfo.InvariantCulture) == 0) {
  113. string file = cb.Substring (7);
  114. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, file).Demand ();
  115. }
  116. }
  117. #endif
  118. return cb;
  119. }
  120. public virtual string CodeBase {
  121. get { return GetCodeBase (false); }
  122. }
  123. public virtual string EscapedCodeBase {
  124. get { return GetCodeBase (true); }
  125. }
  126. public virtual string FullName {
  127. get {
  128. //
  129. // FIXME: This is wrong, but it gets us going
  130. // in the compiler for now
  131. //
  132. return ToString ();
  133. }
  134. }
  135. public virtual extern MethodInfo EntryPoint {
  136. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  137. get;
  138. }
  139. #if !MOONLIGHT
  140. public virtual Evidence Evidence {
  141. [SecurityPermission (SecurityAction.Demand, ControlEvidence = true)]
  142. get { return UnprotectedGetEvidence (); }
  143. }
  144. // note: the security runtime requires evidences but may be unable to do so...
  145. internal Evidence UnprotectedGetEvidence ()
  146. {
  147. // if the host (runtime) hasn't provided it's own evidence...
  148. if (_evidence == null) {
  149. // ... we will provide our own
  150. lock (this) {
  151. _evidence = Evidence.GetDefaultHostEvidence (this);
  152. }
  153. }
  154. return _evidence;
  155. }
  156. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  157. internal extern bool get_global_assembly_cache ();
  158. #endif
  159. internal bool FromByteArray {
  160. set { fromByteArray = value; }
  161. }
  162. public virtual String Location {
  163. get {
  164. if (fromByteArray)
  165. return String.Empty;
  166. string loc = get_location ();
  167. #if !NET_2_1
  168. if ((loc != String.Empty) && SecurityManager.SecurityEnabled) {
  169. // we cannot divulge local file informations
  170. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, loc).Demand ();
  171. }
  172. #endif
  173. return loc;
  174. }
  175. }
  176. [ComVisible (false)]
  177. public virtual string ImageRuntimeVersion {
  178. get {
  179. return InternalImageRuntimeVersion ();
  180. }
  181. }
  182. [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
  183. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  184. {
  185. if (info == null)
  186. throw new ArgumentNullException ("info");
  187. UnitySerializationHolder.GetAssemblyData (this, info, context);
  188. }
  189. public virtual bool IsDefined (Type attributeType, bool inherit)
  190. {
  191. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  192. }
  193. public virtual object [] GetCustomAttributes (bool inherit)
  194. {
  195. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  196. }
  197. public virtual object [] GetCustomAttributes (Type attributeType, bool inherit)
  198. {
  199. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  200. }
  201. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  202. private extern object GetFilesInternal (String name, bool getResourceModules);
  203. public virtual FileStream[] GetFiles ()
  204. {
  205. return GetFiles (false);
  206. }
  207. public virtual FileStream [] GetFiles (bool getResourceModules)
  208. {
  209. string[] names = (string[]) GetFilesInternal (null, getResourceModules);
  210. if (names == null)
  211. return new FileStream [0];
  212. string location = Location;
  213. FileStream[] res;
  214. if (location != String.Empty) {
  215. res = new FileStream [names.Length + 1];
  216. res [0] = new FileStream (location, FileMode.Open, FileAccess.Read);
  217. for (int i = 0; i < names.Length; ++i)
  218. res [i + 1] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
  219. } else {
  220. res = new FileStream [names.Length];
  221. for (int i = 0; i < names.Length; ++i)
  222. res [i] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
  223. }
  224. return res;
  225. }
  226. public virtual FileStream GetFile (String name)
  227. {
  228. if (name == null)
  229. throw new ArgumentNullException (null, "Name cannot be null.");
  230. if (name.Length == 0)
  231. throw new ArgumentException ("Empty name is not valid");
  232. string filename = (string)GetFilesInternal (name, true);
  233. if (filename != null)
  234. return new FileStream (filename, FileMode.Open, FileAccess.Read);
  235. else
  236. return null;
  237. }
  238. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  239. internal extern IntPtr GetManifestResourceInternal (String name, out int size, out Module module);
  240. public virtual Stream GetManifestResourceStream (String name)
  241. {
  242. if (name == null)
  243. throw new ArgumentNullException ("name");
  244. if (name.Length == 0)
  245. throw new ArgumentException ("String cannot have zero length.",
  246. "name");
  247. ManifestResourceInfo info = GetManifestResourceInfo (name);
  248. if (info == null)
  249. return null;
  250. if (info.ReferencedAssembly != null)
  251. return info.ReferencedAssembly.GetManifestResourceStream (name);
  252. if ((info.FileName != null) && (info.ResourceLocation == 0)) {
  253. if (fromByteArray)
  254. throw new FileNotFoundException (info.FileName);
  255. string location = Path.GetDirectoryName (Location);
  256. string filename = Path.Combine (location, info.FileName);
  257. #if MOONLIGHT
  258. // we don't control the content of 'info.FileName' so we want to make sure we keep to ourselves
  259. filename = Path.GetFullPath (filename);
  260. if (!filename.StartsWith (location))
  261. throw new SecurityException ("non-rooted access to manifest resource");
  262. #endif
  263. return new FileStream (filename, FileMode.Open, FileAccess.Read);
  264. }
  265. int size;
  266. Module module;
  267. IntPtr data = GetManifestResourceInternal (name, out size, out module);
  268. if (data == (IntPtr) 0)
  269. return null;
  270. else {
  271. UnmanagedMemoryStream stream;
  272. unsafe {
  273. stream = new UnmanagedMemoryStream ((byte*) data, size);
  274. }
  275. /*
  276. * The returned pointer points inside metadata, so
  277. * we have to increase the refcount of the module, and decrease
  278. * it when the stream is finalized.
  279. */
  280. stream.Closed += new EventHandler (new ResourceCloseHandler (module).OnClose);
  281. return stream;
  282. }
  283. }
  284. public virtual Stream GetManifestResourceStream (Type type, String name)
  285. {
  286. string ns;
  287. if (type != null) {
  288. ns = type.Namespace;
  289. } else {
  290. if (name == null)
  291. throw new ArgumentNullException ("type");
  292. ns = null;
  293. }
  294. if (ns == null || ns.Length == 0)
  295. return GetManifestResourceStream (name);
  296. else
  297. return GetManifestResourceStream (ns + "." + name);
  298. }
  299. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  300. internal virtual extern Type[] GetTypes (bool exportedOnly);
  301. public virtual Type[] GetTypes ()
  302. {
  303. return GetTypes (false);
  304. }
  305. public virtual Type[] GetExportedTypes ()
  306. {
  307. return GetTypes (true);
  308. }
  309. public virtual Type GetType (String name, Boolean throwOnError)
  310. {
  311. return GetType (name, throwOnError, false);
  312. }
  313. public virtual Type GetType (String name) {
  314. return GetType (name, false, false);
  315. }
  316. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  317. internal extern Type InternalGetType (Module module, String name, Boolean throwOnError, Boolean ignoreCase);
  318. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  319. internal extern static void InternalGetAssemblyName (string assemblyFile, AssemblyName aname);
  320. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  321. static extern void FillName (Assembly ass, AssemblyName aname);
  322. [MonoTODO ("copiedName == true is not supported")]
  323. public virtual AssemblyName GetName (Boolean copiedName)
  324. {
  325. // CodeBase, which is restricted, will be copied into the AssemblyName object so...
  326. if (SecurityManager.SecurityEnabled) {
  327. GetCodeBase (true); // this will ensure the Demand is made
  328. }
  329. return UnprotectedGetName ();
  330. }
  331. public virtual AssemblyName GetName ()
  332. {
  333. return GetName (false);
  334. }
  335. // the security runtime requires access to the assemblyname (e.g. to get the strongname)
  336. internal virtual AssemblyName UnprotectedGetName ()
  337. {
  338. AssemblyName aname = new AssemblyName ();
  339. FillName (this, aname);
  340. return aname;
  341. }
  342. public override string ToString ()
  343. {
  344. // note: ToString work without requiring CodeBase (so no checks are needed)
  345. if (assemblyName != null)
  346. return assemblyName;
  347. assemblyName = get_fullname ();
  348. return assemblyName;
  349. }
  350. public static String CreateQualifiedName (String assemblyName, String typeName)
  351. {
  352. return typeName + ", " + assemblyName;
  353. }
  354. public static Assembly GetAssembly (Type type)
  355. {
  356. if (type != null)
  357. return type.Assembly;
  358. throw new ArgumentNullException ("type");
  359. }
  360. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  361. public static extern Assembly GetEntryAssembly();
  362. internal Assembly GetSatelliteAssemblyNoThrow (CultureInfo culture, Version version)
  363. {
  364. return GetSatelliteAssembly (culture, version, false);
  365. }
  366. internal Assembly GetSatelliteAssembly (CultureInfo culture, Version version, bool throwOnError)
  367. {
  368. if (culture == null)
  369. throw new ArgumentException ("culture");
  370. AssemblyName aname = GetName (true);
  371. if (version != null)
  372. aname.Version = version;
  373. aname.CultureInfo = culture;
  374. aname.Name = aname.Name + ".resources";
  375. Assembly assembly;
  376. try {
  377. assembly = AppDomain.CurrentDomain.LoadSatellite (aname, false);
  378. if (assembly != null)
  379. return assembly;
  380. } catch (FileNotFoundException) {
  381. assembly = null;
  382. // ignore
  383. }
  384. // Try the assembly directory
  385. string location = Path.GetDirectoryName (Location);
  386. string fullName = Path.Combine (location, Path.Combine (culture.Name, aname.Name + ".dll"));
  387. #if MOONLIGHT
  388. // it's unlikely that culture.Name or aname.Name could contain stuff like ".." but...
  389. fullName = Path.GetFullPath (fullName);
  390. if (!fullName.StartsWith (location)) {
  391. if (throwOnError)
  392. throw new SecurityException ("non-rooted access to satellite assembly");
  393. return null;
  394. }
  395. #endif
  396. if (!throwOnError && !File.Exists (fullName))
  397. return null;
  398. return LoadFrom (fullName);
  399. }
  400. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  401. private extern static Assembly LoadFrom (String assemblyFile, bool refonly);
  402. public static Assembly LoadFrom (String assemblyFile)
  403. {
  404. return LoadFrom (assemblyFile, false);
  405. }
  406. #if NET_4_0
  407. [Obsolete]
  408. #endif
  409. public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence)
  410. {
  411. Assembly a = LoadFrom (assemblyFile, false);
  412. #if !NET_2_1
  413. if ((a != null) && (securityEvidence != null)) {
  414. // merge evidence (i.e. replace defaults with provided evidences)
  415. a.Evidence.Merge (securityEvidence);
  416. }
  417. #endif
  418. return a;
  419. }
  420. #if NET_4_0
  421. [Obsolete]
  422. #endif
  423. [MonoTODO("This overload is not currently implemented")]
  424. // FIXME: What are we missing?
  425. public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
  426. {
  427. throw new NotImplementedException ();
  428. }
  429. #if NET_4_0
  430. [MonoTODO]
  431. public static Assembly LoadFrom (String assemblyFile, byte [] hashValue, AssemblyHashAlgorithm hashAlgorithm)
  432. {
  433. throw new NotImplementedException ();
  434. }
  435. #endif
  436. #if NET_4_0
  437. [Obsolete]
  438. #endif
  439. public static Assembly LoadFile (String path, Evidence securityEvidence)
  440. {
  441. if (path == null)
  442. throw new ArgumentNullException ("path");
  443. if (path == String.Empty)
  444. throw new ArgumentException ("Path can't be empty", "path");
  445. // FIXME: Make this do the right thing
  446. return LoadFrom (path, securityEvidence);
  447. }
  448. public static Assembly LoadFile (String path)
  449. {
  450. return LoadFile (path, null);
  451. }
  452. public static Assembly Load (String assemblyString)
  453. {
  454. return AppDomain.CurrentDomain.Load (assemblyString);
  455. }
  456. #if NET_4_0
  457. [Obsolete]
  458. #endif
  459. public static Assembly Load (String assemblyString, Evidence assemblySecurity)
  460. {
  461. return AppDomain.CurrentDomain.Load (assemblyString, assemblySecurity);
  462. }
  463. public static Assembly Load (AssemblyName assemblyRef)
  464. {
  465. return AppDomain.CurrentDomain.Load (assemblyRef);
  466. }
  467. #if NET_4_0
  468. [Obsolete]
  469. #endif
  470. public static Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
  471. {
  472. return AppDomain.CurrentDomain.Load (assemblyRef, assemblySecurity);
  473. }
  474. public static Assembly Load (Byte[] rawAssembly)
  475. {
  476. return AppDomain.CurrentDomain.Load (rawAssembly);
  477. }
  478. public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore)
  479. {
  480. return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
  481. }
  482. #if NET_4_0
  483. [Obsolete]
  484. #endif
  485. public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore,
  486. Evidence securityEvidence)
  487. {
  488. return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore, securityEvidence);
  489. }
  490. #if NET_4_0
  491. [MonoLimitation ("Argument securityContextSource is ignored")]
  492. public static Assembly Load (byte [] rawAssembly, byte [] rawSymbolStore, SecurityContextSource securityContextSource)
  493. {
  494. return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
  495. }
  496. #endif
  497. public static Assembly ReflectionOnlyLoad (byte[] rawAssembly)
  498. {
  499. return AppDomain.CurrentDomain.Load (rawAssembly, null, null, true);
  500. }
  501. public static Assembly ReflectionOnlyLoad (string assemblyString)
  502. {
  503. return AppDomain.CurrentDomain.Load (assemblyString, null, true);
  504. }
  505. public static Assembly ReflectionOnlyLoadFrom (string assemblyFile)
  506. {
  507. if (assemblyFile == null)
  508. throw new ArgumentNullException ("assemblyFile");
  509. return LoadFrom (assemblyFile, true);
  510. }
  511. #if NET_4_0
  512. [Obsolete]
  513. #endif
  514. public static Assembly LoadWithPartialName (string partialName)
  515. {
  516. return LoadWithPartialName (partialName, null);
  517. }
  518. [MonoTODO ("Not implemented")]
  519. public Module LoadModule (string moduleName, byte [] rawModule)
  520. {
  521. throw new NotImplementedException ();
  522. }
  523. [MonoTODO ("Not implemented")]
  524. public
  525. #if NET_4_0 || MOONLIGHT
  526. virtual
  527. #endif
  528. Module LoadModule (string moduleName, byte [] rawModule, byte [] rawSymbolStore)
  529. {
  530. throw new NotImplementedException ();
  531. }
  532. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  533. private static extern Assembly load_with_partial_name (string name, Evidence e);
  534. #if NET_4_0
  535. [Obsolete]
  536. #endif
  537. public static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence)
  538. {
  539. return LoadWithPartialName (partialName, securityEvidence, true);
  540. }
  541. /**
  542. * LAMESPEC: It is possible for this method to throw exceptions IF the name supplied
  543. * is a valid gac name and contains filesystem entry charachters at the end of the name
  544. * ie System/// will throw an exception. However ////System will not as that is canocolized
  545. * out of the name.
  546. */
  547. // FIXME: LoadWithPartialName must look cache (no CAS) or read from disk (CAS)
  548. internal static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence, bool oldBehavior)
  549. {
  550. if (!oldBehavior)
  551. throw new NotImplementedException ();
  552. if (partialName == null)
  553. throw new NullReferenceException ();
  554. return load_with_partial_name (partialName, securityEvidence);
  555. }
  556. public Object CreateInstance (String typeName)
  557. {
  558. return CreateInstance (typeName, false);
  559. }
  560. public Object CreateInstance (String typeName, Boolean ignoreCase)
  561. {
  562. Type t = GetType (typeName, false, ignoreCase);
  563. if (t == null)
  564. return null;
  565. try {
  566. return Activator.CreateInstance (t);
  567. } catch (InvalidOperationException) {
  568. throw new ArgumentException ("It is illegal to invoke a method on a Type loaded via ReflectionOnly methods.");
  569. }
  570. }
  571. public
  572. #if NET_4_0 || MOONLIGHT
  573. virtual
  574. #endif
  575. Object CreateInstance (String typeName, Boolean ignoreCase,
  576. BindingFlags bindingAttr, Binder binder,
  577. Object[] args, CultureInfo culture,
  578. Object[] activationAttributes)
  579. {
  580. Type t = GetType (typeName, false, ignoreCase);
  581. if (t == null)
  582. return null;
  583. try {
  584. return Activator.CreateInstance (t, bindingAttr, binder, args, culture, activationAttributes);
  585. } catch (InvalidOperationException) {
  586. throw new ArgumentException ("It is illegal to invoke a method on a Type loaded via ReflectionOnly methods.");
  587. }
  588. }
  589. public Module[] GetLoadedModules ()
  590. {
  591. return GetLoadedModules (false);
  592. }
  593. public Module[] GetModules ()
  594. {
  595. return GetModules (false);
  596. }
  597. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  598. internal virtual extern Module[] GetModulesInternal ();
  599. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  600. internal extern string[] GetNamespaces ();
  601. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  602. public extern virtual String[] GetManifestResourceNames ();
  603. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  604. public extern static Assembly GetExecutingAssembly ();
  605. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  606. public extern static Assembly GetCallingAssembly ();
  607. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  608. internal static extern AssemblyName[] GetReferencedAssemblies (Assembly module);
  609. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  610. private extern bool GetManifestResourceInfoInternal (String name, ManifestResourceInfo info);
  611. public virtual ManifestResourceInfo GetManifestResourceInfo (String resourceName)
  612. {
  613. if (resourceName == null)
  614. throw new ArgumentNullException ("resourceName");
  615. if (resourceName.Length == 0)
  616. throw new ArgumentException ("String cannot have zero length.");
  617. ManifestResourceInfo result = new ManifestResourceInfo ();
  618. bool found = GetManifestResourceInfoInternal (resourceName, result);
  619. if (found)
  620. return result;
  621. else
  622. return null;
  623. }
  624. private class ResourceCloseHandler {
  625. #pragma warning disable 169, 414
  626. Module module;
  627. #pragma warning restore 169, 414
  628. public ResourceCloseHandler (Module module) {
  629. this.module = module;
  630. }
  631. public void OnClose (object sender, EventArgs e) {
  632. // The module dtor will take care of things
  633. module = null;
  634. }
  635. }
  636. //
  637. // The following functions are only for the Mono Debugger.
  638. //
  639. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  640. internal static extern int MonoDebugger_GetMethodToken (MethodBase method);
  641. [MonoTODO ("Currently it always returns zero")]
  642. [ComVisible (false)]
  643. public
  644. #if NET_4_0 || MOONLIGHT
  645. virtual
  646. #endif
  647. long HostContext {
  648. get { return 0; }
  649. }
  650. internal virtual Module GetManifestModule () {
  651. return GetManifestModuleInternal ();
  652. }
  653. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  654. internal extern Module GetManifestModuleInternal ();
  655. [ComVisible (false)]
  656. public virtual extern bool ReflectionOnly {
  657. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  658. get;
  659. }
  660. public override bool Equals (object o)
  661. {
  662. if (((object) this) == o)
  663. return true;
  664. if (o == null)
  665. return false;
  666. Assembly other = (Assembly) o;
  667. return other._mono_assembly == _mono_assembly;
  668. }
  669. #if NET_4_0
  670. #if MOONLIGHT
  671. public virtual IList<CustomAttributeData> GetCustomAttributesData () {
  672. return CustomAttributeData.GetCustomAttributes (this);
  673. }
  674. #endif
  675. public PermissionSet PermissionSet {
  676. get { return this.GrantedPermissionSet; }
  677. }
  678. [MonoTODO]
  679. public bool IsFullyTrusted {
  680. get { return true; }
  681. }
  682. #endif
  683. #if !MOONLIGHT
  684. // Code Access Security
  685. internal void Resolve ()
  686. {
  687. lock (this) {
  688. // FIXME: As we (currently) delay the resolution until the first CAS
  689. // Demand it's too late to evaluate the Minimum permission set as a
  690. // condition to load the assembly into the AppDomain
  691. LoadAssemblyPermissions ();
  692. Evidence e = new Evidence (UnprotectedGetEvidence ()); // we need a copy to add PRE
  693. e.AddHost (new PermissionRequestEvidence (_minimum, _optional, _refuse));
  694. _granted = SecurityManager.ResolvePolicy (e,
  695. _minimum, _optional, _refuse, out _denied);
  696. }
  697. }
  698. internal PermissionSet GrantedPermissionSet {
  699. get {
  700. if (_granted == null) {
  701. if (SecurityManager.ResolvingPolicyLevel != null) {
  702. if (SecurityManager.ResolvingPolicyLevel.IsFullTrustAssembly (this))
  703. return DefaultPolicies.FullTrust;
  704. else
  705. return null; // we can't resolve during resolution
  706. }
  707. Resolve ();
  708. }
  709. return _granted;
  710. }
  711. }
  712. internal PermissionSet DeniedPermissionSet {
  713. get {
  714. // yes we look for granted, as denied may be null
  715. if (_granted == null) {
  716. if (SecurityManager.ResolvingPolicyLevel != null) {
  717. if (SecurityManager.ResolvingPolicyLevel.IsFullTrustAssembly (this))
  718. return null;
  719. else
  720. return DefaultPolicies.FullTrust; // deny unrestricted
  721. }
  722. Resolve ();
  723. }
  724. return _denied;
  725. }
  726. }
  727. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  728. extern internal static bool LoadPermissions (Assembly a,
  729. ref IntPtr minimum, ref int minLength,
  730. ref IntPtr optional, ref int optLength,
  731. ref IntPtr refused, ref int refLength);
  732. // Support for SecurityAction.RequestMinimum, RequestOptional and RequestRefuse
  733. private void LoadAssemblyPermissions ()
  734. {
  735. IntPtr minimum = IntPtr.Zero, optional = IntPtr.Zero, refused = IntPtr.Zero;
  736. int minLength = 0, optLength = 0, refLength = 0;
  737. if (LoadPermissions (this, ref minimum, ref minLength, ref optional,
  738. ref optLength, ref refused, ref refLength)) {
  739. // Note: no need to cache these permission sets as they will only be created once
  740. // at assembly resolution time.
  741. if (minLength > 0) {
  742. byte[] data = new byte [minLength];
  743. Marshal.Copy (minimum, data, 0, minLength);
  744. _minimum = SecurityManager.Decode (data);
  745. }
  746. if (optLength > 0) {
  747. byte[] data = new byte [optLength];
  748. Marshal.Copy (optional, data, 0, optLength);
  749. _optional = SecurityManager.Decode (data);
  750. }
  751. if (refLength > 0) {
  752. byte[] data = new byte [refLength];
  753. Marshal.Copy (refused, data, 0, refLength);
  754. _refuse = SecurityManager.Decode (data);
  755. }
  756. }
  757. }
  758. #endif
  759. #if NET_4_0 || MOONLIGHT
  760. static Exception CreateNIE ()
  761. {
  762. return new NotImplementedException ("Derived classes must implement it");
  763. }
  764. public virtual Type GetType (string name, bool throwOnError, bool ignoreCase)
  765. {
  766. throw CreateNIE ();
  767. }
  768. public virtual Module GetModule (String name)
  769. {
  770. throw CreateNIE ();
  771. }
  772. public virtual AssemblyName[] GetReferencedAssemblies ()
  773. {
  774. throw CreateNIE ();
  775. }
  776. public virtual Module[] GetModules (bool getResourceModules)
  777. {
  778. throw CreateNIE ();
  779. }
  780. [MonoTODO ("Always returns the same as GetModules")]
  781. public virtual Module[] GetLoadedModules (bool getResourceModules)
  782. {
  783. throw CreateNIE ();
  784. }
  785. public virtual Assembly GetSatelliteAssembly (CultureInfo culture)
  786. {
  787. throw CreateNIE ();
  788. }
  789. public virtual Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
  790. {
  791. throw CreateNIE ();
  792. }
  793. public virtual Module ManifestModule {
  794. get { throw CreateNIE (); }
  795. }
  796. public virtual bool GlobalAssemblyCache {
  797. get { throw CreateNIE (); }
  798. }
  799. public virtual bool IsDynamic {
  800. get { return false; }
  801. }
  802. public override int GetHashCode ()
  803. {
  804. return base.GetHashCode ();
  805. }
  806. public static bool operator == (Assembly left, Assembly right)
  807. {
  808. if ((object)left == (object)right)
  809. return true;
  810. if ((object)left == null ^ (object)right == null)
  811. return false;
  812. return left.Equals (right);
  813. }
  814. public static bool operator != (Assembly left, Assembly right)
  815. {
  816. if ((object)left == (object)right)
  817. return false;
  818. if ((object)left == null ^ (object)right == null)
  819. return true;
  820. return !left.Equals (right);
  821. }
  822. #endif
  823. }
  824. }
  825. #pragma warning restore 659