2003-12-17 Zoltan Varga <vargaz@freemail.hu>
[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);
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                         IntPtr data = GetManifestResourceInternal (name, out size);
182                         if (data == (IntPtr) 0)
183                                 return null;
184                         else
185                                 return new IntPtrStream (data, size);
186                 }
187
188                 public virtual Stream GetManifestResourceStream (Type type, String name)
189                 {
190                         string ns;
191                         if (type != null)
192                                 ns = type.Namespace;
193                         else 
194                                 ns = null;
195
196                         if ((ns == null) || (ns == ""))
197                                 return GetManifestResourceStream (name);
198                         else
199                                 return GetManifestResourceStream (ns + "." + name);
200                 }
201
202                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
203                 private extern Type[] GetTypes (bool exportedOnly);
204                 
205                 public virtual Type[] GetTypes ()
206                 {
207                         return GetTypes (false);
208                 }
209
210                 public virtual Type[] GetExportedTypes ()
211                 {
212                         return GetTypes (true);
213                 }
214
215                 public virtual Type GetType (String name, Boolean throwOnError)
216                 {
217                         return GetType (name, throwOnError, false);
218                 }
219
220                 public virtual Type GetType (String name) {
221                         return GetType (name, false, false);
222                 }
223
224                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
225                 internal extern Type InternalGetType (Module module, String name, Boolean throwOnError, Boolean ignoreCase);
226
227                 public Type GetType (string name, bool throwOnError, bool ignoreCase)
228                 {
229                         if (name == null)
230                                 throw new ArgumentNullException (name);
231
232                         return InternalGetType (null, name, throwOnError, ignoreCase);
233                 }
234
235                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
236                 internal extern static void InternalGetAssemblyName (string assemblyFile, AssemblyName aname);
237                 
238                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
239                 static extern void FillName (Assembly ass, AssemblyName aname);
240                 
241                 public virtual AssemblyName GetName (Boolean copiedName)
242                 {
243                         AssemblyName aname = new AssemblyName ();
244                         FillName (this, aname);
245                         return aname;
246                 }
247
248                 public virtual AssemblyName GetName ()
249                 {
250                         return GetName (false);
251                 }
252
253                 public override String ToString ()
254                 {
255                         return GetName ().Name;
256                 }
257
258                 [MonoTODO]
259                 public static String CreateQualifiedName (String assemblyName, String typeName) 
260                 {
261                         return typeName + "," + assemblyName;
262                 }
263
264                 public static Assembly GetAssembly (Type type)
265                 {
266                         if (type != null)
267                                 return type.Assembly;
268                         throw new ArgumentNullException ("type");
269                 }
270
271
272                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
273                 public static extern Assembly GetEntryAssembly();
274
275                 public Assembly GetSatelliteAssembly (CultureInfo culture)
276                 {
277                         return GetSatelliteAssembly (culture, null);
278                 }
279
280                 public Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
281                 {
282                         if (culture == null)
283                                 throw new ArgumentException ("culture");
284
285                         AssemblyName aname = GetName (true);
286                         if (version != null)
287                                 aname.Version = version;
288
289                         aname.CultureInfo = culture;
290                         aname.Name = aname.Name + ".resources";
291                         return Load (aname);
292                 }
293                 
294                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
295                 public extern static Assembly LoadFrom (String assemblyFile);
296
297                 [MonoTODO]
298                 public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence)
299                 {
300                         throw new NotImplementedException ();
301                 }
302
303 #if NET_1_1
304                 [MonoTODO]
305                 public static Assembly LoadFile (String path, Evidence securityEvidence) {
306                         if (path == null)
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);
312                 }
313
314                 public static Assembly LoadFile (String path) {
315                         return LoadFile (path, null);
316                 }
317 #endif
318
319                 public static Assembly Load (String assemblyString)
320                 {
321                         return AppDomain.CurrentDomain.Load (assemblyString);
322                 }
323                 
324                 public static Assembly Load (String assemblyString, Evidence assemblySecurity)
325                 {
326                         return AppDomain.CurrentDomain.Load (assemblyString, assemblySecurity);
327                 }
328
329                 public static Assembly Load (AssemblyName assemblyRef)
330                 {
331                         return AppDomain.CurrentDomain.Load (assemblyRef);
332                 }
333
334                 public static Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
335                 {
336                         return AppDomain.CurrentDomain.Load (assemblyRef, assemblySecurity);
337                 }
338
339                 public static Assembly Load (Byte[] rawAssembly)
340                 {
341                         return AppDomain.CurrentDomain.Load (rawAssembly);
342                 }
343
344                 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore)
345                 {
346                         return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
347                 }
348
349                 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore,
350                                              Evidence securityEvidence)
351                 {
352                         return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore, securityEvidence);
353                 }
354
355                 public static Assembly LoadWithPartialName (string partialName)
356                 {
357                         return LoadWithPartialName (partialName, null);
358                 }
359
360                 [MonoTODO]
361                 public Module LoadModule (string moduleName, byte [] rawModule)
362                 {
363                         throw new NotImplementedException ();
364                 }
365
366                 [MonoTODO]
367                 public Module LoadModule (string moduleName, byte [] rawModule, byte [] rawSymbolStore)
368                 {
369                         throw new NotImplementedException ();
370                 }
371
372                 [MonoTODO]
373                 public static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence)
374                 {
375                         try {
376                                 return AppDomain.CurrentDomain.Load (partialName, securityEvidence);
377                         }
378                         catch (Exception ex) {
379                                 // According to MSDN, this should return null instead of
380                                 // throwing an exception
381                                 return null;
382                         }
383                 }
384
385                 public Object CreateInstance (String typeName) 
386                 {
387                         return CreateInstance (typeName, false);
388                 }
389
390                 public Object CreateInstance (String typeName, Boolean ignoreCase)
391                 {
392                         Type t = GetType (typeName, false, ignoreCase);
393                         if (t == null)
394                                 return null;
395
396                         return Activator.CreateInstance (t);
397                 }
398
399                 public Object CreateInstance (String typeName, Boolean ignoreCase,
400                                               BindingFlags bindingAttr, Binder binder,
401                                               Object[] args, CultureInfo culture,
402                                               Object[] activationAttributes)
403                 {
404                         Type t = GetType (typeName, false, ignoreCase);
405                         if (t == null)
406                                 return null;
407
408                         return Activator.CreateInstance (t, bindingAttr, binder, args, culture, activationAttributes);
409                 }
410
411                 public Module[] GetLoadedModules ()
412                 {
413                         return GetLoadedModules (false);
414                 }
415
416                 [MonoTODO]
417                 public Module[] GetLoadedModules (bool getResourceModules)
418                 {
419                         // Currently, the two sets of modules are equal
420                         return GetModules (getResourceModules);
421                 }
422
423                 public Module[] GetModules ()
424                 {
425                         return GetModules (false);
426                 }
427
428                 public Module GetModule (String name)
429                 {
430                         if (name == null)
431                                 throw new ArgumentNullException ("name");
432                         if (name == "")
433                                 throw new ArgumentException ("Name can't be empty");
434
435                         Module[] modules = GetModules (true);
436                         foreach (Module module in GetModules (true)) {
437                                 if (module.ScopeName == name)
438                                         return module;
439                         }
440
441                         return null;
442                 }
443
444                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
445                 internal extern Module[] GetModulesInternal ();
446
447                 public Module[] GetModules (bool getResourceModules) {
448                         Module[] modules = GetModulesInternal ();
449
450                         if (!getResourceModules) {
451                                 ArrayList result = new ArrayList (modules.Length);
452                                 foreach (Module m in modules)
453                                         if (!m.IsResource ())
454                                                 result.Add (m);
455                                 return (Module[])result.ToArray (typeof (Module));
456                         }
457                         else
458                                 return modules;
459                 }
460
461                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
462                 internal extern string[] GetNamespaces ();
463                 
464                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
465                 public extern virtual String[] GetManifestResourceNames ();
466
467                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
468                 public extern static Assembly GetExecutingAssembly ();
469
470                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
471                 public extern static Assembly GetCallingAssembly ();
472
473                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
474                 public extern AssemblyName[] GetReferencedAssemblies ();
475
476                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
477                 private extern bool GetManifestResourceInfoInternal (String name, ManifestResourceInfo info);
478
479                 public virtual ManifestResourceInfo GetManifestResourceInfo (String resourceName)
480                 {
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);
487                         if (found)
488                                 return result;
489                         else
490                                 return null;
491                 }
492
493                 //
494                 // The following functions are only for the Mono Debugger.
495                 //
496                 // They should be marked `internal', and extracted with GetMethod from the debugger.
497                 //
498
499                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
500                 public extern MethodBase MonoDebugger_GetMethod (int token);
501
502                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
503                 public extern int MonoDebugger_GetMethodToken (MethodBase method);
504
505                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
506                 public extern Type MonoDebugger_GetLocalTypeFromSignature (byte[] signature);
507
508                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
509                 public extern Type MonoDebugger_GetType (int token);
510         }
511 }