2 // System.Reflection/Assembly.cs
5 // Paolo Molaro (lupus@ximian.com)
7 // (C) 2001 Ximian, Inc. http://www.ximian.com
11 using System.Security.Policy;
12 using System.Runtime.Serialization;
13 using System.Reflection.Emit;
15 using System.Globalization;
16 using System.Runtime.CompilerServices;
17 using System.Runtime.InteropServices;
18 using System.Collections;
20 namespace System.Reflection {
23 [ClassInterface(ClassInterfaceType.AutoDual)]
24 public class Assembly : System.Reflection.ICustomAttributeProvider,
25 System.Security.IEvidenceFactory, System.Runtime.Serialization.ISerializable {
26 private IntPtr _mono_assembly;
28 internal Assembly () {}
30 //TODO: when adding this, MonoReflectionAssembly must be modified too.
31 // Probably, adding a delegate field after _mono_assbmely and using it in add/remove
32 // is the way to go (to avoid the compiler inserting the delegate field before).
33 //public event ModuleResolveEventHandler ModuleResolve;
35 [MethodImplAttribute (MethodImplOptions.InternalCall)]
36 private extern string get_code_base ();
38 [MethodImplAttribute (MethodImplOptions.InternalCall)]
39 private extern string get_location ();
41 [MethodImplAttribute (MethodImplOptions.InternalCall)]
42 private extern string InternalImageRuntimeVersion ();
44 [MethodImplAttribute (MethodImplOptions.InternalCall)]
45 private extern bool get_global_assembly_cache ();
47 public virtual string CodeBase {
49 return get_code_base ();
53 internal virtual string CopiedCodeBase {
55 return get_code_base ();
60 public virtual string EscapedCodeBase {
62 //FIXME: escape characters -> Uri
63 return get_code_base ();
67 public virtual string FullName {
70 // FIXME: This is wrong, but it gets us going
71 // in the compiler for now
73 return GetName (false).ToString ();
77 public virtual extern MethodInfo EntryPoint {
78 [MethodImplAttribute (MethodImplOptions.InternalCall)]
82 public virtual Evidence Evidence {
89 public bool GlobalAssemblyCache {
91 return get_global_assembly_cache ();
95 public virtual String Location {
97 return get_location ();
102 public virtual string ImageRuntimeVersion {
104 return InternalImageRuntimeVersion ();
109 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
111 UnitySerializationHolder.GetAssemblyData (this, info, context);
114 public virtual bool IsDefined (Type attributeType, bool inherit)
116 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
119 public virtual object [] GetCustomAttributes (bool inherit)
121 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
124 public virtual object [] GetCustomAttributes (Type attributeType, bool inherit)
126 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
129 [MethodImplAttribute (MethodImplOptions.InternalCall)]
130 private extern object GetFilesInternal (String name);
132 public virtual FileStream[] GetFiles ()
134 string[] names = (string[]) GetFilesInternal (null);
136 return new FileStream [0];
138 FileStream[] res = new FileStream [names.Length];
139 for (int i = 0; i < names.Length; ++i)
140 res [i] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
145 public virtual FileStream [] GetFiles (bool getResourceModules)
147 throw new NotImplementedException ();
150 public virtual FileStream GetFile (String name)
153 throw new ArgumentNullException ("name");
154 string filename = (string)GetFilesInternal (name);
155 if (filename != null)
156 return new FileStream (filename, FileMode.Open, FileAccess.Read);
161 [MethodImplAttribute (MethodImplOptions.InternalCall)]
162 private extern IntPtr GetManifestResourceInternal (String name, out int size, out Module module);
164 public virtual Stream GetManifestResourceStream (String name)
167 throw new ArgumentNullException ("name");
169 throw new ArgumentException ("name cannot have zero length.");
171 ManifestResourceInfo info = GetManifestResourceInfo (name);
175 if (info.ReferencedAssembly != null)
176 return info.ReferencedAssembly.GetManifestResourceStream (name);
177 if ((info.FileName != null) && (info.ResourceLocation == 0)) {
178 string filename = Path.Combine (Path.GetDirectoryName (Location),
180 return new FileStream (filename, FileMode.Open, FileAccess.Read);
185 IntPtr data = GetManifestResourceInternal (name, out size, out module);
186 if (data == (IntPtr) 0)
189 IntPtrStream stream = new IntPtrStream (data, size);
191 * The returned pointer points inside metadata, so
192 * we have to increase the refcount of the module, and decrease
193 * it when the stream is finalized.
195 stream.Closed += new EventHandler (new ResourceCloseHandler (module).OnClose);
200 public virtual Stream GetManifestResourceStream (Type type, String name)
208 if ((ns == null) || (ns == ""))
209 return GetManifestResourceStream (name);
211 return GetManifestResourceStream (ns + "." + name);
214 [MethodImplAttribute (MethodImplOptions.InternalCall)]
215 private extern Type[] GetTypes (bool exportedOnly);
217 public virtual Type[] GetTypes ()
219 return GetTypes (false);
222 public virtual Type[] GetExportedTypes ()
224 return GetTypes (true);
227 public virtual Type GetType (String name, Boolean throwOnError)
229 return GetType (name, throwOnError, false);
232 public virtual Type GetType (String name) {
233 return GetType (name, false, false);
236 [MethodImplAttribute (MethodImplOptions.InternalCall)]
237 internal extern Type InternalGetType (Module module, String name, Boolean throwOnError, Boolean ignoreCase);
239 public Type GetType (string name, bool throwOnError, bool ignoreCase)
242 throw new ArgumentNullException (name);
244 return InternalGetType (null, name, throwOnError, ignoreCase);
247 [MethodImplAttribute (MethodImplOptions.InternalCall)]
248 internal extern static void InternalGetAssemblyName (string assemblyFile, AssemblyName aname);
250 [MethodImplAttribute (MethodImplOptions.InternalCall)]
251 static extern void FillName (Assembly ass, AssemblyName aname);
253 public virtual AssemblyName GetName (Boolean copiedName)
255 AssemblyName aname = new AssemblyName ();
256 FillName (this, aname);
260 public virtual AssemblyName GetName ()
262 return GetName (false);
265 public override String ToString ()
267 return GetName ().Name;
270 public static String CreateQualifiedName (String assemblyName, String typeName)
272 return typeName + ", " + assemblyName;
275 public static Assembly GetAssembly (Type type)
278 return type.Assembly;
279 throw new ArgumentNullException ("type");
283 [MethodImplAttribute (MethodImplOptions.InternalCall)]
284 public static extern Assembly GetEntryAssembly();
286 public Assembly GetSatelliteAssembly (CultureInfo culture)
288 return GetSatelliteAssembly (culture, null);
291 public Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
294 throw new ArgumentException ("culture");
296 AssemblyName aname = GetName (true);
298 aname.Version = version;
300 aname.CultureInfo = culture;
301 aname.Name = aname.Name + ".resources";
305 [MethodImplAttribute (MethodImplOptions.InternalCall)]
306 public extern static Assembly LoadFrom (String assemblyFile);
309 public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence)
311 // Evidence is ignored
312 return LoadFrom (assemblyFile);
317 public static Assembly LoadFile (String path, Evidence securityEvidence) {
319 throw new ArgumentNullException ("path");
320 if (path == String.Empty)
321 throw new ArgumentException ("Path can't be empty", "path");
322 // FIXME: Make this do the right thing
323 return LoadFrom (path, securityEvidence);
326 public static Assembly LoadFile (String path) {
327 return LoadFile (path, null);
331 public static Assembly Load (String assemblyString)
333 return AppDomain.CurrentDomain.Load (assemblyString);
336 public static Assembly Load (String assemblyString, Evidence assemblySecurity)
338 return AppDomain.CurrentDomain.Load (assemblyString, assemblySecurity);
341 public static Assembly Load (AssemblyName assemblyRef)
343 return AppDomain.CurrentDomain.Load (assemblyRef);
346 public static Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
348 return AppDomain.CurrentDomain.Load (assemblyRef, assemblySecurity);
351 public static Assembly Load (Byte[] rawAssembly)
353 return AppDomain.CurrentDomain.Load (rawAssembly);
356 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore)
358 return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
361 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore,
362 Evidence securityEvidence)
364 return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore, securityEvidence);
367 public static Assembly LoadWithPartialName (string partialName)
369 return LoadWithPartialName (partialName, null);
373 public Module LoadModule (string moduleName, byte [] rawModule)
375 throw new NotImplementedException ();
379 public Module LoadModule (string moduleName, byte [] rawModule, byte [] rawSymbolStore)
381 throw new NotImplementedException ();
385 public static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence)
388 return AppDomain.CurrentDomain.Load (partialName, securityEvidence);
391 // According to MSDN, this should return null instead of
392 // throwing an exception
397 public Object CreateInstance (String typeName)
399 return CreateInstance (typeName, false);
402 public Object CreateInstance (String typeName, Boolean ignoreCase)
404 Type t = GetType (typeName, false, ignoreCase);
408 return Activator.CreateInstance (t);
411 public Object CreateInstance (String typeName, Boolean ignoreCase,
412 BindingFlags bindingAttr, Binder binder,
413 Object[] args, CultureInfo culture,
414 Object[] activationAttributes)
416 Type t = GetType (typeName, false, ignoreCase);
420 return Activator.CreateInstance (t, bindingAttr, binder, args, culture, activationAttributes);
423 public Module[] GetLoadedModules ()
425 return GetLoadedModules (false);
429 public Module[] GetLoadedModules (bool getResourceModules)
431 // Currently, the two sets of modules are equal
432 return GetModules (getResourceModules);
435 public Module[] GetModules ()
437 return GetModules (false);
440 public Module GetModule (String name)
443 throw new ArgumentNullException ("name");
445 throw new ArgumentException ("Name can't be empty");
447 Module[] modules = GetModules (true);
448 foreach (Module module in GetModules (true)) {
449 if (module.ScopeName == name)
456 [MethodImplAttribute (MethodImplOptions.InternalCall)]
457 internal extern Module[] GetModulesInternal ();
459 public Module[] GetModules (bool getResourceModules) {
460 Module[] modules = GetModulesInternal ();
462 if (!getResourceModules) {
463 ArrayList result = new ArrayList (modules.Length);
464 foreach (Module m in modules)
465 if (!m.IsResource ())
467 return (Module[])result.ToArray (typeof (Module));
473 [MethodImplAttribute (MethodImplOptions.InternalCall)]
474 internal extern string[] GetNamespaces ();
476 [MethodImplAttribute (MethodImplOptions.InternalCall)]
477 public extern virtual String[] GetManifestResourceNames ();
479 [MethodImplAttribute (MethodImplOptions.InternalCall)]
480 public extern static Assembly GetExecutingAssembly ();
482 [MethodImplAttribute (MethodImplOptions.InternalCall)]
483 public extern static Assembly GetCallingAssembly ();
485 [MethodImplAttribute (MethodImplOptions.InternalCall)]
486 public extern AssemblyName[] GetReferencedAssemblies ();
488 [MethodImplAttribute (MethodImplOptions.InternalCall)]
489 private extern bool GetManifestResourceInfoInternal (String name, ManifestResourceInfo info);
491 public virtual ManifestResourceInfo GetManifestResourceInfo (String resourceName)
493 if (resourceName == null)
494 throw new ArgumentNullException ("resourceName");
495 if (resourceName == "")
496 throw new ArgumentException ("String cannot have zero length.");
497 ManifestResourceInfo result = new ManifestResourceInfo ();
498 bool found = GetManifestResourceInfoInternal (resourceName, result);
505 private class ResourceCloseHandler {
509 public ResourceCloseHandler (Module module) {
510 this.module = module;
513 public void OnClose (object sender, EventArgs e) {
514 // The module dtor will take care of things
520 // The following functions are only for the Mono Debugger.
523 [MethodImplAttribute (MethodImplOptions.InternalCall)]
524 internal static extern MethodBase MonoDebugger_GetMethod (Assembly assembly, int token);
526 [MethodImplAttribute (MethodImplOptions.InternalCall)]
527 internal static extern int MonoDebugger_GetMethodToken (Assembly assembly, MethodBase method);
529 [MethodImplAttribute (MethodImplOptions.InternalCall)]
530 internal static extern Type MonoDebugger_GetLocalTypeFromSignature (Assembly assembly, byte[] signature);
532 [MethodImplAttribute (MethodImplOptions.InternalCall)]
533 internal static extern Type MonoDebugger_GetType (Assembly assembly, int token);