2 // System.Reflection/Assembly.cs
5 // Paolo Molaro (lupus@ximian.com)
7 // (C) 2001 Ximian, Inc. http://www.ximian.com
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using System.Security;
31 using System.Security.Policy;
32 using System.Security.Permissions;
33 using System.Runtime.Serialization;
34 using System.Reflection.Emit;
36 using System.Globalization;
37 using System.Runtime.CompilerServices;
38 using System.Runtime.InteropServices;
39 using System.Collections;
40 using System.Configuration.Assemblies;
44 namespace System.Reflection {
48 [ComDefaultInterfaceAttribute (typeof (_Assembly))]
51 [ClassInterface(ClassInterfaceType.None)]
52 public class Assembly : System.Reflection.ICustomAttributeProvider, _Assembly,
53 System.Security.IEvidenceFactory, System.Runtime.Serialization.ISerializable {
55 internal class ResolveEventHolder {
56 public event ModuleResolveEventHandler ModuleResolve;
59 // Note: changes to fields must be reflected in _MonoReflectionAssembly struct (object-internals.h)
60 #pragma warning disable 169
61 private IntPtr _mono_assembly;
62 #pragma warning restore 169
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;
76 resolve_event_holder = new ResolveEventHolder ();
80 // We can't store the event directly in this class, since the
81 // compiler would silently insert the fields before _mono_assembly
83 public event ModuleResolveEventHandler ModuleResolve {
84 [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
86 resolve_event_holder.ModuleResolve += value;
88 [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
90 resolve_event_holder.ModuleResolve -= value;
94 [MethodImplAttribute (MethodImplOptions.InternalCall)]
95 private extern string get_code_base (bool escaped);
97 [MethodImplAttribute (MethodImplOptions.InternalCall)]
98 private extern string get_fullname ();
100 [MethodImplAttribute (MethodImplOptions.InternalCall)]
101 private extern string get_location ();
103 [MethodImplAttribute (MethodImplOptions.InternalCall)]
104 private extern string InternalImageRuntimeVersion ();
106 [MethodImplAttribute (MethodImplOptions.InternalCall)]
107 private extern bool get_global_assembly_cache ();
109 // SECURITY: this should be the only caller to icall get_code_base
110 private string GetCodeBase (bool escaped)
112 string cb = get_code_base (escaped);
114 if (SecurityManager.SecurityEnabled) {
115 // we cannot divulge local file informations
116 if (String.Compare ("FILE://", 0, cb, 0, 7, true, CultureInfo.InvariantCulture) == 0) {
117 string file = cb.Substring (7);
118 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, file).Demand ();
125 public virtual string CodeBase {
126 get { return GetCodeBase (false); }
129 public virtual string EscapedCodeBase {
130 get { return GetCodeBase (true); }
133 public virtual string FullName {
136 // FIXME: This is wrong, but it gets us going
137 // in the compiler for now
143 public virtual extern MethodInfo EntryPoint {
144 [MethodImplAttribute (MethodImplOptions.InternalCall)]
148 public virtual Evidence Evidence {
149 [SecurityPermission (SecurityAction.Demand, ControlEvidence = true)]
150 get { return UnprotectedGetEvidence (); }
153 // note: the security runtime requires evidences but may be unable to do so...
154 internal Evidence UnprotectedGetEvidence ()
156 // if the host (runtime) hasn't provided it's own evidence...
157 if (_evidence == null) {
158 // ... we will provide our own
160 _evidence = Evidence.GetDefaultHostEvidence (this);
166 public bool GlobalAssemblyCache {
168 return get_global_assembly_cache ();
172 internal bool FromByteArray {
173 set { fromByteArray = value; }
176 public virtual String Location {
181 string loc = get_location ();
183 if ((loc != String.Empty) && SecurityManager.SecurityEnabled) {
184 // we cannot divulge local file informations
185 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, loc).Demand ();
194 public virtual string ImageRuntimeVersion {
196 return InternalImageRuntimeVersion ();
201 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
202 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
205 throw new ArgumentNullException ("info");
207 UnitySerializationHolder.GetAssemblyData (this, info, context);
211 public new Type GetType ()
213 return base.GetType ();
217 public virtual bool IsDefined (Type attributeType, bool inherit)
219 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
222 public virtual object [] GetCustomAttributes (bool inherit)
224 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
227 public virtual object [] GetCustomAttributes (Type attributeType, bool inherit)
229 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
232 [MethodImplAttribute (MethodImplOptions.InternalCall)]
233 private extern object GetFilesInternal (String name, bool getResourceModules);
235 public virtual FileStream[] GetFiles ()
237 return GetFiles (false);
240 public virtual FileStream [] GetFiles (bool getResourceModules)
242 string[] names = (string[]) GetFilesInternal (null, getResourceModules);
244 return new FileStream [0];
246 string location = Location;
249 if (location != String.Empty) {
250 res = new FileStream [names.Length + 1];
251 res [0] = new FileStream (location, FileMode.Open, FileAccess.Read);
252 for (int i = 0; i < names.Length; ++i)
253 res [i + 1] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
255 res = new FileStream [names.Length];
256 for (int i = 0; i < names.Length; ++i)
257 res [i] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
262 public virtual FileStream GetFile (String name)
265 throw new ArgumentNullException (null, "Name cannot be null.");
266 if (name.Length == 0)
267 throw new ArgumentException ("Empty name is not valid");
269 string filename = (string)GetFilesInternal (name, true);
270 if (filename != null)
271 return new FileStream (filename, FileMode.Open, FileAccess.Read);
276 [MethodImplAttribute (MethodImplOptions.InternalCall)]
277 internal extern IntPtr GetManifestResourceInternal (String name, out int size, out Module module);
279 public virtual Stream GetManifestResourceStream (String name)
282 throw new ArgumentNullException ("name");
283 if (name.Length == 0)
284 throw new ArgumentException ("String cannot have zero length.",
287 ManifestResourceInfo info = GetManifestResourceInfo (name);
291 if (info.ReferencedAssembly != null)
292 return info.ReferencedAssembly.GetManifestResourceStream (name);
293 if ((info.FileName != null) && (info.ResourceLocation == 0)) {
296 throw new FileNotFoundException (info.FileName);
301 string filename = Path.Combine (Path.GetDirectoryName (Location),
303 return new FileStream (filename, FileMode.Open, FileAccess.Read);
308 IntPtr data = GetManifestResourceInternal (name, out size, out module);
309 if (data == (IntPtr) 0)
313 UnmanagedMemoryStream stream;
315 stream = new UnmanagedMemoryStream ((byte*) data, size);
318 IntPtrStream stream = new IntPtrStream (data, size);
321 * The returned pointer points inside metadata, so
322 * we have to increase the refcount of the module, and decrease
323 * it when the stream is finalized.
325 stream.Closed += new EventHandler (new ResourceCloseHandler (module).OnClose);
330 public virtual Stream GetManifestResourceStream (Type type, String name)
337 throw new ArgumentNullException ("type");
341 if (ns == null || ns.Length == 0)
342 return GetManifestResourceStream (name);
344 return GetManifestResourceStream (ns + "." + name);
347 [MethodImplAttribute (MethodImplOptions.InternalCall)]
348 internal virtual extern Type[] GetTypes (bool exportedOnly);
350 public virtual Type[] GetTypes ()
352 return GetTypes (false);
355 public virtual Type[] GetExportedTypes ()
357 return GetTypes (true);
360 public virtual Type GetType (String name, Boolean throwOnError)
362 return GetType (name, throwOnError, false);
365 public virtual Type GetType (String name) {
366 return GetType (name, false, false);
369 [MethodImplAttribute (MethodImplOptions.InternalCall)]
370 internal extern Type InternalGetType (Module module, String name, Boolean throwOnError, Boolean ignoreCase);
372 public Type GetType (string name, bool throwOnError, bool ignoreCase)
375 throw new ArgumentNullException (name);
376 if (name.Length == 0)
377 throw new ArgumentException ("name", "Name cannot be empty");
379 return InternalGetType (null, name, throwOnError, ignoreCase);
382 [MethodImplAttribute (MethodImplOptions.InternalCall)]
383 internal extern static void InternalGetAssemblyName (string assemblyFile, AssemblyName aname);
385 [MethodImplAttribute (MethodImplOptions.InternalCall)]
386 static extern void FillName (Assembly ass, AssemblyName aname);
388 [MonoTODO ("copiedName == true is not supported")]
389 public virtual AssemblyName GetName (Boolean copiedName)
391 // CodeBase, which is restricted, will be copied into the AssemblyName object so...
392 if (SecurityManager.SecurityEnabled) {
393 GetCodeBase (true); // this will ensure the Demand is made
395 return UnprotectedGetName ();
398 public virtual AssemblyName GetName ()
400 return GetName (false);
403 // the security runtime requires access to the assemblyname (e.g. to get the strongname)
404 internal virtual AssemblyName UnprotectedGetName ()
406 AssemblyName aname = new AssemblyName ();
407 FillName (this, aname);
411 public override string ToString ()
413 // note: ToString work without requiring CodeBase (so no checks are needed)
415 if (assemblyName != null)
418 assemblyName = get_fullname ();
422 public static String CreateQualifiedName (String assemblyName, String typeName)
424 return typeName + ", " + assemblyName;
427 public static Assembly GetAssembly (Type type)
430 return type.Assembly;
431 throw new ArgumentNullException ("type");
435 [MethodImplAttribute (MethodImplOptions.InternalCall)]
436 public static extern Assembly GetEntryAssembly();
438 public Assembly GetSatelliteAssembly (CultureInfo culture)
440 return GetSatelliteAssembly (culture, null);
443 public Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
446 throw new ArgumentException ("culture");
448 AssemblyName aname = GetName (true);
450 aname.Version = version;
452 aname.CultureInfo = culture;
453 aname.Name = aname.Name + ".resources";
454 Assembly assembly = AppDomain.CurrentDomain.LoadSatellite (aname);
455 if (assembly != null)
458 // Try the assembly directory
459 string fullName = Path.Combine (Path.GetDirectoryName (Location), Path.Combine (culture.Name, aname.Name + ".dll"));
460 return LoadFrom (fullName);
463 internal Assembly GetSatelliteAssemblyNoThrow (CultureInfo culture, Version version)
466 throw new ArgumentException ("culture");
468 AssemblyName aname = GetName (true);
470 aname.Version = version;
472 aname.CultureInfo = culture;
473 aname.Name = aname.Name + ".resources";
474 Assembly assembly = AppDomain.CurrentDomain.LoadSatellite (aname);
475 if (assembly != null)
478 // Try the assembly directory
479 string fullName = Path.Combine (Path.GetDirectoryName (Location), Path.Combine (culture.Name, aname.Name + ".dll"));
480 if (!File.Exists (fullName))
482 return LoadFrom (fullName);
485 [MethodImplAttribute (MethodImplOptions.InternalCall)]
486 private extern static Assembly LoadFrom (String assemblyFile, bool refonly);
488 public static Assembly LoadFrom (String assemblyFile)
490 return LoadFrom (assemblyFile, false);
493 public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence)
495 Assembly a = LoadFrom (assemblyFile, false);
496 if ((a != null) && (securityEvidence != null)) {
497 // merge evidence (i.e. replace defaults with provided evidences)
498 a.Evidence.Merge (securityEvidence);
505 [MonoTODO("This overload is not currently implemented")]
506 // FIXME: What are we missing?
507 public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
509 if (assemblyFile == null)
510 throw new ArgumentNullException ("assemblyFile");
511 if (assemblyFile == String.Empty)
512 throw new ArgumentException ("Name can't be the empty string", "assemblyFile");
513 throw new NotImplementedException ();
516 public static Assembly LoadFile (String path, Evidence securityEvidence)
519 throw new ArgumentNullException ("path");
520 if (path == String.Empty)
521 throw new ArgumentException ("Path can't be empty", "path");
522 // FIXME: Make this do the right thing
523 return LoadFrom (path, securityEvidence);
526 public static Assembly LoadFile (String path)
528 return LoadFile (path, null);
532 public static Assembly Load (String assemblyString)
534 return AppDomain.CurrentDomain.Load (assemblyString);
537 public static Assembly Load (String assemblyString, Evidence assemblySecurity)
539 return AppDomain.CurrentDomain.Load (assemblyString, assemblySecurity);
542 public static Assembly Load (AssemblyName assemblyRef)
544 return AppDomain.CurrentDomain.Load (assemblyRef);
547 public static Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
549 return AppDomain.CurrentDomain.Load (assemblyRef, assemblySecurity);
552 public static Assembly Load (Byte[] rawAssembly)
554 return AppDomain.CurrentDomain.Load (rawAssembly);
557 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore)
559 return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
562 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore,
563 Evidence securityEvidence)
565 return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore, securityEvidence);
568 #if NET_2_0 || BOOTSTRAP_NET_2_0
569 public static Assembly ReflectionOnlyLoad (byte[] rawAssembly)
571 return AppDomain.CurrentDomain.Load (rawAssembly, null, null, true);
574 public static Assembly ReflectionOnlyLoad (string assemblyString)
576 return AppDomain.CurrentDomain.Load (assemblyString, null, true);
579 public static Assembly ReflectionOnlyLoadFrom (string assemblyFile)
581 if (assemblyFile == null)
582 throw new ArgumentNullException ("assemblyFile");
584 return LoadFrom (assemblyFile, true);
591 public static Assembly LoadWithPartialName (string partialName)
593 return LoadWithPartialName (partialName, null);
596 [MonoTODO ("Not implemented")]
597 public Module LoadModule (string moduleName, byte [] rawModule)
599 throw new NotImplementedException ();
602 [MonoTODO ("Not implemented")]
603 public Module LoadModule (string moduleName, byte [] rawModule, byte [] rawSymbolStore)
605 throw new NotImplementedException ();
608 [MethodImplAttribute (MethodImplOptions.InternalCall)]
609 private static extern Assembly load_with_partial_name (string name, Evidence e);
614 public static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence)
616 return LoadWithPartialName (partialName, securityEvidence, true);
620 * LAMESPEC: It is possible for this method to throw exceptions IF the name supplied
621 * is a valid gac name and contains filesystem entry charachters at the end of the name
622 * ie System/// will throw an exception. However ////System will not as that is canocolized
626 // FIXME: LoadWithPartialName must look cache (no CAS) or read from disk (CAS)
627 internal static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence, bool oldBehavior)
630 throw new NotImplementedException ();
632 if (partialName == null)
633 throw new NullReferenceException ();
635 return load_with_partial_name (partialName, securityEvidence);
638 public Object CreateInstance (String typeName)
640 return CreateInstance (typeName, false);
643 public Object CreateInstance (String typeName, Boolean ignoreCase)
645 Type t = GetType (typeName, false, ignoreCase);
650 return Activator.CreateInstance (t);
651 } catch (InvalidOperationException) {
652 throw new ArgumentException ("It is illegal to invoke a method on a Type loaded via ReflectionOnly methods.");
656 public Object CreateInstance (String typeName, Boolean ignoreCase,
657 BindingFlags bindingAttr, Binder binder,
658 Object[] args, CultureInfo culture,
659 Object[] activationAttributes)
661 Type t = GetType (typeName, false, ignoreCase);
666 return Activator.CreateInstance (t, bindingAttr, binder, args, culture, activationAttributes);
667 } catch (InvalidOperationException) {
668 throw new ArgumentException ("It is illegal to invoke a method on a Type loaded via ReflectionOnly methods.");
672 public Module[] GetLoadedModules ()
674 return GetLoadedModules (false);
677 // FIXME: Currently, the two sets of modules are equal
678 public Module[] GetLoadedModules (bool getResourceModules)
680 return GetModules (getResourceModules);
683 public Module[] GetModules ()
685 return GetModules (false);
688 public Module GetModule (String name)
691 throw new ArgumentNullException ("name");
692 if (name.Length == 0)
693 throw new ArgumentException ("Name can't be empty");
695 Module[] modules = GetModules (true);
696 foreach (Module module in modules) {
697 if (module.ScopeName == name)
704 [MethodImplAttribute (MethodImplOptions.InternalCall)]
705 internal virtual extern Module[] GetModulesInternal ();
707 public Module[] GetModules (bool getResourceModules) {
708 Module[] modules = GetModulesInternal ();
710 if (!getResourceModules) {
711 ArrayList result = new ArrayList (modules.Length);
712 foreach (Module m in modules)
713 if (!m.IsResource ())
715 return (Module[])result.ToArray (typeof (Module));
721 [MethodImplAttribute (MethodImplOptions.InternalCall)]
722 internal extern string[] GetNamespaces ();
724 [MethodImplAttribute (MethodImplOptions.InternalCall)]
725 public extern virtual String[] GetManifestResourceNames ();
727 [MethodImplAttribute (MethodImplOptions.InternalCall)]
728 public extern static Assembly GetExecutingAssembly ();
730 [MethodImplAttribute (MethodImplOptions.InternalCall)]
731 public extern static Assembly GetCallingAssembly ();
733 [MethodImplAttribute (MethodImplOptions.InternalCall)]
734 public extern AssemblyName[] GetReferencedAssemblies ();
736 [MethodImplAttribute (MethodImplOptions.InternalCall)]
737 private extern bool GetManifestResourceInfoInternal (String name, ManifestResourceInfo info);
739 public virtual ManifestResourceInfo GetManifestResourceInfo (String resourceName)
741 if (resourceName == null)
742 throw new ArgumentNullException ("resourceName");
743 if (resourceName.Length == 0)
744 throw new ArgumentException ("String cannot have zero length.");
745 ManifestResourceInfo result = new ManifestResourceInfo ();
746 bool found = GetManifestResourceInfoInternal (resourceName, result);
753 private class ResourceCloseHandler {
754 #pragma warning disable 169, 414
756 #pragma warning restore 169, 414
758 public ResourceCloseHandler (Module module) {
759 this.module = module;
762 public void OnClose (object sender, EventArgs e) {
763 // The module dtor will take care of things
769 // The following functions are only for the Mono Debugger.
772 [MethodImplAttribute (MethodImplOptions.InternalCall)]
773 internal static extern int MonoDebugger_GetMethodToken (MethodBase method);
775 #if NET_2_0 || BOOTSTRAP_NET_2_0
776 [MonoTODO ("Always returns zero")]
778 public long HostContext {
783 public Module ManifestModule {
785 return GetManifestModule ();
789 internal virtual Module GetManifestModule () {
790 return GetManifestModuleInternal ();
793 [MethodImplAttribute (MethodImplOptions.InternalCall)]
794 internal extern Module GetManifestModuleInternal ();
797 public virtual extern bool ReflectionOnly {
798 [MethodImplAttribute (MethodImplOptions.InternalCall)]
803 #if !NET_2_1 || MONOTOUCH
804 // Code Access Security
806 internal void Resolve ()
809 // FIXME: As we (currently) delay the resolution until the first CAS
810 // Demand it's too late to evaluate the Minimum permission set as a
811 // condition to load the assembly into the AppDomain
812 LoadAssemblyPermissions ();
813 Evidence e = new Evidence (UnprotectedGetEvidence ()); // we need a copy to add PRE
814 e.AddHost (new PermissionRequestEvidence (_minimum, _optional, _refuse));
815 _granted = SecurityManager.ResolvePolicy (e,
816 _minimum, _optional, _refuse, out _denied);
820 internal PermissionSet GrantedPermissionSet {
822 if (_granted == null) {
823 if (SecurityManager.ResolvingPolicyLevel != null) {
824 if (SecurityManager.ResolvingPolicyLevel.IsFullTrustAssembly (this))
825 return DefaultPolicies.FullTrust;
827 return null; // we can't resolve during resolution
835 internal PermissionSet DeniedPermissionSet {
837 // yes we look for granted, as denied may be null
838 if (_granted == null) {
839 if (SecurityManager.ResolvingPolicyLevel != null) {
840 if (SecurityManager.ResolvingPolicyLevel.IsFullTrustAssembly (this))
843 return DefaultPolicies.FullTrust; // deny unrestricted
851 [MethodImplAttribute (MethodImplOptions.InternalCall)]
852 extern internal static bool LoadPermissions (Assembly a,
853 ref IntPtr minimum, ref int minLength,
854 ref IntPtr optional, ref int optLength,
855 ref IntPtr refused, ref int refLength);
857 // Support for SecurityAction.RequestMinimum, RequestOptional and RequestRefuse
858 private void LoadAssemblyPermissions ()
860 IntPtr minimum = IntPtr.Zero, optional = IntPtr.Zero, refused = IntPtr.Zero;
861 int minLength = 0, optLength = 0, refLength = 0;
862 if (LoadPermissions (this, ref minimum, ref minLength, ref optional,
863 ref optLength, ref refused, ref refLength)) {
865 // Note: no need to cache these permission sets as they will only be created once
866 // at assembly resolution time.
868 byte[] data = new byte [minLength];
869 Marshal.Copy (minimum, data, 0, minLength);
870 _minimum = SecurityManager.Decode (data);
873 byte[] data = new byte [optLength];
874 Marshal.Copy (optional, data, 0, optLength);
875 _optional = SecurityManager.Decode (data);
878 byte[] data = new byte [refLength];
879 Marshal.Copy (refused, data, 0, refLength);
880 _refuse = SecurityManager.Decode (data);