Assembly.cs 31 KB

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