2004-02-12 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / class / corlib / System.Reflection / Assembly.cs
1 //
2 // System.Reflection/Assembly.cs
3 //
4 // Author:
5 //   Paolo Molaro (lupus@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.  http://www.ximian.com
8 //
9
10 using System;
11 using System.Security.Policy;
12 using System.Runtime.Serialization;
13 using System.Reflection.Emit;
14 using System.IO;
15 using System.Globalization;
16 using System.Runtime.CompilerServices;
17 using System.Runtime.InteropServices;
18 using System.Collections;
19
20 namespace System.Reflection {
21
22         [Serializable]
23         [ClassInterface(ClassInterfaceType.AutoDual)]
24         public class Assembly : System.Reflection.ICustomAttributeProvider,
25                 System.Security.IEvidenceFactory, System.Runtime.Serialization.ISerializable {
26                 private IntPtr _mono_assembly;
27
28                 internal Assembly () {}
29
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;
34
35                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
36                 private extern string get_code_base ();
37                 
38                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
39                 private extern string get_location ();
40
41                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
42                 private extern string InternalImageRuntimeVersion ();
43                 
44                 public virtual string CodeBase {
45                         get {
46                                 return get_code_base ();
47                         }
48                 }
49
50                 internal virtual string CopiedCodeBase {
51                         get {
52                                 return get_code_base ();
53                         }
54                 } 
55
56                 [MonoTODO]
57                 public virtual string EscapedCodeBase {
58                         get {
59                                 //FIXME: escape characters -> Uri
60                                 return get_code_base ();
61                         }
62                 }
63
64                 public virtual string FullName {
65                         get {
66                                 //
67                                 // FIXME: This is wrong, but it gets us going
68                                 // in the compiler for now
69                                 //
70                                 return GetName (false).ToString ();
71                         }
72                 }
73
74                 public virtual extern MethodInfo EntryPoint {
75                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
76                         get;
77                 }
78
79                 public virtual Evidence Evidence {
80                         get {
81                                 return null;
82                         }
83                 }
84
85                 public bool GlobalAssemblyCache {
86                         get {
87                                 //TODO: if we ever have a GAC, fix this.
88                                 return false;
89                         }
90                 }
91                 
92                 public virtual String Location {
93                         get {
94                                 return get_location ();
95                         }
96                 }
97
98 #if NET_1_1
99                 public virtual string ImageRuntimeVersion {
100                         get {
101                                 return InternalImageRuntimeVersion ();
102                         }
103                 }
104 #endif
105
106                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
107                 {
108                         UnitySerializationHolder.GetAssemblyData (this, info, context);
109                 }
110
111                 public virtual bool IsDefined (Type attributeType, bool inherit)
112                 {
113                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
114                 }
115
116                 public virtual object [] GetCustomAttributes (bool inherit)
117                 {
118                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
119                 }
120
121                 public virtual object [] GetCustomAttributes (Type attributeType, bool inherit)
122                 {
123                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
124                 }
125
126                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
127                 private extern object GetFilesInternal (String name);
128
129                 public virtual FileStream[] GetFiles ()
130                 {
131                         string[] names = (string[]) GetFilesInternal (null);
132                         if (names == null)
133                                 return new FileStream [0];
134
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);
138                         return res;
139                 }
140
141                 [MonoTODO]
142                 public virtual FileStream [] GetFiles (bool getResourceModules)
143                 {
144                         throw new NotImplementedException ();
145                 }
146
147                 public virtual FileStream GetFile (String name)
148                 {
149                         if (name == null)
150                                 throw new ArgumentNullException ("name");
151                         string filename = (string)GetFilesInternal (name);
152                         if (filename != null)
153                                 return new FileStream (filename, FileMode.Open, FileAccess.Read);
154                         else
155                                 return null;
156                 }
157
158                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
159                 private extern IntPtr GetManifestResourceInternal (String name, out int size, out Module module);
160
161                 public virtual Stream GetManifestResourceStream (String name)
162                 {
163                         if (name == null)
164                                 throw new ArgumentNullException ("name");
165                         if (name == "")
166                                 throw new ArgumentException ("name cannot have zero length.");
167
168                         ManifestResourceInfo info = GetManifestResourceInfo (name);
169                         if (info == null)
170                                 return null;
171
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),
176                                                                                         info.FileName);
177                                 return new FileStream (filename, FileMode.Open, FileAccess.Read);
178                         }
179
180                         int size;
181                         Module module;
182                         IntPtr data = GetManifestResourceInternal (name, out size, out module);
183                         if (data == (IntPtr) 0)
184                                 return null;
185                         else {
186                                 IntPtrStream stream = new IntPtrStream (data, size);
187                                 /* 
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.
191                                  */
192                                 stream.Closed += new EventHandler (new ResourceCloseHandler (module).OnClose);
193                                 return stream;
194                         }
195                 }
196
197                 public virtual Stream GetManifestResourceStream (Type type, String name)
198                 {
199                         string ns;
200                         if (type != null)
201                                 ns = type.Namespace;
202                         else 
203                                 ns = null;
204
205                         if ((ns == null) || (ns == ""))
206                                 return GetManifestResourceStream (name);
207                         else
208                                 return GetManifestResourceStream (ns + "." + name);
209                 }
210
211                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
212                 private extern Type[] GetTypes (bool exportedOnly);
213                 
214                 public virtual Type[] GetTypes ()
215                 {
216                         return GetTypes (false);
217                 }
218
219                 public virtual Type[] GetExportedTypes ()
220                 {
221                         return GetTypes (true);
222                 }
223
224                 public virtual Type GetType (String name, Boolean throwOnError)
225                 {
226                         return GetType (name, throwOnError, false);
227                 }
228
229                 public virtual Type GetType (String name) {
230                         return GetType (name, false, false);
231                 }
232
233                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
234                 internal extern Type InternalGetType (Module module, String name, Boolean throwOnError, Boolean ignoreCase);
235
236                 public Type GetType (string name, bool throwOnError, bool ignoreCase)
237                 {
238                         if (name == null)
239                                 throw new ArgumentNullException (name);
240
241                         return InternalGetType (null, name, throwOnError, ignoreCase);
242                 }
243
244                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
245                 internal extern static void InternalGetAssemblyName (string assemblyFile, AssemblyName aname);
246                 
247                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
248                 static extern void FillName (Assembly ass, AssemblyName aname);
249                 
250                 public virtual AssemblyName GetName (Boolean copiedName)
251                 {
252                         AssemblyName aname = new AssemblyName ();
253                         FillName (this, aname);
254                         return aname;
255                 }
256
257                 public virtual AssemblyName GetName ()
258                 {
259                         return GetName (false);
260                 }
261
262                 public override String ToString ()
263                 {
264                         return GetName ().Name;
265                 }
266
267                 [MonoTODO]
268                 public static String CreateQualifiedName (String assemblyName, String typeName) 
269                 {
270                         return typeName + "," + assemblyName;
271                 }
272
273                 public static Assembly GetAssembly (Type type)
274                 {
275                         if (type != null)
276                                 return type.Assembly;
277                         throw new ArgumentNullException ("type");
278                 }
279
280
281                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
282                 public static extern Assembly GetEntryAssembly();
283
284                 public Assembly GetSatelliteAssembly (CultureInfo culture)
285                 {
286                         return GetSatelliteAssembly (culture, null);
287                 }
288
289                 public Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
290                 {
291                         if (culture == null)
292                                 throw new ArgumentException ("culture");
293
294                         AssemblyName aname = GetName (true);
295                         if (version != null)
296                                 aname.Version = version;
297
298                         aname.CultureInfo = culture;
299                         aname.Name = aname.Name + ".resources";
300                         return Load (aname);
301                 }
302                 
303                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
304                 public extern static Assembly LoadFrom (String assemblyFile);
305
306                 [MonoTODO]
307                 public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence)
308                 {
309                         // Evidence is ignored
310                         return LoadFrom (assemblyFile);
311                 }
312
313 #if NET_1_1
314                 [MonoTODO]
315                 public static Assembly LoadFile (String path, Evidence securityEvidence) {
316                         if (path == null)
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);
322                 }
323
324                 public static Assembly LoadFile (String path) {
325                         return LoadFile (path, null);
326                 }
327 #endif
328
329                 public static Assembly Load (String assemblyString)
330                 {
331                         return AppDomain.CurrentDomain.Load (assemblyString);
332                 }
333                 
334                 public static Assembly Load (String assemblyString, Evidence assemblySecurity)
335                 {
336                         return AppDomain.CurrentDomain.Load (assemblyString, assemblySecurity);
337                 }
338
339                 public static Assembly Load (AssemblyName assemblyRef)
340                 {
341                         return AppDomain.CurrentDomain.Load (assemblyRef);
342                 }
343
344                 public static Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
345                 {
346                         return AppDomain.CurrentDomain.Load (assemblyRef, assemblySecurity);
347                 }
348
349                 public static Assembly Load (Byte[] rawAssembly)
350                 {
351                         return AppDomain.CurrentDomain.Load (rawAssembly);
352                 }
353
354                 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore)
355                 {
356                         return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
357                 }
358
359                 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore,
360                                              Evidence securityEvidence)
361                 {
362                         return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore, securityEvidence);
363                 }
364
365                 public static Assembly LoadWithPartialName (string partialName)
366                 {
367                         return LoadWithPartialName (partialName, null);
368                 }
369
370                 [MonoTODO]
371                 public Module LoadModule (string moduleName, byte [] rawModule)
372                 {
373                         throw new NotImplementedException ();
374                 }
375
376                 [MonoTODO]
377                 public Module LoadModule (string moduleName, byte [] rawModule, byte [] rawSymbolStore)
378                 {
379                         throw new NotImplementedException ();
380                 }
381
382                 [MonoTODO]
383                 public static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence)
384                 {
385                         try {
386                                 return AppDomain.CurrentDomain.Load (partialName, securityEvidence);
387                         }
388                         catch (Exception) {
389                                 // According to MSDN, this should return null instead of
390                                 // throwing an exception
391                                 return null;
392                         }
393                 }
394
395                 public Object CreateInstance (String typeName) 
396                 {
397                         return CreateInstance (typeName, false);
398                 }
399
400                 public Object CreateInstance (String typeName, Boolean ignoreCase)
401                 {
402                         Type t = GetType (typeName, false, ignoreCase);
403                         if (t == null)
404                                 return null;
405
406                         return Activator.CreateInstance (t);
407                 }
408
409                 public Object CreateInstance (String typeName, Boolean ignoreCase,
410                                               BindingFlags bindingAttr, Binder binder,
411                                               Object[] args, CultureInfo culture,
412                                               Object[] activationAttributes)
413                 {
414                         Type t = GetType (typeName, false, ignoreCase);
415                         if (t == null)
416                                 return null;
417
418                         return Activator.CreateInstance (t, bindingAttr, binder, args, culture, activationAttributes);
419                 }
420
421                 public Module[] GetLoadedModules ()
422                 {
423                         return GetLoadedModules (false);
424                 }
425
426                 [MonoTODO]
427                 public Module[] GetLoadedModules (bool getResourceModules)
428                 {
429                         // Currently, the two sets of modules are equal
430                         return GetModules (getResourceModules);
431                 }
432
433                 public Module[] GetModules ()
434                 {
435                         return GetModules (false);
436                 }
437
438                 public Module GetModule (String name)
439                 {
440                         if (name == null)
441                                 throw new ArgumentNullException ("name");
442                         if (name == "")
443                                 throw new ArgumentException ("Name can't be empty");
444
445                         Module[] modules = GetModules (true);
446                         foreach (Module module in GetModules (true)) {
447                                 if (module.ScopeName == name)
448                                         return module;
449                         }
450
451                         return null;
452                 }
453
454                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
455                 internal extern Module[] GetModulesInternal ();
456
457                 public Module[] GetModules (bool getResourceModules) {
458                         Module[] modules = GetModulesInternal ();
459
460                         if (!getResourceModules) {
461                                 ArrayList result = new ArrayList (modules.Length);
462                                 foreach (Module m in modules)
463                                         if (!m.IsResource ())
464                                                 result.Add (m);
465                                 return (Module[])result.ToArray (typeof (Module));
466                         }
467                         else
468                                 return modules;
469                 }
470
471                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
472                 internal extern string[] GetNamespaces ();
473                 
474                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
475                 public extern virtual String[] GetManifestResourceNames ();
476
477                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
478                 public extern static Assembly GetExecutingAssembly ();
479
480                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
481                 public extern static Assembly GetCallingAssembly ();
482
483                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
484                 public extern AssemblyName[] GetReferencedAssemblies ();
485
486                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
487                 private extern bool GetManifestResourceInfoInternal (String name, ManifestResourceInfo info);
488
489                 public virtual ManifestResourceInfo GetManifestResourceInfo (String resourceName)
490                 {
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);
497                         if (found)
498                                 return result;
499                         else
500                                 return null;
501                 }
502
503                 private class ResourceCloseHandler {
504
505                         Module module;
506
507                         public ResourceCloseHandler (Module module) {
508                                 this.module = module;
509                         }
510
511                         public void OnClose (object sender, EventArgs e) {
512                                 // The module dtor will take care of things
513                                 module = null;
514                         }
515                 }
516
517                 //
518                 // The following functions are only for the Mono Debugger.
519                 //
520
521                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
522                 internal static extern MethodBase MonoDebugger_GetMethod (Assembly assembly, int token);
523
524                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
525                 internal static extern int MonoDebugger_GetMethodToken (Assembly assembly, MethodBase method);
526
527                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
528                 internal static extern Type MonoDebugger_GetLocalTypeFromSignature (Assembly assembly, byte[] signature);
529
530                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
531                 internal static extern Type MonoDebugger_GetType (Assembly assembly, int token);
532         }
533 }