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 public virtual string CodeBase {
46 return get_code_base ();
50 internal virtual string CopiedCodeBase {
52 return get_code_base ();
57 public virtual string EscapedCodeBase {
59 //FIXME: escape characters -> Uri
60 return get_code_base ();
64 public virtual string FullName {
67 // FIXME: This is wrong, but it gets us going
68 // in the compiler for now
70 return GetName (false).ToString ();
74 public virtual extern MethodInfo EntryPoint {
75 [MethodImplAttribute (MethodImplOptions.InternalCall)]
79 public virtual Evidence Evidence {
85 public bool GlobalAssemblyCache {
87 //TODO: if we ever have a GAC, fix this.
92 public virtual String Location {
94 return get_location ();
99 public virtual string ImageRuntimeVersion {
101 return InternalImageRuntimeVersion ();
106 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
108 UnitySerializationHolder.GetAssemblyData (this, info, context);
111 public virtual bool IsDefined (Type attributeType, bool inherit)
113 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
116 public virtual object [] GetCustomAttributes (bool inherit)
118 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
121 public virtual object [] GetCustomAttributes (Type attributeType, bool inherit)
123 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
126 [MethodImplAttribute (MethodImplOptions.InternalCall)]
127 private extern object GetFilesInternal (String name);
129 public virtual FileStream[] GetFiles ()
131 string[] names = (string[]) GetFilesInternal (null);
133 return new FileStream [0];
135 FileStream[] res = new FileStream [names.Length];
136 for (int i = 0; i < names.Length; ++i)
137 res [i] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
142 public virtual FileStream [] GetFiles (bool getResourceModules)
144 throw new NotImplementedException ();
147 public virtual FileStream GetFile (String name)
150 throw new ArgumentNullException ("name");
151 string filename = (string)GetFilesInternal (name);
152 if (filename != null)
153 return new FileStream (filename, FileMode.Open, FileAccess.Read);
158 [MethodImplAttribute (MethodImplOptions.InternalCall)]
159 private extern IntPtr GetManifestResourceInternal (String name, out int size, out Module module);
161 public virtual Stream GetManifestResourceStream (String name)
164 throw new ArgumentNullException ("name");
166 throw new ArgumentException ("name cannot have zero length.");
168 ManifestResourceInfo info = GetManifestResourceInfo (name);
172 if (info.ReferencedAssembly != null)
173 return info.ReferencedAssembly.GetManifestResourceStream (name);
174 if ((info.FileName != null) && (info.ResourceLocation == 0)) {
175 string filename = Path.Combine (Path.GetDirectoryName (Location),
177 return new FileStream (filename, FileMode.Open, FileAccess.Read);
182 IntPtr data = GetManifestResourceInternal (name, out size, out module);
183 if (data == (IntPtr) 0)
186 IntPtrStream stream = new IntPtrStream (data, size);
188 * The returned pointer points inside metadata, so
189 * we have to increase the refcount of the module, and decrease
190 * it when the stream is finalized.
192 stream.Closed += new EventHandler (new ResourceCloseHandler (module).OnClose);
197 public virtual Stream GetManifestResourceStream (Type type, String name)
205 if ((ns == null) || (ns == ""))
206 return GetManifestResourceStream (name);
208 return GetManifestResourceStream (ns + "." + name);
211 [MethodImplAttribute (MethodImplOptions.InternalCall)]
212 private extern Type[] GetTypes (bool exportedOnly);
214 public virtual Type[] GetTypes ()
216 return GetTypes (false);
219 public virtual Type[] GetExportedTypes ()
221 return GetTypes (true);
224 public virtual Type GetType (String name, Boolean throwOnError)
226 return GetType (name, throwOnError, false);
229 public virtual Type GetType (String name) {
230 return GetType (name, false, false);
233 [MethodImplAttribute (MethodImplOptions.InternalCall)]
234 internal extern Type InternalGetType (Module module, String name, Boolean throwOnError, Boolean ignoreCase);
236 public Type GetType (string name, bool throwOnError, bool ignoreCase)
239 throw new ArgumentNullException (name);
241 return InternalGetType (null, name, throwOnError, ignoreCase);
244 [MethodImplAttribute (MethodImplOptions.InternalCall)]
245 internal extern static void InternalGetAssemblyName (string assemblyFile, AssemblyName aname);
247 [MethodImplAttribute (MethodImplOptions.InternalCall)]
248 static extern void FillName (Assembly ass, AssemblyName aname);
250 public virtual AssemblyName GetName (Boolean copiedName)
252 AssemblyName aname = new AssemblyName ();
253 FillName (this, aname);
257 public virtual AssemblyName GetName ()
259 return GetName (false);
262 public override String ToString ()
264 return GetName ().Name;
268 public static String CreateQualifiedName (String assemblyName, String typeName)
270 return typeName + "," + assemblyName;
273 public static Assembly GetAssembly (Type type)
276 return type.Assembly;
277 throw new ArgumentNullException ("type");
281 [MethodImplAttribute (MethodImplOptions.InternalCall)]
282 public static extern Assembly GetEntryAssembly();
284 public Assembly GetSatelliteAssembly (CultureInfo culture)
286 return GetSatelliteAssembly (culture, null);
289 public Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
292 throw new ArgumentException ("culture");
294 AssemblyName aname = GetName (true);
296 aname.Version = version;
298 aname.CultureInfo = culture;
299 aname.Name = aname.Name + ".resources";
303 [MethodImplAttribute (MethodImplOptions.InternalCall)]
304 public extern static Assembly LoadFrom (String assemblyFile);
307 public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence)
309 // Evidence is ignored
310 return LoadFrom (assemblyFile);
315 public static Assembly LoadFile (String path, Evidence securityEvidence) {
317 throw new ArgumentNullException ("path");
318 if (path == String.Empty)
319 throw new ArgumentException ("Path can't be empty", "path");
320 // FIXME: Make this do the right thing
321 return LoadFrom (path, securityEvidence);
324 public static Assembly LoadFile (String path) {
325 return LoadFile (path, null);
329 public static Assembly Load (String assemblyString)
331 return AppDomain.CurrentDomain.Load (assemblyString);
334 public static Assembly Load (String assemblyString, Evidence assemblySecurity)
336 return AppDomain.CurrentDomain.Load (assemblyString, assemblySecurity);
339 public static Assembly Load (AssemblyName assemblyRef)
341 return AppDomain.CurrentDomain.Load (assemblyRef);
344 public static Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
346 return AppDomain.CurrentDomain.Load (assemblyRef, assemblySecurity);
349 public static Assembly Load (Byte[] rawAssembly)
351 return AppDomain.CurrentDomain.Load (rawAssembly);
354 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore)
356 return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
359 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore,
360 Evidence securityEvidence)
362 return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore, securityEvidence);
365 public static Assembly LoadWithPartialName (string partialName)
367 return LoadWithPartialName (partialName, null);
371 public Module LoadModule (string moduleName, byte [] rawModule)
373 throw new NotImplementedException ();
377 public Module LoadModule (string moduleName, byte [] rawModule, byte [] rawSymbolStore)
379 throw new NotImplementedException ();
383 public static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence)
386 return AppDomain.CurrentDomain.Load (partialName, securityEvidence);
389 // According to MSDN, this should return null instead of
390 // throwing an exception
395 public Object CreateInstance (String typeName)
397 return CreateInstance (typeName, false);
400 public Object CreateInstance (String typeName, Boolean ignoreCase)
402 Type t = GetType (typeName, false, ignoreCase);
406 return Activator.CreateInstance (t);
409 public Object CreateInstance (String typeName, Boolean ignoreCase,
410 BindingFlags bindingAttr, Binder binder,
411 Object[] args, CultureInfo culture,
412 Object[] activationAttributes)
414 Type t = GetType (typeName, false, ignoreCase);
418 return Activator.CreateInstance (t, bindingAttr, binder, args, culture, activationAttributes);
421 public Module[] GetLoadedModules ()
423 return GetLoadedModules (false);
427 public Module[] GetLoadedModules (bool getResourceModules)
429 // Currently, the two sets of modules are equal
430 return GetModules (getResourceModules);
433 public Module[] GetModules ()
435 return GetModules (false);
438 public Module GetModule (String name)
441 throw new ArgumentNullException ("name");
443 throw new ArgumentException ("Name can't be empty");
445 Module[] modules = GetModules (true);
446 foreach (Module module in GetModules (true)) {
447 if (module.ScopeName == name)
454 [MethodImplAttribute (MethodImplOptions.InternalCall)]
455 internal extern Module[] GetModulesInternal ();
457 public Module[] GetModules (bool getResourceModules) {
458 Module[] modules = GetModulesInternal ();
460 if (!getResourceModules) {
461 ArrayList result = new ArrayList (modules.Length);
462 foreach (Module m in modules)
463 if (!m.IsResource ())
465 return (Module[])result.ToArray (typeof (Module));
471 [MethodImplAttribute (MethodImplOptions.InternalCall)]
472 internal extern string[] GetNamespaces ();
474 [MethodImplAttribute (MethodImplOptions.InternalCall)]
475 public extern virtual String[] GetManifestResourceNames ();
477 [MethodImplAttribute (MethodImplOptions.InternalCall)]
478 public extern static Assembly GetExecutingAssembly ();
480 [MethodImplAttribute (MethodImplOptions.InternalCall)]
481 public extern static Assembly GetCallingAssembly ();
483 [MethodImplAttribute (MethodImplOptions.InternalCall)]
484 public extern AssemblyName[] GetReferencedAssemblies ();
486 [MethodImplAttribute (MethodImplOptions.InternalCall)]
487 private extern bool GetManifestResourceInfoInternal (String name, ManifestResourceInfo info);
489 public virtual ManifestResourceInfo GetManifestResourceInfo (String resourceName)
491 if (resourceName == null)
492 throw new ArgumentNullException ("resourceName");
493 if (resourceName == "")
494 throw new ArgumentException ("String cannot have zero length.");
495 ManifestResourceInfo result = new ManifestResourceInfo ();
496 bool found = GetManifestResourceInfoInternal (resourceName, result);
503 private class ResourceCloseHandler {
507 public ResourceCloseHandler (Module module) {
508 this.module = module;
511 public void OnClose (object sender, EventArgs e) {
512 // The module dtor will take care of things
518 // The following functions are only for the Mono Debugger.
521 [MethodImplAttribute (MethodImplOptions.InternalCall)]
522 internal static extern MethodBase MonoDebugger_GetMethod (Assembly assembly, int token);
524 [MethodImplAttribute (MethodImplOptions.InternalCall)]
525 internal static extern int MonoDebugger_GetMethodToken (Assembly assembly, MethodBase method);
527 [MethodImplAttribute (MethodImplOptions.InternalCall)]
528 internal static extern Type MonoDebugger_GetLocalTypeFromSignature (Assembly assembly, byte[] signature);
530 [MethodImplAttribute (MethodImplOptions.InternalCall)]
531 internal static extern Type MonoDebugger_GetType (Assembly assembly, int token);