Assembly.cs 28 KB

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