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 public class Assembly : System.Reflection.ICustomAttributeProvider,
24 System.Security.IEvidenceFactory, System.Runtime.Serialization.ISerializable {
25 private IntPtr _mono_assembly;
27 internal Assembly () {}
29 //TODO: when adding this, MonoReflectionAssembly must be modified too.
30 // Probably, adding a delegate field after _mono_assbmely and using it in add/remove
31 // is the way to go (to avoid the compiler inserting the delegate field before).
32 //public event ModuleResolveEventHandler ModuleResolve;
34 [MethodImplAttribute (MethodImplOptions.InternalCall)]
35 private extern string get_code_base ();
37 [MethodImplAttribute (MethodImplOptions.InternalCall)]
38 private extern string get_location ();
40 [MethodImplAttribute (MethodImplOptions.InternalCall)]
41 private extern string InternalImageRuntimeVersion ();
43 public virtual string CodeBase {
45 return get_code_base ();
49 internal virtual string CopiedCodeBase {
51 return get_code_base ();
56 public virtual string EscapedCodeBase {
58 //FIXME: escape characters -> Uri
59 return get_code_base ();
63 public virtual string FullName {
66 // FIXME: This is wrong, but it gets us going
67 // in the compiler for now
69 return GetName (false).ToString ();
73 public virtual extern MethodInfo EntryPoint {
74 [MethodImplAttribute (MethodImplOptions.InternalCall)]
78 public virtual Evidence Evidence {
84 public bool GlobalAssemblyCache {
86 //TODO: if we ever have a GAC, fix this.
91 public virtual String Location {
93 return get_location ();
98 public virtual string ImageRuntimeVersion {
100 return InternalImageRuntimeVersion ();
105 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
107 UnitySerializationHolder.GetAssemblyData (this, info, context);
110 public virtual bool IsDefined (Type attributeType, bool inherit)
112 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
115 public virtual object [] GetCustomAttributes (bool inherit)
117 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
120 public virtual object [] GetCustomAttributes (Type attributeType, bool inherit)
122 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
125 [MethodImplAttribute (MethodImplOptions.InternalCall)]
126 private extern object GetFilesInternal (String name);
128 public virtual FileStream[] GetFiles ()
130 string[] names = (string[]) GetFilesInternal (null);
132 return new FileStream [0];
134 FileStream[] res = new FileStream [names.Length];
135 for (int i = 0; i < names.Length; ++i)
136 res [i] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
141 public virtual FileStream [] GetFiles (bool getResourceModules)
143 throw new NotImplementedException ();
146 public virtual FileStream GetFile (String name)
149 throw new ArgumentNullException ("name");
150 string filename = (string)GetFilesInternal (name);
151 if (filename != null)
152 return new FileStream (filename, FileMode.Open, FileAccess.Read);
157 [MethodImplAttribute (MethodImplOptions.InternalCall)]
158 private extern IntPtr GetManifestResourceInternal (String name, out int size);
160 public virtual Stream GetManifestResourceStream (String name)
163 throw new ArgumentNullException ("name");
165 throw new ArgumentException ("name cannot have zero length.");
167 ManifestResourceInfo info = GetManifestResourceInfo (name);
171 if (info.ReferencedAssembly != null)
172 return info.ReferencedAssembly.GetManifestResourceStream (name);
173 if (info.FileName != null) {
174 string filename = Path.Combine (Path.GetDirectoryName (Location),
176 return new FileStream (filename, FileMode.Open, FileAccess.Read);
180 IntPtr data = GetManifestResourceInternal (name, out size);
181 if (data == (IntPtr) 0)
184 return new IntPtrStream (data, size);
187 public virtual Stream GetManifestResourceStream (Type type, String name)
195 if ((ns == null) || (ns == ""))
196 return GetManifestResourceStream (name);
198 return GetManifestResourceStream (ns + "." + name);
201 [MethodImplAttribute (MethodImplOptions.InternalCall)]
202 private extern Type[] GetTypes (bool exportedOnly);
204 public virtual Type[] GetTypes ()
206 return GetTypes (false);
209 public virtual Type[] GetExportedTypes ()
211 return GetTypes (true);
214 public virtual Type GetType (String name, Boolean throwOnError)
216 return GetType (name, throwOnError, false);
219 public virtual Type GetType (String name) {
220 return GetType (name, false, false);
223 [MethodImplAttribute (MethodImplOptions.InternalCall)]
224 extern Type InternalGetType (String name, Boolean throwOnError, Boolean ignoreCase);
226 public Type GetType (string name, bool throwOnError, bool ignoreCase)
229 throw new ArgumentNullException (name);
231 return InternalGetType (name, throwOnError, ignoreCase);
234 [MethodImplAttribute (MethodImplOptions.InternalCall)]
235 static extern void FillName (Assembly ass, AssemblyName aname);
237 public virtual AssemblyName GetName (Boolean copiedName)
239 AssemblyName aname = new AssemblyName ();
240 FillName (this, aname);
244 public virtual AssemblyName GetName ()
246 return GetName (false);
249 public override String ToString ()
251 return GetName ().Name;
255 public static String CreateQualifiedName (String assemblyName, String typeName)
257 return typeName + "," + assemblyName;
260 public static Assembly GetAssembly (Type type)
263 return type.Assembly;
264 throw new ArgumentNullException ("type");
268 [MethodImplAttribute (MethodImplOptions.InternalCall)]
269 public static extern Assembly GetEntryAssembly();
271 public Assembly GetSatelliteAssembly (CultureInfo culture)
273 return GetSatelliteAssembly (culture, null);
276 public Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
279 throw new ArgumentException ("culture");
281 AssemblyName aname = GetName (true);
283 aname.Version = version;
285 aname.CultureInfo = culture;
286 aname.Name = aname.Name + ".resources";
290 [MethodImplAttribute (MethodImplOptions.InternalCall)]
291 public extern static Assembly LoadFrom (String assemblyFile);
294 public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence)
296 throw new NotImplementedException ();
299 public static Assembly Load (String assemblyString)
301 return AppDomain.CurrentDomain.Load (assemblyString);
304 public static Assembly Load (String assemblyString, Evidence assemblySecurity)
306 return AppDomain.CurrentDomain.Load (assemblyString, assemblySecurity);
309 public static Assembly Load (AssemblyName assemblyRef)
311 return AppDomain.CurrentDomain.Load (assemblyRef);
314 public static Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
316 return AppDomain.CurrentDomain.Load (assemblyRef, assemblySecurity);
319 public static Assembly Load (Byte[] rawAssembly)
321 return AppDomain.CurrentDomain.Load (rawAssembly);
324 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore)
326 return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
329 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore,
330 Evidence securityEvidence)
332 return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore, securityEvidence);
335 public static Assembly LoadWithPartialName (string partialName)
337 return LoadWithPartialName (partialName, null);
341 public Module LoadModule (string moduleName, byte [] rawModule)
343 throw new NotImplementedException ();
347 public Module LoadModule (string moduleName, byte [] rawModule, byte [] rawSymbolStore)
349 throw new NotImplementedException ();
353 public static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence)
355 return AppDomain.CurrentDomain.Load (partialName, securityEvidence);
359 public Object CreateInstance (String typeName)
361 return CreateInstance (typeName, false);
364 public Object CreateInstance (String typeName, Boolean ignoreCase)
366 Type t = GetType (typeName, false, ignoreCase);
370 return Activator.CreateInstance (t);
373 public Object CreateInstance (String typeName, Boolean ignoreCase,
374 BindingFlags bindingAttr, Binder binder,
375 Object[] args, CultureInfo culture,
376 Object[] activationAttributes)
378 Type t = GetType (typeName, false, ignoreCase);
382 return Activator.CreateInstance (t, bindingAttr, binder, args, culture, activationAttributes);
385 public Module[] GetLoadedModules ()
387 return GetLoadedModules (false);
391 public Module[] GetLoadedModules (bool getResourceModules)
393 // Currently, the two sets of modules are equal
394 return GetModules (getResourceModules);
397 public Module[] GetModules ()
399 return GetModules (false);
402 public Module GetModule (String name)
405 throw new ArgumentNullException ("name");
407 throw new ArgumentException ("Name can't be empty");
409 Module[] modules = GetModules (true);
410 foreach (Module module in GetModules (true)) {
411 if (module.ScopeName == name)
418 [MethodImplAttribute (MethodImplOptions.InternalCall)]
419 public extern Module[] GetModulesInternal ();
421 public Module[] GetModules (bool getResourceModules) {
422 Module[] modules = GetModulesInternal ();
424 if (!getResourceModules) {
425 ArrayList result = new ArrayList (modules.Length);
426 foreach (Module m in modules)
427 if (!m.IsResource ())
429 return (Module[])result.ToArray (typeof (Module));
435 [MethodImplAttribute (MethodImplOptions.InternalCall)]
436 public extern string[] GetNamespaces ();
438 [MethodImplAttribute (MethodImplOptions.InternalCall)]
439 public extern virtual String[] GetManifestResourceNames ();
441 [MethodImplAttribute (MethodImplOptions.InternalCall)]
442 public extern static Assembly GetExecutingAssembly ();
444 [MethodImplAttribute (MethodImplOptions.InternalCall)]
445 public extern static Assembly GetCallingAssembly ();
447 [MethodImplAttribute (MethodImplOptions.InternalCall)]
448 public extern AssemblyName[] GetReferencedAssemblies ();
450 [MethodImplAttribute (MethodImplOptions.InternalCall)]
451 private extern bool GetManifestResourceInfoInternal (String name, ManifestResourceInfo info);
453 public virtual ManifestResourceInfo GetManifestResourceInfo (String resourceName)
455 if (resourceName == null)
456 throw new ArgumentNullException ("resourceName");
457 if (resourceName == "")
458 throw new ArgumentException ("String cannot have zero length.");
459 ManifestResourceInfo result = new ManifestResourceInfo ();
460 bool found = GetManifestResourceInfoInternal (resourceName, result);
468 // The following functions are only for the Mono Debugger.
471 [MethodImplAttribute (MethodImplOptions.InternalCall)]
472 public extern MethodBase MonoDebugger_GetMethod (int token);
474 [MethodImplAttribute (MethodImplOptions.InternalCall)]
475 public extern int MonoDebugger_GetMethodToken (MethodBase method);
477 [MethodImplAttribute (MethodImplOptions.InternalCall)]
478 public extern Type MonoDebugger_GetLocalTypeFromSignature (byte[] signature);
480 [MethodImplAttribute (MethodImplOptions.InternalCall)]
481 public extern Type MonoDebugger_GetType (int token);