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