Assembly.cs 27 KB

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