Assembly.cs 28 KB

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