Assembly.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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. internal 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. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  125. static internal extern string GetAotId ();
  126. // SECURITY: this should be the only caller to icall get_code_base
  127. private string GetCodeBase (bool escaped)
  128. {
  129. string cb = get_code_base (escaped);
  130. #if !NET_2_1
  131. if (SecurityManager.SecurityEnabled) {
  132. // we cannot divulge local file informations
  133. if (String.Compare ("FILE://", 0, cb, 0, 7, true, CultureInfo.InvariantCulture) == 0) {
  134. string file = cb.Substring (7);
  135. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, file).Demand ();
  136. }
  137. }
  138. #endif
  139. return cb;
  140. }
  141. public virtual string CodeBase {
  142. get { return GetCodeBase (false); }
  143. }
  144. public virtual string EscapedCodeBase {
  145. get { return GetCodeBase (true); }
  146. }
  147. public virtual string FullName {
  148. get {
  149. //
  150. // FIXME: This is wrong, but it gets us going
  151. // in the compiler for now
  152. //
  153. return ToString ();
  154. }
  155. }
  156. public virtual extern MethodInfo EntryPoint {
  157. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  158. get;
  159. }
  160. public virtual Evidence Evidence {
  161. [SecurityPermission (SecurityAction.Demand, ControlEvidence = true)]
  162. get { return UnprotectedGetEvidence (); }
  163. }
  164. // note: the security runtime requires evidences but may be unable to do so...
  165. internal Evidence UnprotectedGetEvidence ()
  166. {
  167. #if MOBILE
  168. return null;
  169. #else
  170. // if the host (runtime) hasn't provided it's own evidence...
  171. if (_evidence == null) {
  172. // ... we will provide our own
  173. lock (this) {
  174. _evidence = Evidence.GetDefaultHostEvidence (this);
  175. }
  176. }
  177. return _evidence;
  178. #endif
  179. }
  180. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  181. internal extern bool get_global_assembly_cache ();
  182. internal bool FromByteArray {
  183. set { fromByteArray = value; }
  184. }
  185. public virtual String Location {
  186. get {
  187. if (fromByteArray)
  188. return String.Empty;
  189. string loc = get_location ();
  190. #if !NET_2_1
  191. if ((loc != String.Empty) && SecurityManager.SecurityEnabled) {
  192. // we cannot divulge local file informations
  193. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, loc).Demand ();
  194. }
  195. #endif
  196. return loc;
  197. }
  198. }
  199. [ComVisible (false)]
  200. public virtual string ImageRuntimeVersion {
  201. get {
  202. return InternalImageRuntimeVersion ();
  203. }
  204. }
  205. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  206. {
  207. throw new NotImplementedException ();
  208. }
  209. public virtual bool IsDefined (Type attributeType, bool inherit)
  210. {
  211. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  212. }
  213. public virtual object [] GetCustomAttributes (bool inherit)
  214. {
  215. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  216. }
  217. public virtual object [] GetCustomAttributes (Type attributeType, bool inherit)
  218. {
  219. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  220. }
  221. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  222. private extern object GetFilesInternal (String name, bool getResourceModules);
  223. public virtual FileStream[] GetFiles ()
  224. {
  225. return GetFiles (false);
  226. }
  227. public virtual FileStream [] GetFiles (bool getResourceModules)
  228. {
  229. string[] names = (string[]) GetFilesInternal (null, getResourceModules);
  230. if (names == null)
  231. return EmptyArray<FileStream>.Value;
  232. string location = Location;
  233. FileStream[] res;
  234. if (location != String.Empty) {
  235. res = new FileStream [names.Length + 1];
  236. res [0] = new FileStream (location, FileMode.Open, FileAccess.Read);
  237. for (int i = 0; i < names.Length; ++i)
  238. res [i + 1] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
  239. } else {
  240. res = new FileStream [names.Length];
  241. for (int i = 0; i < names.Length; ++i)
  242. res [i] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
  243. }
  244. return res;
  245. }
  246. public virtual FileStream GetFile (String name)
  247. {
  248. if (name == null)
  249. throw new ArgumentNullException (null, "Name cannot be null.");
  250. if (name.Length == 0)
  251. throw new ArgumentException ("Empty name is not valid");
  252. string filename = (string)GetFilesInternal (name, true);
  253. if (filename != null)
  254. return new FileStream (filename, FileMode.Open, FileAccess.Read);
  255. else
  256. return null;
  257. }
  258. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  259. internal extern IntPtr GetManifestResourceInternal (String name, out int size, out Module module);
  260. public virtual Stream GetManifestResourceStream (String name)
  261. {
  262. if (name == null)
  263. throw new ArgumentNullException ("name");
  264. if (name.Length == 0)
  265. throw new ArgumentException ("String cannot have zero length.",
  266. "name");
  267. ManifestResourceInfo info = GetManifestResourceInfo (name);
  268. if (info == null) {
  269. Assembly a = AppDomain.CurrentDomain.DoResourceResolve (name, this);
  270. if (a != null && a != this)
  271. return a.GetManifestResourceStream (name);
  272. else
  273. return null;
  274. }
  275. if (info.ReferencedAssembly != null)
  276. return info.ReferencedAssembly.GetManifestResourceStream (name);
  277. if ((info.FileName != null) && (info.ResourceLocation == 0)) {
  278. if (fromByteArray)
  279. throw new FileNotFoundException (info.FileName);
  280. string location = Path.GetDirectoryName (Location);
  281. string filename = Path.Combine (location, info.FileName);
  282. return new FileStream (filename, FileMode.Open, FileAccess.Read);
  283. }
  284. int size;
  285. Module module;
  286. IntPtr data = GetManifestResourceInternal (name, out size, out module);
  287. if (data == (IntPtr) 0)
  288. return null;
  289. else {
  290. UnmanagedMemoryStream stream;
  291. unsafe {
  292. stream = new UnmanagedMemoryStreamForModule ((byte*) data, size, module);
  293. }
  294. return stream;
  295. }
  296. }
  297. public virtual Stream GetManifestResourceStream (Type type, String name)
  298. {
  299. StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  300. return GetManifestResourceStream(type, name, false, ref stackMark);
  301. }
  302. internal Stream GetManifestResourceStream (Type type, String name, bool skipSecurityCheck, ref StackCrawlMark stackMark)
  303. {
  304. StringBuilder sb = new StringBuilder ();
  305. if (type == null) {
  306. if (name == null)
  307. throw new ArgumentNullException ("type");
  308. } else {
  309. String nameSpace = type.Namespace;
  310. if (nameSpace != null) {
  311. sb.Append (nameSpace);
  312. if (name != null)
  313. sb.Append (Type.Delimiter);
  314. }
  315. }
  316. if (name != null)
  317. sb.Append(name);
  318. return GetManifestResourceStream (sb.ToString());
  319. }
  320. internal unsafe Stream GetManifestResourceStream(String name, ref StackCrawlMark stackMark, bool skipSecurityCheck)
  321. {
  322. return GetManifestResourceStream (null, name, skipSecurityCheck, ref stackMark);
  323. }
  324. internal String GetSimpleName()
  325. {
  326. AssemblyName aname = GetName (true);
  327. return aname.Name;
  328. }
  329. internal byte[] GetPublicKey()
  330. {
  331. AssemblyName aname = GetName (true);
  332. return aname.GetPublicKey ();
  333. }
  334. internal Version GetVersion()
  335. {
  336. AssemblyName aname = GetName (true);
  337. return aname.Version;
  338. }
  339. private AssemblyNameFlags GetFlags()
  340. {
  341. AssemblyName aname = GetName (true);
  342. return aname.Flags;
  343. }
  344. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  345. internal virtual extern Type[] GetTypes (bool exportedOnly);
  346. public virtual Type[] GetTypes ()
  347. {
  348. return GetTypes (false);
  349. }
  350. public virtual Type[] GetExportedTypes ()
  351. {
  352. return GetTypes (true);
  353. }
  354. public virtual Type GetType (String name, Boolean throwOnError)
  355. {
  356. return GetType (name, throwOnError, false);
  357. }
  358. public virtual Type GetType (String name) {
  359. return GetType (name, false, false);
  360. }
  361. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  362. internal extern Type InternalGetType (Module module, String name, Boolean throwOnError, Boolean ignoreCase);
  363. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  364. internal extern static void InternalGetAssemblyName (string assemblyFile, AssemblyName aname);
  365. public virtual AssemblyName GetName (Boolean copiedName)
  366. {
  367. throw new NotImplementedException ();
  368. }
  369. public virtual AssemblyName GetName ()
  370. {
  371. return GetName (false);
  372. }
  373. public override string ToString ()
  374. {
  375. // note: ToString work without requiring CodeBase (so no checks are needed)
  376. if (assemblyName != null)
  377. return assemblyName;
  378. assemblyName = get_fullname ();
  379. return assemblyName;
  380. }
  381. public static String CreateQualifiedName (String assemblyName, String typeName)
  382. {
  383. return typeName + ", " + assemblyName;
  384. }
  385. public static Assembly GetAssembly (Type type)
  386. {
  387. if (type != null)
  388. return type.Assembly;
  389. throw new ArgumentNullException ("type");
  390. }
  391. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  392. public static extern Assembly GetEntryAssembly();
  393. internal Assembly GetSatelliteAssembly (CultureInfo culture, Version version, bool throwOnError)
  394. {
  395. if (culture == null)
  396. throw new ArgumentNullException("culture");
  397. Contract.EndContractBlock();
  398. StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  399. String name = GetSimpleName() + ".resources";
  400. return InternalGetSatelliteAssembly(name, culture, version, true, ref stackMark);
  401. }
  402. internal RuntimeAssembly InternalGetSatelliteAssembly (String name, CultureInfo culture, Version version, bool throwOnFileNotFound, ref StackCrawlMark stackMark)
  403. {
  404. AssemblyName an = new AssemblyName ();
  405. an.SetPublicKey (GetPublicKey ());
  406. an.Flags = GetFlags () | AssemblyNameFlags.PublicKey;
  407. if (version == null)
  408. an.Version = GetVersion ();
  409. else
  410. an.Version = version;
  411. an.CultureInfo = culture;
  412. an.Name = name;
  413. Assembly assembly;
  414. try {
  415. assembly = AppDomain.CurrentDomain.LoadSatellite (an, false);
  416. if (assembly != null)
  417. return (RuntimeAssembly)assembly;
  418. } catch (FileNotFoundException) {
  419. assembly = null;
  420. // ignore
  421. }
  422. if (String.IsNullOrEmpty (Location))
  423. return null;
  424. // Try the assembly directory
  425. string location = Path.GetDirectoryName (Location);
  426. string fullName = Path.Combine (location, Path.Combine (culture.Name, an.Name + ".dll"));
  427. if (!throwOnFileNotFound && !File.Exists (fullName))
  428. return null;
  429. return (RuntimeAssembly)LoadFrom (fullName);
  430. }
  431. #if !MOBILE
  432. Type _Assembly.GetType ()
  433. {
  434. // Required or object::GetType becomes virtual final
  435. return base.GetType ();
  436. }
  437. #endif
  438. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  439. private extern static Assembly LoadFrom (String assemblyFile, bool refonly);
  440. public static Assembly LoadFrom (String assemblyFile)
  441. {
  442. return LoadFrom (assemblyFile, false);
  443. }
  444. [Obsolete]
  445. public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence)
  446. {
  447. Assembly a = LoadFrom (assemblyFile, false);
  448. #if !NET_2_1
  449. if ((a != null) && (securityEvidence != null)) {
  450. // merge evidence (i.e. replace defaults with provided evidences)
  451. a.Evidence.Merge (securityEvidence);
  452. }
  453. #endif
  454. return a;
  455. }
  456. [Obsolete]
  457. [MonoTODO("This overload is not currently implemented")]
  458. // FIXME: What are we missing?
  459. public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
  460. {
  461. throw new NotImplementedException ();
  462. }
  463. [MonoTODO]
  464. public static Assembly LoadFrom (String assemblyFile, byte [] hashValue, AssemblyHashAlgorithm hashAlgorithm)
  465. {
  466. throw new NotImplementedException ();
  467. }
  468. public static Assembly UnsafeLoadFrom (String assemblyFile)
  469. {
  470. return LoadFrom (assemblyFile);
  471. }
  472. [Obsolete]
  473. public static Assembly LoadFile (String path, Evidence securityEvidence)
  474. {
  475. if (path == null)
  476. throw new ArgumentNullException ("path");
  477. if (path == String.Empty)
  478. throw new ArgumentException ("Path can't be empty", "path");
  479. // FIXME: Make this do the right thing
  480. return LoadFrom (path, securityEvidence);
  481. }
  482. public static Assembly LoadFile (String path)
  483. {
  484. return LoadFile (path, null);
  485. }
  486. public static Assembly Load (String assemblyString)
  487. {
  488. return AppDomain.CurrentDomain.Load (assemblyString);
  489. }
  490. [Obsolete]
  491. public static Assembly Load (String assemblyString, Evidence assemblySecurity)
  492. {
  493. return AppDomain.CurrentDomain.Load (assemblyString, assemblySecurity);
  494. }
  495. public static Assembly Load (AssemblyName assemblyRef)
  496. {
  497. return AppDomain.CurrentDomain.Load (assemblyRef);
  498. }
  499. [Obsolete]
  500. public static Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
  501. {
  502. return AppDomain.CurrentDomain.Load (assemblyRef, assemblySecurity);
  503. }
  504. public static Assembly Load (Byte[] rawAssembly)
  505. {
  506. return AppDomain.CurrentDomain.Load (rawAssembly);
  507. }
  508. public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore)
  509. {
  510. return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
  511. }
  512. [Obsolete]
  513. public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore,
  514. Evidence securityEvidence)
  515. {
  516. return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore, securityEvidence);
  517. }
  518. [MonoLimitation ("Argument securityContextSource is ignored")]
  519. public static Assembly Load (byte [] rawAssembly, byte [] rawSymbolStore, SecurityContextSource securityContextSource)
  520. {
  521. return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
  522. }
  523. public static Assembly ReflectionOnlyLoad (byte[] rawAssembly)
  524. {
  525. return AppDomain.CurrentDomain.Load (rawAssembly, null, null, true);
  526. }
  527. public static Assembly ReflectionOnlyLoad (string assemblyString)
  528. {
  529. return AppDomain.CurrentDomain.Load (assemblyString, null, true);
  530. }
  531. public static Assembly ReflectionOnlyLoadFrom (string assemblyFile)
  532. {
  533. if (assemblyFile == null)
  534. throw new ArgumentNullException ("assemblyFile");
  535. return LoadFrom (assemblyFile, true);
  536. }
  537. [Obsolete]
  538. public static Assembly LoadWithPartialName (string partialName)
  539. {
  540. return LoadWithPartialName (partialName, null);
  541. }
  542. [MonoTODO ("Not implemented")]
  543. public Module LoadModule (string moduleName, byte [] rawModule)
  544. {
  545. throw new NotImplementedException ();
  546. }
  547. [MonoTODO ("Not implemented")]
  548. public
  549. virtual
  550. Module LoadModule (string moduleName, byte [] rawModule, byte [] rawSymbolStore)
  551. {
  552. throw new NotImplementedException ();
  553. }
  554. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  555. private static extern Assembly load_with_partial_name (string name, Evidence e);
  556. [Obsolete]
  557. public static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence)
  558. {
  559. return LoadWithPartialName (partialName, securityEvidence, true);
  560. }
  561. /**
  562. * LAMESPEC: It is possible for this method to throw exceptions IF the name supplied
  563. * is a valid gac name and contains filesystem entry charachters at the end of the name
  564. * ie System/// will throw an exception. However ////System will not as that is canocolized
  565. * out of the name.
  566. */
  567. // FIXME: LoadWithPartialName must look cache (no CAS) or read from disk (CAS)
  568. internal static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence, bool oldBehavior)
  569. {
  570. if (!oldBehavior)
  571. throw new NotImplementedException ();
  572. if (partialName == null)
  573. throw new NullReferenceException ();
  574. return load_with_partial_name (partialName, securityEvidence);
  575. }
  576. public Object CreateInstance (String typeName)
  577. {
  578. return CreateInstance (typeName, false);
  579. }
  580. public Object CreateInstance (String typeName, Boolean ignoreCase)
  581. {
  582. Type t = GetType (typeName, false, ignoreCase);
  583. if (t == null)
  584. return null;
  585. try {
  586. return Activator.CreateInstance (t);
  587. } catch (InvalidOperationException) {
  588. throw new ArgumentException ("It is illegal to invoke a method on a Type loaded via ReflectionOnly methods.");
  589. }
  590. }
  591. public
  592. virtual
  593. Object CreateInstance (String typeName, Boolean ignoreCase,
  594. BindingFlags bindingAttr, Binder binder,
  595. Object[] args, CultureInfo culture,
  596. Object[] activationAttributes)
  597. {
  598. Type t = GetType (typeName, false, ignoreCase);
  599. if (t == null)
  600. return null;
  601. try {
  602. return Activator.CreateInstance (t, bindingAttr, binder, args, culture, activationAttributes);
  603. } catch (InvalidOperationException) {
  604. throw new ArgumentException ("It is illegal to invoke a method on a Type loaded via ReflectionOnly methods.");
  605. }
  606. }
  607. public Module[] GetLoadedModules ()
  608. {
  609. return GetLoadedModules (false);
  610. }
  611. public Module[] GetModules ()
  612. {
  613. return GetModules (false);
  614. }
  615. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  616. internal virtual extern Module[] GetModulesInternal ();
  617. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  618. public extern virtual String[] GetManifestResourceNames ();
  619. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  620. public extern static Assembly GetExecutingAssembly ();
  621. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  622. public extern static Assembly GetCallingAssembly ();
  623. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  624. internal static extern AssemblyName[] GetReferencedAssemblies (Assembly module);
  625. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  626. private extern bool GetManifestResourceInfoInternal (String name, ManifestResourceInfo info);
  627. public virtual ManifestResourceInfo GetManifestResourceInfo (String resourceName)
  628. {
  629. if (resourceName == null)
  630. throw new ArgumentNullException ("resourceName");
  631. if (resourceName.Length == 0)
  632. throw new ArgumentException ("String cannot have zero length.");
  633. ManifestResourceInfo result = new ManifestResourceInfo (null, null, 0);
  634. bool found = GetManifestResourceInfoInternal (resourceName, result);
  635. if (found)
  636. return result;
  637. else
  638. return null;
  639. }
  640. [MonoTODO ("Currently it always returns zero")]
  641. [ComVisible (false)]
  642. public
  643. virtual
  644. long HostContext {
  645. get { return 0; }
  646. }
  647. internal virtual Module GetManifestModule () {
  648. return GetManifestModuleInternal ();
  649. }
  650. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  651. internal extern Module GetManifestModuleInternal ();
  652. [ComVisible (false)]
  653. public virtual extern bool ReflectionOnly {
  654. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  655. get;
  656. }
  657. public override int GetHashCode ()
  658. {
  659. return base.GetHashCode ();
  660. }
  661. public override bool Equals (object o)
  662. {
  663. if (((object) this) == o)
  664. return true;
  665. if (o == null)
  666. return false;
  667. Assembly other = (Assembly) o;
  668. return other._mono_assembly == _mono_assembly;
  669. }
  670. #if !NET_2_1
  671. // Code Access Security
  672. internal void Resolve ()
  673. {
  674. lock (this) {
  675. // FIXME: As we (currently) delay the resolution until the first CAS
  676. // Demand it's too late to evaluate the Minimum permission set as a
  677. // condition to load the assembly into the AppDomain
  678. LoadAssemblyPermissions ();
  679. Evidence e = new Evidence (UnprotectedGetEvidence ()); // we need a copy to add PRE
  680. e.AddHost (new PermissionRequestEvidence (_minimum, _optional, _refuse));
  681. _granted = SecurityManager.ResolvePolicy (e,
  682. _minimum, _optional, _refuse, out _denied);
  683. }
  684. }
  685. internal PermissionSet GrantedPermissionSet {
  686. get {
  687. if (_granted == null) {
  688. if (SecurityManager.ResolvingPolicyLevel != null) {
  689. if (SecurityManager.ResolvingPolicyLevel.IsFullTrustAssembly (this))
  690. return DefaultPolicies.FullTrust;
  691. else
  692. return null; // we can't resolve during resolution
  693. }
  694. Resolve ();
  695. }
  696. return _granted;
  697. }
  698. }
  699. internal PermissionSet DeniedPermissionSet {
  700. get {
  701. // yes we look for granted, as denied may be null
  702. if (_granted == null) {
  703. if (SecurityManager.ResolvingPolicyLevel != null) {
  704. if (SecurityManager.ResolvingPolicyLevel.IsFullTrustAssembly (this))
  705. return null;
  706. else
  707. return DefaultPolicies.FullTrust; // deny unrestricted
  708. }
  709. Resolve ();
  710. }
  711. return _denied;
  712. }
  713. }
  714. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  715. extern internal static bool LoadPermissions (Assembly a,
  716. ref IntPtr minimum, ref int minLength,
  717. ref IntPtr optional, ref int optLength,
  718. ref IntPtr refused, ref int refLength);
  719. // Support for SecurityAction.RequestMinimum, RequestOptional and RequestRefuse
  720. private void LoadAssemblyPermissions ()
  721. {
  722. IntPtr minimum = IntPtr.Zero, optional = IntPtr.Zero, refused = IntPtr.Zero;
  723. int minLength = 0, optLength = 0, refLength = 0;
  724. if (LoadPermissions (this, ref minimum, ref minLength, ref optional,
  725. ref optLength, ref refused, ref refLength)) {
  726. // Note: no need to cache these permission sets as they will only be created once
  727. // at assembly resolution time.
  728. if (minLength > 0) {
  729. byte[] data = new byte [minLength];
  730. Marshal.Copy (minimum, data, 0, minLength);
  731. _minimum = SecurityManager.Decode (data);
  732. }
  733. if (optLength > 0) {
  734. byte[] data = new byte [optLength];
  735. Marshal.Copy (optional, data, 0, optLength);
  736. _optional = SecurityManager.Decode (data);
  737. }
  738. if (refLength > 0) {
  739. byte[] data = new byte [refLength];
  740. Marshal.Copy (refused, data, 0, refLength);
  741. _refuse = SecurityManager.Decode (data);
  742. }
  743. }
  744. }
  745. public virtual PermissionSet PermissionSet {
  746. get { return this.GrantedPermissionSet; }
  747. }
  748. public virtual SecurityRuleSet SecurityRuleSet {
  749. get { throw CreateNIE (); }
  750. }
  751. #endif
  752. static Exception CreateNIE ()
  753. {
  754. return new NotImplementedException ("Derived classes must implement it");
  755. }
  756. public virtual IList<CustomAttributeData> GetCustomAttributesData ()
  757. {
  758. return CustomAttributeData.GetCustomAttributes (this);
  759. }
  760. [MonoTODO]
  761. public bool IsFullyTrusted {
  762. get { return true; }
  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 static bool operator == (Assembly left, Assembly right)
  803. {
  804. if ((object)left == (object)right)
  805. return true;
  806. if ((object)left == null ^ (object)right == null)
  807. return false;
  808. return left.Equals (right);
  809. }
  810. public static bool operator != (Assembly left, Assembly right)
  811. {
  812. if ((object)left == (object)right)
  813. return false;
  814. if ((object)left == null ^ (object)right == null)
  815. return true;
  816. return !left.Equals (right);
  817. }
  818. public virtual IEnumerable<TypeInfo> DefinedTypes {
  819. get {
  820. foreach (var type in GetTypes ()) {
  821. yield return type.GetTypeInfo ();
  822. }
  823. }
  824. }
  825. public virtual IEnumerable<Type> ExportedTypes {
  826. get { return GetExportedTypes (); }
  827. }
  828. public virtual IEnumerable<Module> Modules {
  829. get { return GetModules (); }
  830. }
  831. public virtual IEnumerable<CustomAttributeData> CustomAttributes {
  832. get { return GetCustomAttributesData (); }
  833. }
  834. }
  835. }