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