2 // System.Reflection/Assembly.cs
5 // Paolo Molaro (lupus@ximian.com)
7 // (C) 2001 Ximian, Inc. http://www.ximian.com
8 // Copyright (C) 2004 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.
31 using System.Security;
32 using System.Security.Policy;
33 using System.Security.Permissions;
34 using System.Runtime.Serialization;
35 using System.Reflection.Emit;
37 using System.Globalization;
38 using System.Runtime.CompilerServices;
39 using System.Runtime.InteropServices;
40 using System.Collections;
41 using System.Configuration.Assemblies;
45 namespace System.Reflection {
48 [ClassInterface(ClassInterfaceType.None)]
49 public class Assembly : System.Reflection.ICustomAttributeProvider,
50 System.Security.IEvidenceFactory, System.Runtime.Serialization.ISerializable {
51 internal class ResolveEventHolder {
52 public event ModuleResolveEventHandler ModuleResolve;
55 // Note: changes to fields must be reflected in _MonoReflectionAssembly struct (object-internals.h)
56 private IntPtr _mono_assembly;
58 private ResolveEventHolder resolve_event_holder;
59 private Evidence _evidence;
60 internal PermissionSet _minimum; // for SecurityAction.RequestMinimum
61 internal PermissionSet _optional; // for SecurityAction.RequestOptional
62 internal PermissionSet _refuse; // for SecurityAction.RequestRefuse
63 private PermissionSet _granted; // for the resolved assembly granted permissions
64 private PermissionSet _denied; // for the resolved assembly denied permissions
65 private bool fromByteArray;
69 resolve_event_holder = new ResolveEventHolder ();
73 // We can't store the event directly in this class, since the
74 // compiler would silently insert the fields before _mono_assembly
76 public event ModuleResolveEventHandler ModuleResolve {
77 [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
79 resolve_event_holder.ModuleResolve += value;
81 [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
83 resolve_event_holder.ModuleResolve -= value;
87 [MethodImplAttribute (MethodImplOptions.InternalCall)]
88 private extern string get_code_base ();
90 [MethodImplAttribute (MethodImplOptions.InternalCall)]
91 private extern string get_location ();
93 [MethodImplAttribute (MethodImplOptions.InternalCall)]
94 private extern string InternalImageRuntimeVersion ();
96 [MethodImplAttribute (MethodImplOptions.InternalCall)]
97 private extern bool get_global_assembly_cache ();
99 // SECURITY: this should be the only caller to icall get_code_base
100 private string GetCodeBase ()
102 string cb = get_code_base ();
103 if (SecurityManager.SecurityEnabled) {
104 // we cannot divulge local file informations
105 if (String.Compare ("FILE://", 0, cb, 0, 7, true, CultureInfo.InvariantCulture) == 0) {
106 string file = cb.Substring (7);
107 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, file).Demand ();
113 public virtual string CodeBase {
114 get { return GetCodeBase (); }
117 public virtual string EscapedCodeBase {
118 get { return Uri.EscapeString (GetCodeBase (), false, true, true); }
121 public virtual string FullName {
124 // FIXME: This is wrong, but it gets us going
125 // in the compiler for now
131 public virtual extern MethodInfo EntryPoint {
132 [MethodImplAttribute (MethodImplOptions.InternalCall)]
136 public virtual Evidence Evidence {
137 [SecurityPermission (SecurityAction.Demand, ControlEvidence = true)]
138 get { return UnprotectedGetEvidence (); }
141 // note: the security runtime requires evidences but may be unable to do so...
142 internal Evidence UnprotectedGetEvidence ()
144 // if the host (runtime) hasn't provided it's own evidence...
145 if (_evidence == null) {
146 // ... we will provide our own
148 _evidence = Evidence.GetDefaultHostEvidence (this);
154 public bool GlobalAssemblyCache {
156 return get_global_assembly_cache ();
160 internal bool FromByteArray {
161 set { fromByteArray = true; }
164 public virtual String Location {
169 string loc = get_location ();
170 if ((loc != String.Empty) && SecurityManager.SecurityEnabled) {
171 // we cannot divulge local file informations
172 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, loc).Demand ();
180 public virtual string ImageRuntimeVersion {
182 return InternalImageRuntimeVersion ();
187 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
188 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
191 throw new ArgumentNullException ("info");
193 UnitySerializationHolder.GetAssemblyData (this, info, context);
196 public virtual bool IsDefined (Type attributeType, bool inherit)
198 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
201 public virtual object [] GetCustomAttributes (bool inherit)
203 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
206 public virtual object [] GetCustomAttributes (Type attributeType, bool inherit)
208 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
211 [MethodImplAttribute (MethodImplOptions.InternalCall)]
212 private extern object GetFilesInternal (String name, bool getResourceModules);
214 public virtual FileStream[] GetFiles ()
216 return GetFiles (false);
219 public virtual FileStream [] GetFiles (bool getResourceModules)
221 string[] names = (string[]) GetFilesInternal (null, getResourceModules);
223 return new FileStream [0];
225 FileStream[] res = new FileStream [names.Length];
226 for (int i = 0; i < names.Length; ++i)
227 res [i] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
231 public virtual FileStream GetFile (String name)
234 throw new ArgumentNullException ("name");
235 if (name.Length == 0)
236 throw new ArgumentException ("name");
238 string filename = (string)GetFilesInternal (name, true);
239 if (filename != null)
240 return new FileStream (filename, FileMode.Open, FileAccess.Read);
245 [MethodImplAttribute (MethodImplOptions.InternalCall)]
246 private extern IntPtr GetManifestResourceInternal (String name, out int size, out Module module);
248 public virtual Stream GetManifestResourceStream (String name)
251 throw new ArgumentNullException ("name");
253 throw new ArgumentException ("name cannot have zero length.");
255 ManifestResourceInfo info = GetManifestResourceInfo (name);
259 if (info.ReferencedAssembly != null)
260 return info.ReferencedAssembly.GetManifestResourceStream (name);
261 if ((info.FileName != null) && (info.ResourceLocation == 0)) {
262 string filename = Path.Combine (Path.GetDirectoryName (Location),
264 return new FileStream (filename, FileMode.Open, FileAccess.Read);
269 IntPtr data = GetManifestResourceInternal (name, out size, out module);
270 if (data == (IntPtr) 0)
273 IntPtrStream stream = new IntPtrStream (data, size);
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.
279 stream.Closed += new EventHandler (new ResourceCloseHandler (module).OnClose);
284 public virtual Stream GetManifestResourceStream (Type type, String name)
292 if ((ns == null) || (ns == ""))
293 return GetManifestResourceStream (name);
295 return GetManifestResourceStream (ns + "." + name);
298 [MethodImplAttribute (MethodImplOptions.InternalCall)]
299 private extern Type[] GetTypes (bool exportedOnly);
301 public virtual Type[] GetTypes ()
303 return GetTypes (false);
306 public virtual Type[] GetExportedTypes ()
308 return GetTypes (true);
311 public virtual Type GetType (String name, Boolean throwOnError)
313 return GetType (name, throwOnError, false);
316 public virtual Type GetType (String name) {
317 return GetType (name, false, false);
320 [MethodImplAttribute (MethodImplOptions.InternalCall)]
321 internal extern Type InternalGetType (Module module, String name, Boolean throwOnError, Boolean ignoreCase);
323 public Type GetType (string name, bool throwOnError, bool ignoreCase)
326 throw new ArgumentNullException (name);
328 return InternalGetType (null, name, throwOnError, ignoreCase);
331 [MethodImplAttribute (MethodImplOptions.InternalCall)]
332 internal extern static void InternalGetAssemblyName (string assemblyFile, AssemblyName aname);
334 [MethodImplAttribute (MethodImplOptions.InternalCall)]
335 static extern void FillName (Assembly ass, AssemblyName aname);
337 [MonoTODO ("true == not supported")]
338 public virtual AssemblyName GetName (Boolean copiedName)
340 // CodeBase, which is restricted, will be copied into the AssemblyName object so...
341 if (SecurityManager.SecurityEnabled) {
342 GetCodeBase (); // this will ensure the Demand is made
344 return UnprotectedGetName ();
347 public virtual AssemblyName GetName ()
349 return GetName (false);
352 // the security runtime requires access to the assemblyname (e.g. to get the strongname)
353 internal AssemblyName UnprotectedGetName ()
355 AssemblyName aname = new AssemblyName ();
356 FillName (this, aname);
360 public override string ToString ()
362 // note: ToString work without requiring CodeBase (so no checks are needed)
363 AssemblyName aname = new AssemblyName ();
364 FillName (this, aname);
365 return aname.ToString ();
368 public static String CreateQualifiedName (String assemblyName, String typeName)
370 return typeName + ", " + assemblyName;
373 public static Assembly GetAssembly (Type type)
376 return type.Assembly;
377 throw new ArgumentNullException ("type");
381 [MethodImplAttribute (MethodImplOptions.InternalCall)]
382 public static extern Assembly GetEntryAssembly();
384 public Assembly GetSatelliteAssembly (CultureInfo culture)
386 return GetSatelliteAssembly (culture, null);
389 public Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
392 throw new ArgumentException ("culture");
394 AssemblyName aname = GetName (true);
396 aname.Version = version;
398 aname.CultureInfo = culture;
399 aname.Name = aname.Name + ".resources";
403 [MethodImplAttribute (MethodImplOptions.InternalCall)]
404 private extern static Assembly LoadFrom (String assemblyFile, bool refonly);
406 public static Assembly LoadFrom (String assemblyFile)
408 return LoadFrom (assemblyFile, false);
411 public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence)
413 Assembly a = LoadFrom (assemblyFile, false);
414 if ((a != null) && (securityEvidence != null)) {
415 // merge evidence (i.e. replace defaults with provided evidences)
416 a.Evidence.Merge (securityEvidence);
424 public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
426 if (assemblyFile == null)
427 throw new ArgumentNullException ("assemblyFile");
428 if (assemblyFile == String.Empty)
429 throw new ArgumentException ("Name can't be the empty string", "assemblyFile");
430 throw new NotImplementedException ();
434 public static Assembly LoadFile (String path, Evidence securityEvidence) {
436 throw new ArgumentNullException ("path");
437 if (path == String.Empty)
438 throw new ArgumentException ("Path can't be empty", "path");
439 // FIXME: Make this do the right thing
440 return LoadFrom (path, securityEvidence);
443 public static Assembly LoadFile (String path) {
444 return LoadFile (path, null);
448 public static Assembly Load (String assemblyString)
450 return AppDomain.CurrentDomain.Load (assemblyString);
453 public static Assembly Load (String assemblyString, Evidence assemblySecurity)
455 return AppDomain.CurrentDomain.Load (assemblyString, assemblySecurity);
458 public static Assembly Load (AssemblyName assemblyRef)
460 return AppDomain.CurrentDomain.Load (assemblyRef);
463 public static Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
465 return AppDomain.CurrentDomain.Load (assemblyRef, assemblySecurity);
468 public static Assembly Load (Byte[] rawAssembly)
470 return AppDomain.CurrentDomain.Load (rawAssembly);
473 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore)
475 return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
478 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore,
479 Evidence securityEvidence)
481 return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore, securityEvidence);
485 public static Assembly ReflectionOnlyLoad (byte[] rawAssembly)
487 return AppDomain.CurrentDomain.Load (rawAssembly, null, null, true);
490 public static Assembly ReflectionOnlyLoad (string assemblyName)
492 return AppDomain.CurrentDomain.Load (assemblyName, null, true);
495 public static Assembly ReflectionOnlyLoadFrom (string assemblyFile)
497 if (assemblyFile == null)
498 throw new ArgumentNullException ("assemblyFile");
500 return LoadFrom (assemblyFile, true);
507 public static Assembly LoadWithPartialName (string partialName)
509 return LoadWithPartialName (partialName, null);
513 public Module LoadModule (string moduleName, byte [] rawModule)
515 throw new NotImplementedException ();
519 public Module LoadModule (string moduleName, byte [] rawModule, byte [] rawSymbolStore)
521 throw new NotImplementedException ();
524 [MethodImplAttribute (MethodImplOptions.InternalCall)]
525 private static extern Assembly load_with_partial_name (string name, Evidence e);
530 public static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence)
532 return LoadWithPartialName (partialName, securityEvidence, true);
536 * LAMESPEC: It is possible for this method to throw exceptions IF the name supplied
537 * is a valid gac name and contains filesystem entry charachters at the end of the name
538 * ie System/// will throw an exception. However ////System will not as that is canocolized
542 // FIXME: LoadWithPartialName must look cache (no CAS) or read from disk (CAS)
551 static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence, bool oldBehavior)
554 throw new NotImplementedException ();
556 if (partialName == null)
557 throw new NullReferenceException ();
559 return load_with_partial_name (partialName, securityEvidence);
562 public Object CreateInstance (String typeName)
564 return CreateInstance (typeName, false);
567 public Object CreateInstance (String typeName, Boolean ignoreCase)
569 Type t = GetType (typeName, false, ignoreCase);
574 return Activator.CreateInstance (t);
575 } catch (InvalidOperationException) {
576 throw new ArgumentException ("It is illegal to invoke a method on a Type loaded via ReflectionOnly methods.");
580 public Object CreateInstance (String typeName, Boolean ignoreCase,
581 BindingFlags bindingAttr, Binder binder,
582 Object[] args, CultureInfo culture,
583 Object[] activationAttributes)
585 Type t = GetType (typeName, false, ignoreCase);
590 return Activator.CreateInstance (t, bindingAttr, binder, args, culture, activationAttributes);
591 } catch (InvalidOperationException) {
592 throw new ArgumentException ("It is illegal to invoke a method on a Type loaded via ReflectionOnly methods.");
596 public Module[] GetLoadedModules ()
598 return GetLoadedModules (false);
602 public Module[] GetLoadedModules (bool getResourceModules)
604 // Currently, the two sets of modules are equal
605 return GetModules (getResourceModules);
608 public Module[] GetModules ()
610 return GetModules (false);
613 public Module GetModule (String name)
616 throw new ArgumentNullException ("name");
618 throw new ArgumentException ("Name can't be empty");
620 Module[] modules = GetModules (true);
621 foreach (Module module in modules) {
622 if (module.ScopeName == name)
629 [MethodImplAttribute (MethodImplOptions.InternalCall)]
630 internal extern Module[] GetModulesInternal ();
632 public Module[] GetModules (bool getResourceModules) {
633 Module[] modules = GetModulesInternal ();
635 if (!getResourceModules) {
636 ArrayList result = new ArrayList (modules.Length);
637 foreach (Module m in modules)
638 if (!m.IsResource ())
640 return (Module[])result.ToArray (typeof (Module));
646 [MethodImplAttribute (MethodImplOptions.InternalCall)]
647 internal extern string[] GetNamespaces ();
649 [MethodImplAttribute (MethodImplOptions.InternalCall)]
650 public extern virtual String[] GetManifestResourceNames ();
652 [MethodImplAttribute (MethodImplOptions.InternalCall)]
653 public extern static Assembly GetExecutingAssembly ();
655 [MethodImplAttribute (MethodImplOptions.InternalCall)]
656 public extern static Assembly GetCallingAssembly ();
658 [MethodImplAttribute (MethodImplOptions.InternalCall)]
659 public extern AssemblyName[] GetReferencedAssemblies ();
661 [MethodImplAttribute (MethodImplOptions.InternalCall)]
662 private extern bool GetManifestResourceInfoInternal (String name, ManifestResourceInfo info);
664 public virtual ManifestResourceInfo GetManifestResourceInfo (String resourceName)
666 if (resourceName == null)
667 throw new ArgumentNullException ("resourceName");
668 if (resourceName == "")
669 throw new ArgumentException ("String cannot have zero length.");
670 ManifestResourceInfo result = new ManifestResourceInfo ();
671 bool found = GetManifestResourceInfoInternal (resourceName, result);
678 private class ResourceCloseHandler {
682 public ResourceCloseHandler (Module module) {
683 this.module = module;
686 public void OnClose (object sender, EventArgs e) {
687 // The module dtor will take care of things
693 // The following functions are only for the Mono Debugger.
696 [MethodImplAttribute (MethodImplOptions.InternalCall)]
697 internal static extern MethodBase MonoDebugger_GetMethod (Assembly assembly, int token);
699 [MethodImplAttribute (MethodImplOptions.InternalCall)]
700 internal static extern int MonoDebugger_GetMethodToken (Assembly assembly, MethodBase method);
702 [MethodImplAttribute (MethodImplOptions.InternalCall)]
703 internal static extern Type MonoDebugger_GetLocalTypeFromSignature (Assembly assembly, byte[] signature);
705 [MethodImplAttribute (MethodImplOptions.InternalCall)]
706 internal static extern Type MonoDebugger_GetType (Assembly assembly, int token);
708 [MethodImplAttribute (MethodImplOptions.InternalCall)]
709 internal static extern string MonoDebugger_CheckRuntimeVersion (string filename);
711 [MethodImplAttribute (MethodImplOptions.InternalCall)]
712 internal static extern string MonoDebugger_GetMethodIndex (MethodBase method);
714 [MethodImplAttribute (MethodImplOptions.InternalCall)]
715 internal static extern Type MonoDebugger_MakeArrayType (Type type, int rank);
717 [MethodImplAttribute (MethodImplOptions.InternalCall)]
718 internal static extern int MonoDebugger_GetTypeToken (Type type);
723 public long HostContext {
728 public ImageFileMachine ImageFileMachine {
730 ImageFileMachine machine;
731 PortableExecutableKind kind;
732 ModuleHandle handle = ManifestModule.ModuleHandle;
733 handle.GetPEKind (out kind, out machine);
739 public extern Module ManifestModule {
740 [MethodImplAttribute (MethodImplOptions.InternalCall)]
745 public extern int MetadataToken {
746 [MethodImplAttribute (MethodImplOptions.InternalCall)]
751 public PortableExecutableKind PortableExecutableKind {
753 ImageFileMachine machine;
754 PortableExecutableKind kind;
755 ModuleHandle handle = ManifestModule.ModuleHandle;
756 handle.GetPEKind (out kind, out machine);
762 public virtual extern bool ReflectionOnly {
763 [MethodImplAttribute (MethodImplOptions.InternalCall)]
768 // Code Access Security
770 internal void Resolve ()
773 // FIXME: As we (currently) delay the resolution until the first CAS
774 // Demand it's too late to evaluate the Minimum permission set as a
775 // condition to load the assembly into the AppDomain
776 LoadAssemblyPermissions ();
777 _granted = SecurityManager.ResolvePolicy (UnprotectedGetEvidence (),
778 _minimum, _optional, _refuse, out _denied);
781 Console.WriteLine ("*** ASSEMBLY RESOLVE INPUT ***");
782 if (_minimum != null)
783 Console.WriteLine ("Minimum: {0}", _minimum);
784 if (_optional != null)
785 Console.WriteLine ("Optional: {0}", _optional);
787 Console.WriteLine ("Refuse: {0}", _refuse);
788 Console.WriteLine ("*** ASSEMBLY RESOLVE RESULTS ***");
789 Console.WriteLine ("Granted: {0}", _granted);
791 Console.WriteLine ("Denied: {0}", _denied);
792 Console.WriteLine ("*** ASSEMBLY RESOLVE END ***");
796 internal PermissionSet GrantedPermissionSet {
798 if (_granted == null) {
805 internal PermissionSet DeniedPermissionSet {
807 // yes we look for granted, as denied may be null
808 if (_granted == null) {
815 [MethodImplAttribute (MethodImplOptions.InternalCall)]
816 extern internal static bool LoadPermissions (Assembly a,
817 ref IntPtr minimum, ref int minLength,
818 ref IntPtr optional, ref int optLength,
819 ref IntPtr refused, ref int refLength);
821 // Support for SecurityAction.RequestMinimum, RequestOptional and RequestRefuse
822 private void LoadAssemblyPermissions ()
824 IntPtr minimum = IntPtr.Zero, optional = IntPtr.Zero, refused = IntPtr.Zero;
825 int minLength = 0, optLength = 0, refLength = 0;
826 if (LoadPermissions (this, ref minimum, ref minLength, ref optional,
827 ref optLength, ref refused, ref refLength)) {
829 // Note: no need to cache these permission sets as they will only be created once
830 // at assembly resolution time.
832 byte[] data = new byte [minLength];
833 Marshal.Copy (minimum, data, 0, minLength);
834 _minimum = SecurityManager.Decode (data);
837 byte[] data = new byte [optLength];
838 Marshal.Copy (optional, data, 0, optLength);
839 _optional = SecurityManager.Decode (data);
842 byte[] data = new byte [refLength];
843 Marshal.Copy (refused, data, 0, refLength);
844 _refuse = SecurityManager.Decode (data);