2009-09-18 Sebastien Pouliot <sebastien@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 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Security;
31 using System.Security.Policy;
32 using System.Security.Permissions;
33 using System.Runtime.Serialization;
34 using System.Reflection.Emit;
35 using System.IO;
36 using System.Globalization;
37 using System.Runtime.CompilerServices;
38 using System.Runtime.InteropServices;
39 using System.Collections;
40 using System.Configuration.Assemblies;
41
42 using Mono.Security;
43
44 namespace System.Reflection {
45
46 #if NET_2_0
47         [ComVisible (true)]
48         [ComDefaultInterfaceAttribute (typeof (_Assembly))]
49 #endif
50         [Serializable]
51         [ClassInterface(ClassInterfaceType.None)]
52         public class Assembly : System.Reflection.ICustomAttributeProvider, _Assembly,
53                 System.Security.IEvidenceFactory, System.Runtime.Serialization.ISerializable {
54
55                 internal class ResolveEventHolder {
56                         public event ModuleResolveEventHandler ModuleResolve;
57                 }
58
59                 // Note: changes to fields must be reflected in _MonoReflectionAssembly struct (object-internals.h)
60 #pragma warning disable 169
61                 private IntPtr _mono_assembly;
62 #pragma warning restore 169
63
64                 private ResolveEventHolder resolve_event_holder;
65                 private Evidence _evidence;
66                 internal PermissionSet _minimum;        // for SecurityAction.RequestMinimum
67                 internal PermissionSet _optional;       // for SecurityAction.RequestOptional
68                 internal PermissionSet _refuse;         // for SecurityAction.RequestRefuse
69                 private PermissionSet _granted;         // for the resolved assembly granted permissions
70                 private PermissionSet _denied;          // for the resolved assembly denied permissions
71                 private bool fromByteArray;
72                 private string assemblyName;
73
74                 internal Assembly () 
75                 {
76                         resolve_event_holder = new ResolveEventHolder ();
77                 }
78
79                 //
80                 // We can't store the event directly in this class, since the
81                 // compiler would silently insert the fields before _mono_assembly
82                 //
83                 public event ModuleResolveEventHandler ModuleResolve {
84                         [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
85                         add {
86                                 resolve_event_holder.ModuleResolve += value;
87                         }
88                         [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
89                         remove {
90                                 resolve_event_holder.ModuleResolve -= value;
91                         }
92                 }
93
94                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
95                 private extern string get_code_base (bool escaped);
96
97                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
98                 private extern string get_fullname ();
99
100                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
101                 private extern string get_location ();
102
103                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
104                 private extern string InternalImageRuntimeVersion ();
105
106                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
107                 private extern bool get_global_assembly_cache ();
108
109                 // SECURITY: this should be the only caller to icall get_code_base
110                 private string GetCodeBase (bool escaped)
111                 {
112                         string cb = get_code_base (escaped);
113 #if !NET_2_1
114                         if (SecurityManager.SecurityEnabled) {
115                                 // we cannot divulge local file informations
116                                 if (String.Compare ("FILE://", 0, cb, 0, 7, true, CultureInfo.InvariantCulture) == 0) {
117                                         string file = cb.Substring (7);
118                                         new FileIOPermission (FileIOPermissionAccess.PathDiscovery, file).Demand ();
119                                 }
120                         }
121 #endif
122                         return cb;
123                 }
124
125                 public virtual string CodeBase {
126                         get { return GetCodeBase (false); }
127                 }
128
129                 public virtual string EscapedCodeBase {
130                         get { return GetCodeBase (true); }
131                 }
132
133                 public virtual string FullName {
134                         get {
135                                 //
136                                 // FIXME: This is wrong, but it gets us going
137                                 // in the compiler for now
138                                 //
139                                 return ToString ();
140                         }
141                 }
142
143                 public virtual extern MethodInfo EntryPoint {
144                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
145                         get;
146                 }
147
148                 public virtual Evidence Evidence {
149                         [SecurityPermission (SecurityAction.Demand, ControlEvidence = true)]
150                         get { return UnprotectedGetEvidence (); }
151                 }
152
153                 // note: the security runtime requires evidences but may be unable to do so...
154                 internal Evidence UnprotectedGetEvidence ()
155                 {
156                         // if the host (runtime) hasn't provided it's own evidence...
157                         if (_evidence == null) {
158                                 // ... we will provide our own
159                                 lock (this) {
160                                         _evidence = Evidence.GetDefaultHostEvidence (this);
161                                 }
162                         }
163                         return _evidence;
164                 }
165
166                 public bool GlobalAssemblyCache {
167                         get {
168                                 return get_global_assembly_cache ();
169                         }
170                 }
171
172                 internal bool FromByteArray {
173                         set { fromByteArray = value; }
174                 }
175
176                 public virtual String Location {
177                         get {
178                                 if (fromByteArray)
179                                         return String.Empty;
180
181                                 string loc = get_location ();
182 #if !NET_2_1
183                                 if ((loc != String.Empty) && SecurityManager.SecurityEnabled) {
184                                         // we cannot divulge local file informations
185                                         new FileIOPermission (FileIOPermissionAccess.PathDiscovery, loc).Demand ();
186                                 }
187 #endif
188                                 return loc;
189                         }
190                 }
191
192 #if NET_1_1
193                 [ComVisible (false)]
194                 public virtual string ImageRuntimeVersion {
195                         get {
196                                 return InternalImageRuntimeVersion ();
197                         }
198                 }
199 #endif
200
201                 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
202                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
203                 {
204                         if (info == null)
205                                 throw new ArgumentNullException ("info");
206
207                         UnitySerializationHolder.GetAssemblyData (this, info, context);
208                 }
209
210 #if ONLY_1_1
211                 public new Type GetType ()
212                 {
213                         return base.GetType ();
214                 }
215 #endif
216
217                 public virtual bool IsDefined (Type attributeType, bool inherit)
218                 {
219                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
220                 }
221
222                 public virtual object [] GetCustomAttributes (bool inherit)
223                 {
224                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
225                 }
226
227                 public virtual object [] GetCustomAttributes (Type attributeType, bool inherit)
228                 {
229                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
230                 }
231
232                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
233                 private extern object GetFilesInternal (String name, bool getResourceModules);
234
235                 public virtual FileStream[] GetFiles ()
236                 {
237                         return GetFiles (false);
238                 }
239
240                 public virtual FileStream [] GetFiles (bool getResourceModules)
241                 {
242                         string[] names = (string[]) GetFilesInternal (null, getResourceModules);
243                         if (names == null)
244                                 return new FileStream [0];
245
246                         string location = Location;
247
248                         FileStream[] res;
249                         if (location != String.Empty) {
250                                 res = new FileStream [names.Length + 1];
251                                 res [0] = new FileStream (location, FileMode.Open, FileAccess.Read);
252                                 for (int i = 0; i < names.Length; ++i)
253                                         res [i + 1] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
254                         } else {
255                                 res = new FileStream [names.Length];
256                                 for (int i = 0; i < names.Length; ++i)
257                                         res [i] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
258                         }
259                         return res;
260                 }
261
262                 public virtual FileStream GetFile (String name)
263                 {
264                         if (name == null)
265                                 throw new ArgumentNullException (null, "Name cannot be null.");
266                         if (name.Length == 0)
267                                 throw new ArgumentException ("Empty name is not valid");
268
269                         string filename = (string)GetFilesInternal (name, true);
270                         if (filename != null)
271                                 return new FileStream (filename, FileMode.Open, FileAccess.Read);
272                         else
273                                 return null;
274                 }
275
276                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
277                 internal extern IntPtr GetManifestResourceInternal (String name, out int size, out Module module);
278
279                 public virtual Stream GetManifestResourceStream (String name)
280                 {
281                         if (name == null)
282                                 throw new ArgumentNullException ("name");
283                         if (name.Length == 0)
284                                 throw new ArgumentException ("String cannot have zero length.",
285                                         "name");
286
287                         ManifestResourceInfo info = GetManifestResourceInfo (name);
288                         if (info == null)
289                                 return null;
290
291                         if (info.ReferencedAssembly != null)
292                                 return info.ReferencedAssembly.GetManifestResourceStream (name);
293                         if ((info.FileName != null) && (info.ResourceLocation == 0)) {
294                                 if (fromByteArray)
295 #if NET_2_0
296                                         throw new FileNotFoundException (info.FileName);
297 #else
298                                         return null;
299 #endif
300
301                                 string filename = Path.Combine (Path.GetDirectoryName (Location),
302                                                                                         info.FileName);
303                                 return new FileStream (filename, FileMode.Open, FileAccess.Read);
304                         }
305
306                         int size;
307                         Module module;
308                         IntPtr data = GetManifestResourceInternal (name, out size, out module);
309                         if (data == (IntPtr) 0)
310                                 return null;
311                         else {
312 #if NET_2_0
313                                 UnmanagedMemoryStream stream;
314                                 unsafe {
315                                         stream = new UnmanagedMemoryStream ((byte*) data, size);
316                                 }
317 #else
318                                 IntPtrStream stream = new IntPtrStream (data, size);
319 #endif
320                                 /* 
321                                  * The returned pointer points inside metadata, so
322                                  * we have to increase the refcount of the module, and decrease
323                                  * it when the stream is finalized.
324                                  */
325                                 stream.Closed += new EventHandler (new ResourceCloseHandler (module).OnClose);
326                                 return stream;
327                         }
328                 }
329
330                 public virtual Stream GetManifestResourceStream (Type type, String name)
331                 {
332                         string ns;
333                         if (type != null) {
334                                 ns = type.Namespace;
335                         } else {
336                                 if (name == null)
337                                         throw new ArgumentNullException ("type");
338                                 ns = null;
339                         }
340
341                         if (ns == null || ns.Length == 0)
342                                 return GetManifestResourceStream (name);
343                         else
344                                 return GetManifestResourceStream (ns + "." + name);
345                 }
346
347                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
348                 internal virtual extern Type[] GetTypes (bool exportedOnly);
349                 
350                 public virtual Type[] GetTypes ()
351                 {
352                         return GetTypes (false);
353                 }
354
355                 public virtual Type[] GetExportedTypes ()
356                 {
357                         return GetTypes (true);
358                 }
359
360                 public virtual Type GetType (String name, Boolean throwOnError)
361                 {
362                         return GetType (name, throwOnError, false);
363                 }
364
365                 public virtual Type GetType (String name) {
366                         return GetType (name, false, false);
367                 }
368
369                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
370                 internal extern Type InternalGetType (Module module, String name, Boolean throwOnError, Boolean ignoreCase);
371
372                 public Type GetType (string name, bool throwOnError, bool ignoreCase)
373                 {
374                         if (name == null)
375                                 throw new ArgumentNullException (name);
376                         if (name.Length == 0)
377                         throw new ArgumentException ("name", "Name cannot be empty");
378
379                         return InternalGetType (null, name, throwOnError, ignoreCase);
380                 }
381
382                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
383                 internal extern static void InternalGetAssemblyName (string assemblyFile, AssemblyName aname);
384
385                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
386                 static extern void FillName (Assembly ass, AssemblyName aname);
387
388                 [MonoTODO ("copiedName == true is not supported")]
389                 public virtual AssemblyName GetName (Boolean copiedName)
390                 {
391                         // CodeBase, which is restricted, will be copied into the AssemblyName object so...
392                         if (SecurityManager.SecurityEnabled) {
393                                 GetCodeBase (true); // this will ensure the Demand is made
394                         }
395                         return UnprotectedGetName ();
396                 }
397
398                 public virtual AssemblyName GetName ()
399                 {
400                         return GetName (false);
401                 }
402
403                 // the security runtime requires access to the assemblyname (e.g. to get the strongname)
404                 internal virtual AssemblyName UnprotectedGetName ()
405                 {
406                         AssemblyName aname = new AssemblyName ();
407                         FillName (this, aname);
408                         return aname;
409                 }
410
411                 public override string ToString ()
412                 {
413                         // note: ToString work without requiring CodeBase (so no checks are needed)
414
415                         if (assemblyName != null)
416                                 return assemblyName;
417
418                         assemblyName = get_fullname ();
419                         return assemblyName;
420                 }
421
422                 public static String CreateQualifiedName (String assemblyName, String typeName) 
423                 {
424                         return typeName + ", " + assemblyName;
425                 }
426
427                 public static Assembly GetAssembly (Type type)
428                 {
429                         if (type != null)
430                                 return type.Assembly;
431                         throw new ArgumentNullException ("type");
432                 }
433
434
435                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
436                 public static extern Assembly GetEntryAssembly();
437
438                 public Assembly GetSatelliteAssembly (CultureInfo culture)
439                 {
440                         return GetSatelliteAssembly (culture, null);
441                 }
442
443                 public Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
444                 {
445                         if (culture == null)
446                                 throw new ArgumentException ("culture");
447
448                         AssemblyName aname = GetName (true);
449                         if (version != null)
450                                 aname.Version = version;
451
452                         aname.CultureInfo = culture;
453                         aname.Name = aname.Name + ".resources";
454                         Assembly assembly = AppDomain.CurrentDomain.LoadSatellite (aname);
455                         if (assembly != null)
456                                 return assembly;
457
458                         // Try the assembly directory
459                         string fullName = Path.Combine (Path.GetDirectoryName (Location), Path.Combine (culture.Name, aname.Name + ".dll"));
460                         return LoadFrom (fullName);
461                 }
462
463                 internal Assembly GetSatelliteAssemblyNoThrow (CultureInfo culture, Version version)
464                 {
465                         if (culture == null)
466                                 throw new ArgumentException ("culture");
467
468                         AssemblyName aname = GetName (true);
469                         if (version != null)
470                                 aname.Version = version;
471
472                         aname.CultureInfo = culture;
473                         aname.Name = aname.Name + ".resources";
474                         Assembly assembly = AppDomain.CurrentDomain.LoadSatellite (aname);
475                         if (assembly != null)
476                                 return assembly;
477
478                         // Try the assembly directory
479                         string fullName = Path.Combine (Path.GetDirectoryName (Location), Path.Combine (culture.Name, aname.Name + ".dll"));
480                         if (!File.Exists (fullName))
481                                 return null;
482                         return LoadFrom (fullName);
483                 }
484                 
485                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
486                 private extern static Assembly LoadFrom (String assemblyFile, bool refonly);
487
488                 public static Assembly LoadFrom (String assemblyFile)
489                 {
490                         return LoadFrom (assemblyFile, false);
491                 }
492
493                 public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence)
494                 {
495                         Assembly a = LoadFrom (assemblyFile, false);
496                         if ((a != null) && (securityEvidence != null)) {
497                                 // merge evidence (i.e. replace defaults with provided evidences)
498                                 a.Evidence.Merge (securityEvidence);
499                         }
500                         return a;
501                 }
502
503 #if NET_1_1
504
505                 [MonoTODO("This overload is not currently implemented")]
506                 // FIXME: What are we missing?
507                 public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
508                 {
509                         if (assemblyFile == null)
510                                 throw new ArgumentNullException ("assemblyFile");
511                         if (assemblyFile == String.Empty)
512                                 throw new ArgumentException ("Name can't be the empty string", "assemblyFile");
513                         throw new NotImplementedException ();
514                 }
515
516                 public static Assembly LoadFile (String path, Evidence securityEvidence)
517                 {
518                         if (path == null)
519                                 throw new ArgumentNullException ("path");
520                         if (path == String.Empty)
521                                 throw new ArgumentException ("Path can't be empty", "path");
522                         // FIXME: Make this do the right thing
523                         return LoadFrom (path, securityEvidence);
524                 }
525
526                 public static Assembly LoadFile (String path)
527                 {
528                         return LoadFile (path, null);
529                 }
530 #endif
531
532                 public static Assembly Load (String assemblyString)
533                 {
534                         return AppDomain.CurrentDomain.Load (assemblyString);
535                 }
536                 
537                 public static Assembly Load (String assemblyString, Evidence assemblySecurity)
538                 {
539                         return AppDomain.CurrentDomain.Load (assemblyString, assemblySecurity);
540                 }
541
542                 public static Assembly Load (AssemblyName assemblyRef)
543                 {
544                         return AppDomain.CurrentDomain.Load (assemblyRef);
545                 }
546
547                 public static Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
548                 {
549                         return AppDomain.CurrentDomain.Load (assemblyRef, assemblySecurity);
550                 }
551
552                 public static Assembly Load (Byte[] rawAssembly)
553                 {
554                         return AppDomain.CurrentDomain.Load (rawAssembly);
555                 }
556
557                 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore)
558                 {
559                         return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
560                 }
561
562                 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore,
563                                              Evidence securityEvidence)
564                 {
565                         return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore, securityEvidence);
566                 }
567
568 #if NET_2_0 || BOOTSTRAP_NET_2_0
569                 public static Assembly ReflectionOnlyLoad (byte[] rawAssembly)
570                 {
571                         return AppDomain.CurrentDomain.Load (rawAssembly, null, null, true);
572                 }
573
574                 public static Assembly ReflectionOnlyLoad (string assemblyString) 
575                 {
576                         return AppDomain.CurrentDomain.Load (assemblyString, null, true);
577                 }
578
579                 public static Assembly ReflectionOnlyLoadFrom (string assemblyFile) 
580                 {
581                         if (assemblyFile == null)
582                                 throw new ArgumentNullException ("assemblyFile");
583                         
584                         return LoadFrom (assemblyFile, true);
585                 }
586 #endif
587
588 #if NET_2_0
589                 [Obsolete ("")]
590 #endif
591                 public static Assembly LoadWithPartialName (string partialName)
592                 {
593                         return LoadWithPartialName (partialName, null);
594                 }
595
596                 [MonoTODO ("Not implemented")]
597                 public Module LoadModule (string moduleName, byte [] rawModule)
598                 {
599                         throw new NotImplementedException ();
600                 }
601
602                 [MonoTODO ("Not implemented")]
603                 public Module LoadModule (string moduleName, byte [] rawModule, byte [] rawSymbolStore)
604                 {
605                         throw new NotImplementedException ();
606                 }
607
608                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
609                 private static extern Assembly load_with_partial_name (string name, Evidence e);
610
611 #if NET_2_0
612                 [Obsolete ("")]
613 #endif
614                 public static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence)
615                 {
616                         return LoadWithPartialName (partialName, securityEvidence, true);
617                 }
618
619                 /**
620                  * LAMESPEC: It is possible for this method to throw exceptions IF the name supplied
621                  * is a valid gac name and contains filesystem entry charachters at the end of the name
622                  * ie System/// will throw an exception. However ////System will not as that is canocolized
623                  * out of the name.
624                  */
625
626                 // FIXME: LoadWithPartialName must look cache (no CAS) or read from disk (CAS)
627                 internal static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence, bool oldBehavior)
628                 {
629                         if (!oldBehavior)
630                                 throw new NotImplementedException ();
631
632                         if (partialName == null)
633                                 throw new NullReferenceException ();
634
635                         return load_with_partial_name (partialName, securityEvidence);
636                 }
637
638                 public Object CreateInstance (String typeName) 
639                 {
640                         return CreateInstance (typeName, false);
641                 }
642
643                 public Object CreateInstance (String typeName, Boolean ignoreCase)
644                 {
645                         Type t = GetType (typeName, false, ignoreCase);
646                         if (t == null)
647                                 return null;
648
649                         try {
650                                 return Activator.CreateInstance (t);
651                         } catch (InvalidOperationException) {
652                                 throw new ArgumentException ("It is illegal to invoke a method on a Type loaded via ReflectionOnly methods.");
653                         }
654                 }
655
656                 public Object CreateInstance (String typeName, Boolean ignoreCase,
657                                               BindingFlags bindingAttr, Binder binder,
658                                               Object[] args, CultureInfo culture,
659                                               Object[] activationAttributes)
660                 {
661                         Type t = GetType (typeName, false, ignoreCase);
662                         if (t == null)
663                                 return null;
664
665                         try {
666                                 return Activator.CreateInstance (t, bindingAttr, binder, args, culture, activationAttributes);
667                         } catch (InvalidOperationException) {
668                                 throw new ArgumentException ("It is illegal to invoke a method on a Type loaded via ReflectionOnly methods.");
669                         }
670                 }
671
672                 public Module[] GetLoadedModules ()
673                 {
674                         return GetLoadedModules (false);
675                 }
676
677                 // FIXME: Currently, the two sets of modules are equal
678                 public Module[] GetLoadedModules (bool getResourceModules)
679                 {
680                         return GetModules (getResourceModules);
681                 }
682
683                 public Module[] GetModules ()
684                 {
685                         return GetModules (false);
686                 }
687
688                 public Module GetModule (String name)
689                 {
690                         if (name == null)
691                                 throw new ArgumentNullException ("name");
692                         if (name.Length == 0)
693                                 throw new ArgumentException ("Name can't be empty");
694
695                         Module[] modules = GetModules (true);
696                         foreach (Module module in modules) {
697                                 if (module.ScopeName == name)
698                                         return module;
699                         }
700
701                         return null;
702                 }
703
704                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
705                 internal virtual extern Module[] GetModulesInternal ();
706
707                 public Module[] GetModules (bool getResourceModules) {
708                         Module[] modules = GetModulesInternal ();
709
710                         if (!getResourceModules) {
711                                 ArrayList result = new ArrayList (modules.Length);
712                                 foreach (Module m in modules)
713                                         if (!m.IsResource ())
714                                                 result.Add (m);
715                                 return (Module[])result.ToArray (typeof (Module));
716                         }
717                         else
718                                 return modules;
719                 }
720
721                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
722                 internal extern string[] GetNamespaces ();
723                 
724                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
725                 public extern virtual String[] GetManifestResourceNames ();
726
727                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
728                 public extern static Assembly GetExecutingAssembly ();
729
730                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
731                 public extern static Assembly GetCallingAssembly ();
732
733                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
734                 public extern AssemblyName[] GetReferencedAssemblies ();
735
736                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
737                 private extern bool GetManifestResourceInfoInternal (String name, ManifestResourceInfo info);
738
739                 public virtual ManifestResourceInfo GetManifestResourceInfo (String resourceName)
740                 {
741                         if (resourceName == null)
742                                 throw new ArgumentNullException ("resourceName");
743                         if (resourceName.Length == 0)
744                                 throw new ArgumentException ("String cannot have zero length.");
745                         ManifestResourceInfo result = new ManifestResourceInfo ();
746                         bool found = GetManifestResourceInfoInternal (resourceName, result);
747                         if (found)
748                                 return result;
749                         else
750                                 return null;
751                 }
752
753                 private class ResourceCloseHandler {
754 #pragma warning disable 169, 414
755                         Module module;
756 #pragma warning restore 169, 414                        
757
758                         public ResourceCloseHandler (Module module) {
759                                 this.module = module;
760                         }
761
762                         public void OnClose (object sender, EventArgs e) {
763                                 // The module dtor will take care of things
764                                 module = null;
765                         }
766                 }
767
768                 //
769                 // The following functions are only for the Mono Debugger.
770                 //
771
772                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
773                 internal static extern int MonoDebugger_GetMethodToken (MethodBase method);
774
775 #if NET_2_0 || BOOTSTRAP_NET_2_0
776                 [MonoTODO ("Always returns zero")]
777                 [ComVisible (false)]
778                 public long HostContext {
779                         get { return 0; }
780                 }
781
782                 [ComVisible (false)]
783                 public Module ManifestModule {
784                         get {
785                                 return GetManifestModule ();
786                         }
787                 }
788
789                 internal virtual Module GetManifestModule () {
790                         return GetManifestModuleInternal ();
791                 }
792
793                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
794                 internal extern Module GetManifestModuleInternal ();
795
796                 [ComVisible (false)]
797                 public virtual extern bool ReflectionOnly {
798                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
799                         get;
800                 }
801 #endif
802
803 #if !NET_2_1 || MONOTOUCH
804                 // Code Access Security
805
806                 internal void Resolve () 
807                 {
808                         lock (this) {
809                                 // FIXME: As we (currently) delay the resolution until the first CAS
810                                 // Demand it's too late to evaluate the Minimum permission set as a 
811                                 // condition to load the assembly into the AppDomain
812                                 LoadAssemblyPermissions ();
813                                 Evidence e = new Evidence (UnprotectedGetEvidence ()); // we need a copy to add PRE
814                                 e.AddHost (new PermissionRequestEvidence (_minimum, _optional, _refuse));
815                                 _granted = SecurityManager.ResolvePolicy (e,
816                                         _minimum, _optional, _refuse, out _denied);
817                         }
818                 }
819
820                 internal PermissionSet GrantedPermissionSet {
821                         get {
822                                 if (_granted == null) {
823                                         if (SecurityManager.ResolvingPolicyLevel != null) {
824                                                 if (SecurityManager.ResolvingPolicyLevel.IsFullTrustAssembly (this))
825                                                         return DefaultPolicies.FullTrust;
826                                                 else
827                                                         return null; // we can't resolve during resolution
828                                         }
829                                         Resolve ();
830                                 }
831                                 return _granted;
832                         }
833                 }
834
835                 internal PermissionSet DeniedPermissionSet {
836                         get {
837                                 // yes we look for granted, as denied may be null
838                                 if (_granted == null) {
839                                         if (SecurityManager.ResolvingPolicyLevel != null) {
840                                                 if (SecurityManager.ResolvingPolicyLevel.IsFullTrustAssembly (this))
841                                                         return null;
842                                                 else
843                                                         return DefaultPolicies.FullTrust; // deny unrestricted
844                                         }
845                                         Resolve ();
846                                 }
847                                 return _denied;
848                         }
849                 }
850
851                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
852                 extern internal static bool LoadPermissions (Assembly a, 
853                         ref IntPtr minimum, ref int minLength,
854                         ref IntPtr optional, ref int optLength,
855                         ref IntPtr refused, ref int refLength);
856
857                 // Support for SecurityAction.RequestMinimum, RequestOptional and RequestRefuse
858                 private void LoadAssemblyPermissions ()
859                 {
860                         IntPtr minimum = IntPtr.Zero, optional = IntPtr.Zero, refused = IntPtr.Zero;
861                         int minLength = 0, optLength = 0, refLength = 0;
862                         if (LoadPermissions (this, ref minimum, ref minLength, ref optional,
863                                 ref optLength, ref refused, ref refLength)) {
864
865                                 // Note: no need to cache these permission sets as they will only be created once
866                                 // at assembly resolution time.
867                                 if (minLength > 0) {
868                                         byte[] data = new byte [minLength];
869                                         Marshal.Copy (minimum, data, 0, minLength);
870                                         _minimum = SecurityManager.Decode (data);
871                                 }
872                                 if (optLength > 0) {
873                                         byte[] data = new byte [optLength];
874                                         Marshal.Copy (optional, data, 0, optLength);
875                                         _optional = SecurityManager.Decode (data);
876                                 }
877                                 if (refLength > 0) {
878                                         byte[] data = new byte [refLength];
879                                         Marshal.Copy (refused, data, 0, refLength);
880                                         _refuse = SecurityManager.Decode (data);
881                                 }
882                         }
883                 }
884 #endif
885         }
886 }