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);
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);
181 IntPtr data = GetManifestResourceInternal (name, out size);
182 if (data == (IntPtr) 0)
185 return new IntPtrStream (data, size);
188 public virtual Stream GetManifestResourceStream (Type type, String name)
196 if ((ns == null) || (ns == ""))
197 return GetManifestResourceStream (name);
199 return GetManifestResourceStream (ns + "." + name);
202 [MethodImplAttribute (MethodImplOptions.InternalCall)]
203 private extern Type[] GetTypes (bool exportedOnly);
205 public virtual Type[] GetTypes ()
207 return GetTypes (false);
210 public virtual Type[] GetExportedTypes ()
212 return GetTypes (true);
215 public virtual Type GetType (String name, Boolean throwOnError)
217 return GetType (name, throwOnError, false);
220 public virtual Type GetType (String name) {
221 return GetType (name, false, false);
224 [MethodImplAttribute (MethodImplOptions.InternalCall)]
225 internal extern Type InternalGetType (Module module, String name, Boolean throwOnError, Boolean ignoreCase);
227 public Type GetType (string name, bool throwOnError, bool ignoreCase)
230 throw new ArgumentNullException (name);
232 return InternalGetType (null, name, throwOnError, ignoreCase);
235 [MethodImplAttribute (MethodImplOptions.InternalCall)]
236 internal extern static void InternalGetAssemblyName (string assemblyFile, AssemblyName aname);
238 [MethodImplAttribute (MethodImplOptions.InternalCall)]
239 static extern void FillName (Assembly ass, AssemblyName aname);
241 public virtual AssemblyName GetName (Boolean copiedName)
243 AssemblyName aname = new AssemblyName ();
244 FillName (this, aname);
248 public virtual AssemblyName GetName ()
250 return GetName (false);
253 public override String ToString ()
255 return GetName ().Name;
259 public static String CreateQualifiedName (String assemblyName, String typeName)
261 return typeName + "," + assemblyName;
264 public static Assembly GetAssembly (Type type)
267 return type.Assembly;
268 throw new ArgumentNullException ("type");
272 [MethodImplAttribute (MethodImplOptions.InternalCall)]
273 public static extern Assembly GetEntryAssembly();
275 public Assembly GetSatelliteAssembly (CultureInfo culture)
277 return GetSatelliteAssembly (culture, null);
280 public Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
283 throw new ArgumentException ("culture");
285 AssemblyName aname = GetName (true);
287 aname.Version = version;
289 aname.CultureInfo = culture;
290 aname.Name = aname.Name + ".resources";
294 [MethodImplAttribute (MethodImplOptions.InternalCall)]
295 public extern static Assembly LoadFrom (String assemblyFile);
298 public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence)
300 throw new NotImplementedException ();
305 public static Assembly LoadFile (String path, Evidence securityEvidence) {
307 throw new ArgumentNullException ("path");
308 if (path == String.Empty)
309 throw new ArgumentException ("Path can't be empty", "path");
310 // FIXME: Make this do the right thing
311 return LoadFrom (path, securityEvidence);
314 public static Assembly LoadFile (String path) {
315 return LoadFile (path, null);
319 public static Assembly Load (String assemblyString)
321 return AppDomain.CurrentDomain.Load (assemblyString);
324 public static Assembly Load (String assemblyString, Evidence assemblySecurity)
326 return AppDomain.CurrentDomain.Load (assemblyString, assemblySecurity);
329 public static Assembly Load (AssemblyName assemblyRef)
331 return AppDomain.CurrentDomain.Load (assemblyRef);
334 public static Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
336 return AppDomain.CurrentDomain.Load (assemblyRef, assemblySecurity);
339 public static Assembly Load (Byte[] rawAssembly)
341 return AppDomain.CurrentDomain.Load (rawAssembly);
344 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore)
346 return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
349 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore,
350 Evidence securityEvidence)
352 return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore, securityEvidence);
355 public static Assembly LoadWithPartialName (string partialName)
357 return LoadWithPartialName (partialName, null);
361 public Module LoadModule (string moduleName, byte [] rawModule)
363 throw new NotImplementedException ();
367 public Module LoadModule (string moduleName, byte [] rawModule, byte [] rawSymbolStore)
369 throw new NotImplementedException ();
373 public static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence)
376 return AppDomain.CurrentDomain.Load (partialName, securityEvidence);
378 catch (Exception ex) {
379 // According to MSDN, this should return null instead of
380 // throwing an exception
385 public Object CreateInstance (String typeName)
387 return CreateInstance (typeName, false);
390 public Object CreateInstance (String typeName, Boolean ignoreCase)
392 Type t = GetType (typeName, false, ignoreCase);
396 return Activator.CreateInstance (t);
399 public Object CreateInstance (String typeName, Boolean ignoreCase,
400 BindingFlags bindingAttr, Binder binder,
401 Object[] args, CultureInfo culture,
402 Object[] activationAttributes)
404 Type t = GetType (typeName, false, ignoreCase);
408 return Activator.CreateInstance (t, bindingAttr, binder, args, culture, activationAttributes);
411 public Module[] GetLoadedModules ()
413 return GetLoadedModules (false);
417 public Module[] GetLoadedModules (bool getResourceModules)
419 // Currently, the two sets of modules are equal
420 return GetModules (getResourceModules);
423 public Module[] GetModules ()
425 return GetModules (false);
428 public Module GetModule (String name)
431 throw new ArgumentNullException ("name");
433 throw new ArgumentException ("Name can't be empty");
435 Module[] modules = GetModules (true);
436 foreach (Module module in GetModules (true)) {
437 if (module.ScopeName == name)
444 [MethodImplAttribute (MethodImplOptions.InternalCall)]
445 internal extern Module[] GetModulesInternal ();
447 public Module[] GetModules (bool getResourceModules) {
448 Module[] modules = GetModulesInternal ();
450 if (!getResourceModules) {
451 ArrayList result = new ArrayList (modules.Length);
452 foreach (Module m in modules)
453 if (!m.IsResource ())
455 return (Module[])result.ToArray (typeof (Module));
461 [MethodImplAttribute (MethodImplOptions.InternalCall)]
462 internal extern string[] GetNamespaces ();
464 [MethodImplAttribute (MethodImplOptions.InternalCall)]
465 public extern virtual String[] GetManifestResourceNames ();
467 [MethodImplAttribute (MethodImplOptions.InternalCall)]
468 public extern static Assembly GetExecutingAssembly ();
470 [MethodImplAttribute (MethodImplOptions.InternalCall)]
471 public extern static Assembly GetCallingAssembly ();
473 [MethodImplAttribute (MethodImplOptions.InternalCall)]
474 public extern AssemblyName[] GetReferencedAssemblies ();
476 [MethodImplAttribute (MethodImplOptions.InternalCall)]
477 private extern bool GetManifestResourceInfoInternal (String name, ManifestResourceInfo info);
479 public virtual ManifestResourceInfo GetManifestResourceInfo (String resourceName)
481 if (resourceName == null)
482 throw new ArgumentNullException ("resourceName");
483 if (resourceName == "")
484 throw new ArgumentException ("String cannot have zero length.");
485 ManifestResourceInfo result = new ManifestResourceInfo ();
486 bool found = GetManifestResourceInfoInternal (resourceName, result);
494 // The following functions are only for the Mono Debugger.
496 // They should be marked `internal', and extracted with GetMethod from the debugger.
499 [MethodImplAttribute (MethodImplOptions.InternalCall)]
500 public extern MethodBase MonoDebugger_GetMethod (int token);
502 [MethodImplAttribute (MethodImplOptions.InternalCall)]
503 public extern int MonoDebugger_GetMethodToken (MethodBase method);
505 [MethodImplAttribute (MethodImplOptions.InternalCall)]
506 public extern Type MonoDebugger_GetLocalTypeFromSignature (byte[] signature);
508 [MethodImplAttribute (MethodImplOptions.InternalCall)]
509 public extern Type MonoDebugger_GetType (int token);