Assembly.cs 27 KB

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