Assembly.cs 27 KB

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