99acaf3947634382c14f987852a8f4fb69d0cd3e
[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;
35 using System.Reflection.Emit;
36 using System.IO;
37 using System.Globalization;
38 using System.Runtime.CompilerServices;
39 using System.Runtime.InteropServices;
40 using System.Collections;
41 using System.Collections.Generic;
42 using System.Configuration.Assemblies;
43
44 using Mono.Security;
45
46 namespace System.Reflection {
47
48 #pragma warning disable 659 // overrides Equals but not GetHashCode
49
50         [ComVisible (true)]
51         [ComDefaultInterfaceAttribute (typeof (_Assembly))]
52         [Serializable]
53         [ClassInterface(ClassInterfaceType.None)]
54 #if NET_2_1
55         public partial class Assembly : ICustomAttributeProvider, _Assembly {
56 #elif NET_4_0
57         public abstract class Assembly : ICustomAttributeProvider, _Assembly, IEvidenceFactory, ISerializable {
58 #else
59         public partial class Assembly : ICustomAttributeProvider, _Assembly, IEvidenceFactory, ISerializable {
60 #endif
61                 internal class ResolveEventHolder {
62                         public event ModuleResolveEventHandler ModuleResolve;
63                 }
64
65                 // Note: changes to fields must be reflected in _MonoReflectionAssembly struct (object-internals.h)
66 #pragma warning disable 649
67                 private IntPtr _mono_assembly;
68 #pragma warning restore 649
69
70                 private ResolveEventHolder resolve_event_holder;
71                 private Evidence _evidence;
72                 internal PermissionSet _minimum;        // for SecurityAction.RequestMinimum
73                 internal PermissionSet _optional;       // for SecurityAction.RequestOptional
74                 internal PermissionSet _refuse;         // for SecurityAction.RequestRefuse
75                 private PermissionSet _granted;         // for the resolved assembly granted permissions
76                 private PermissionSet _denied;          // for the resolved assembly denied permissions
77                 private bool fromByteArray;
78                 private string assemblyName;
79
80 #if NET_4_0
81                 protected
82 #else
83                 internal
84 #endif
85                 Assembly () 
86                 {
87                         resolve_event_holder = new ResolveEventHolder ();
88                 }
89
90                 //
91                 // We can't store the event directly in this class, since the
92                 // compiler would silently insert the fields before _mono_assembly
93                 //
94                 public event ModuleResolveEventHandler ModuleResolve {
95                         [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
96                         add {
97                                 resolve_event_holder.ModuleResolve += value;
98                         }
99                         [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
100                         remove {
101                                 resolve_event_holder.ModuleResolve -= value;
102                         }
103                 }
104
105                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
106                 private extern string get_code_base (bool escaped);
107
108                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
109                 private extern string get_fullname ();
110
111                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
112                 private extern string get_location ();
113
114                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
115                 private extern string InternalImageRuntimeVersion ();
116
117                 // SECURITY: this should be the only caller to icall get_code_base
118                 private string GetCodeBase (bool escaped)
119                 {
120                         string cb = get_code_base (escaped);
121 #if !NET_2_1
122                         if (SecurityManager.SecurityEnabled) {
123                                 // we cannot divulge local file informations
124                                 if (String.Compare ("FILE://", 0, cb, 0, 7, true, CultureInfo.InvariantCulture) == 0) {
125                                         string file = cb.Substring (7);
126                                         new FileIOPermission (FileIOPermissionAccess.PathDiscovery, file).Demand ();
127                                 }
128                         }
129 #endif
130                         return cb;
131                 }
132
133                 public virtual string CodeBase {
134                         get { return GetCodeBase (false); }
135                 }
136
137                 public virtual string EscapedCodeBase {
138                         get { return GetCodeBase (true); }
139                 }
140
141                 public virtual string FullName {
142                         get {
143                                 //
144                                 // FIXME: This is wrong, but it gets us going
145                                 // in the compiler for now
146                                 //
147                                 return ToString ();
148                         }
149                 }
150
151                 public virtual extern MethodInfo EntryPoint {
152                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
153                         get;
154                 }
155 #if !MOONLIGHT
156                 public virtual Evidence Evidence {
157                         [SecurityPermission (SecurityAction.Demand, ControlEvidence = true)]
158                         get { return UnprotectedGetEvidence (); }
159                 }
160
161                 // note: the security runtime requires evidences but may be unable to do so...
162                 internal Evidence UnprotectedGetEvidence ()
163                 {
164                         // if the host (runtime) hasn't provided it's own evidence...
165                         if (_evidence == null) {
166                                 // ... we will provide our own
167                                 lock (this) {
168                                         _evidence = Evidence.GetDefaultHostEvidence (this);
169                                 }
170                         }
171                         return _evidence;
172                 }
173
174                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
175                 internal extern bool get_global_assembly_cache ();
176
177 #endif
178                 internal bool FromByteArray {
179                         set { fromByteArray = value; }
180                 }
181
182                 public virtual String Location {
183                         get {
184                                 if (fromByteArray)
185                                         return String.Empty;
186
187                                 string loc = get_location ();
188 #if !NET_2_1
189                                 if ((loc != String.Empty) && SecurityManager.SecurityEnabled) {
190                                         // we cannot divulge local file informations
191                                         new FileIOPermission (FileIOPermissionAccess.PathDiscovery, loc).Demand ();
192                                 }
193 #endif
194                                 return loc;
195                         }
196                 }
197
198                 [ComVisible (false)]
199                 public virtual string ImageRuntimeVersion {
200                         get {
201                                 return InternalImageRuntimeVersion ();
202                         }
203                 }
204
205                 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
206                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
207                 {
208                         if (info == null)
209                                 throw new ArgumentNullException ("info");
210
211                         UnitySerializationHolder.GetAssemblyData (this, info, context);
212                 }
213
214                 public virtual bool IsDefined (Type attributeType, bool inherit)
215                 {
216                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
217                 }
218
219                 public virtual object [] GetCustomAttributes (bool inherit)
220                 {
221                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
222                 }
223
224                 public virtual object [] GetCustomAttributes (Type attributeType, bool inherit)
225                 {
226                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
227                 }
228
229                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
230                 private extern object GetFilesInternal (String name, bool getResourceModules);
231
232                 public virtual FileStream[] GetFiles ()
233                 {
234                         return GetFiles (false);
235                 }
236
237                 public virtual FileStream [] GetFiles (bool getResourceModules)
238                 {
239                         string[] names = (string[]) GetFilesInternal (null, getResourceModules);
240                         if (names == null)
241                                 return new FileStream [0];
242
243                         string location = Location;
244
245                         FileStream[] res;
246                         if (location != String.Empty) {
247                                 res = new FileStream [names.Length + 1];
248                                 res [0] = new FileStream (location, FileMode.Open, FileAccess.Read);
249                                 for (int i = 0; i < names.Length; ++i)
250                                         res [i + 1] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
251                         } else {
252                                 res = new FileStream [names.Length];
253                                 for (int i = 0; i < names.Length; ++i)
254                                         res [i] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
255                         }
256                         return res;
257                 }
258
259                 public virtual FileStream GetFile (String name)
260                 {
261                         if (name == null)
262                                 throw new ArgumentNullException (null, "Name cannot be null.");
263                         if (name.Length == 0)
264                                 throw new ArgumentException ("Empty name is not valid");
265
266                         string filename = (string)GetFilesInternal (name, true);
267                         if (filename != null)
268                                 return new FileStream (filename, FileMode.Open, FileAccess.Read);
269                         else
270                                 return null;
271                 }
272
273                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
274                 internal extern IntPtr GetManifestResourceInternal (String name, out int size, out Module module);
275
276                 public virtual Stream GetManifestResourceStream (String name)
277                 {
278                         if (name == null)
279                                 throw new ArgumentNullException ("name");
280                         if (name.Length == 0)
281                                 throw new ArgumentException ("String cannot have zero length.",
282                                         "name");
283
284                         ManifestResourceInfo info = GetManifestResourceInfo (name);
285                         if (info == null)
286                                 return null;
287
288                         if (info.ReferencedAssembly != null)
289                                 return info.ReferencedAssembly.GetManifestResourceStream (name);
290                         if ((info.FileName != null) && (info.ResourceLocation == 0)) {
291                                 if (fromByteArray)
292                                         throw new FileNotFoundException (info.FileName);
293
294                                 string location = Path.GetDirectoryName (Location);
295                                 string filename = Path.Combine (location, info.FileName);
296 #if MOONLIGHT
297                                 // we don't control the content of 'info.FileName' so we want to make sure we keep to ourselves
298                                 filename = Path.GetFullPath (filename);
299                                 if (!filename.StartsWith (location))
300                                         throw new SecurityException ("non-rooted access to manifest resource");
301 #endif
302                                 return new FileStream (filename, FileMode.Open, FileAccess.Read);
303                         }
304
305                         int size;
306                         Module module;
307                         IntPtr data = GetManifestResourceInternal (name, out size, out module);
308                         if (data == (IntPtr) 0)
309                                 return null;
310                         else {
311                                 UnmanagedMemoryStream stream;
312                                 unsafe {
313                                         stream = new UnmanagedMemoryStream ((byte*) data, size);
314                                 }
315                                 /* 
316                                  * The returned pointer points inside metadata, so
317                                  * we have to increase the refcount of the module, and decrease
318                                  * it when the stream is finalized.
319                                  */
320                                 stream.Closed += new EventHandler (new ResourceCloseHandler (module).OnClose);
321                                 return stream;
322                         }
323                 }
324
325                 public virtual Stream GetManifestResourceStream (Type type, String name)
326                 {
327                         string ns;
328                         if (type != null) {
329                                 ns = type.Namespace;
330                         } else {
331                                 if (name == null)
332                                         throw new ArgumentNullException ("type");
333                                 ns = null;
334                         }
335
336                         if (ns == null || ns.Length == 0)
337                                 return GetManifestResourceStream (name);
338                         else
339                                 return GetManifestResourceStream (ns + "." + name);
340                 }
341
342                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
343                 internal virtual extern Type[] GetTypes (bool exportedOnly);
344                 
345                 public virtual Type[] GetTypes ()
346                 {
347                         return GetTypes (false);
348                 }
349
350                 public virtual Type[] GetExportedTypes ()
351                 {
352                         return GetTypes (true);
353                 }
354
355                 public virtual Type GetType (String name, Boolean throwOnError)
356                 {
357                         return GetType (name, throwOnError, false);
358                 }
359
360                 public virtual Type GetType (String name) {
361                         return GetType (name, false, false);
362                 }
363
364                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
365                 internal extern Type InternalGetType (Module module, String name, Boolean throwOnError, Boolean ignoreCase);
366
367                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
368                 internal extern static void InternalGetAssemblyName (string assemblyFile, AssemblyName aname);
369
370                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
371                 static extern void FillName (Assembly ass, AssemblyName aname);
372
373                 [MonoTODO ("copiedName == true is not supported")]
374                 public virtual AssemblyName GetName (Boolean copiedName)
375                 {
376                         // CodeBase, which is restricted, will be copied into the AssemblyName object so...
377                         if (SecurityManager.SecurityEnabled) {
378                                 GetCodeBase (true); // this will ensure the Demand is made
379                         }
380                         return UnprotectedGetName ();
381                 }
382
383                 public virtual AssemblyName GetName ()
384                 {
385                         return GetName (false);
386                 }
387
388                 // the security runtime requires access to the assemblyname (e.g. to get the strongname)
389                 internal virtual AssemblyName UnprotectedGetName ()
390                 {
391                         AssemblyName aname = new AssemblyName ();
392                         FillName (this, aname);
393                         return aname;
394                 }
395
396                 public override string ToString ()
397                 {
398                         // note: ToString work without requiring CodeBase (so no checks are needed)
399
400                         if (assemblyName != null)
401                                 return assemblyName;
402
403                         assemblyName = get_fullname ();
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                 internal Assembly GetSatelliteAssemblyNoThrow (CultureInfo culture, Version version)
424                 {
425                         return GetSatelliteAssembly (culture, version, false);
426                 }
427
428                 internal Assembly GetSatelliteAssembly (CultureInfo culture, Version version, bool throwOnError)
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                         Assembly assembly;
440
441                         try {
442                                 assembly = AppDomain.CurrentDomain.LoadSatellite (aname, false);
443                                 if (assembly != null)
444                                         return assembly;
445                         } catch (FileNotFoundException) {
446                                 assembly = null;
447                                 // ignore
448                         }
449
450                         // Try the assembly directory
451                         string location = Path.GetDirectoryName (Location);
452                         string fullName = Path.Combine (location, Path.Combine (culture.Name, aname.Name + ".dll"));
453 #if MOONLIGHT
454                         // it's unlikely that culture.Name or aname.Name could contain stuff like ".." but...
455                         fullName = Path.GetFullPath (fullName);
456                         if (!fullName.StartsWith (location)) {
457                                 if (throwOnError)
458                                         throw new SecurityException ("non-rooted access to satellite assembly");
459                                 return null;
460                         }
461 #endif
462                         if (!throwOnError && !File.Exists (fullName))
463                                 return null;
464
465                         return LoadFrom (fullName);
466                 }
467                 
468                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
469                 private extern static Assembly LoadFrom (String assemblyFile, bool refonly);
470
471                 public static Assembly LoadFrom (String assemblyFile)
472                 {
473                         return LoadFrom (assemblyFile, false);
474                 }
475
476                 public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence)
477                 {
478                         Assembly a = LoadFrom (assemblyFile, false);
479 #if !NET_2_1
480                         if ((a != null) && (securityEvidence != null)) {
481                                 // merge evidence (i.e. replace defaults with provided evidences)
482                                 a.Evidence.Merge (securityEvidence);
483                         }
484 #endif
485                         return a;
486                 }
487
488                 [MonoTODO("This overload is not currently implemented")]
489                 // FIXME: What are we missing?
490                 public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
491                 {
492                         if (assemblyFile == null)
493                                 throw new ArgumentNullException ("assemblyFile");
494                         if (assemblyFile == String.Empty)
495                                 throw new ArgumentException ("Name can't be the empty string", "assemblyFile");
496                         throw new NotImplementedException ();
497                 }
498
499                 public static Assembly LoadFile (String path, Evidence securityEvidence)
500                 {
501                         if (path == null)
502                                 throw new ArgumentNullException ("path");
503                         if (path == String.Empty)
504                                 throw new ArgumentException ("Path can't be empty", "path");
505                         // FIXME: Make this do the right thing
506                         return LoadFrom (path, securityEvidence);
507                 }
508
509                 public static Assembly LoadFile (String path)
510                 {
511                         return LoadFile (path, null);
512                 }
513
514                 public static Assembly Load (String assemblyString)
515                 {
516                         return AppDomain.CurrentDomain.Load (assemblyString);
517                 }
518                 
519                 public static Assembly Load (String assemblyString, Evidence assemblySecurity)
520                 {
521                         return AppDomain.CurrentDomain.Load (assemblyString, assemblySecurity);
522                 }
523
524                 public static Assembly Load (AssemblyName assemblyRef)
525                 {
526                         return AppDomain.CurrentDomain.Load (assemblyRef);
527                 }
528
529                 public static Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
530                 {
531                         return AppDomain.CurrentDomain.Load (assemblyRef, assemblySecurity);
532                 }
533
534                 public static Assembly Load (Byte[] rawAssembly)
535                 {
536                         return AppDomain.CurrentDomain.Load (rawAssembly);
537                 }
538
539                 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore)
540                 {
541                         return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
542                 }
543
544                 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore,
545                                              Evidence securityEvidence)
546                 {
547                         return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore, securityEvidence);
548                 }
549
550                 public static Assembly ReflectionOnlyLoad (byte[] rawAssembly)
551                 {
552                         return AppDomain.CurrentDomain.Load (rawAssembly, null, null, true);
553                 }
554
555                 public static Assembly ReflectionOnlyLoad (string assemblyString) 
556                 {
557                         return AppDomain.CurrentDomain.Load (assemblyString, null, true);
558                 }
559
560                 public static Assembly ReflectionOnlyLoadFrom (string assemblyFile) 
561                 {
562                         if (assemblyFile == null)
563                                 throw new ArgumentNullException ("assemblyFile");
564                         
565                         return LoadFrom (assemblyFile, true);
566                 }
567
568                 public static Assembly LoadWithPartialName (string partialName)
569                 {
570                         return LoadWithPartialName (partialName, null);
571                 }
572
573                 [MonoTODO ("Not implemented")]
574                 public Module LoadModule (string moduleName, byte [] rawModule)
575                 {
576                         throw new NotImplementedException ();
577                 }
578
579                 [MonoTODO ("Not implemented")]
580                 public
581 #if NET_4_0
582                 virtual
583 #endif
584                 Module LoadModule (string moduleName, byte [] rawModule, byte [] rawSymbolStore)
585                 {
586                         throw new NotImplementedException ();
587                 }
588
589                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
590                 private static extern Assembly load_with_partial_name (string name, Evidence e);
591
592                 public static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence)
593                 {
594                         return LoadWithPartialName (partialName, securityEvidence, true);
595                 }
596
597                 /**
598                  * LAMESPEC: It is possible for this method to throw exceptions IF the name supplied
599                  * is a valid gac name and contains filesystem entry charachters at the end of the name
600                  * ie System/// will throw an exception. However ////System will not as that is canocolized
601                  * out of the name.
602                  */
603
604                 // FIXME: LoadWithPartialName must look cache (no CAS) or read from disk (CAS)
605                 internal static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence, bool oldBehavior)
606                 {
607                         if (!oldBehavior)
608                                 throw new NotImplementedException ();
609
610                         if (partialName == null)
611                                 throw new NullReferenceException ();
612
613                         return load_with_partial_name (partialName, securityEvidence);
614                 }
615
616                 public Object CreateInstance (String typeName) 
617                 {
618                         return CreateInstance (typeName, false);
619                 }
620
621                 public Object CreateInstance (String typeName, Boolean ignoreCase)
622                 {
623                         Type t = GetType (typeName, false, ignoreCase);
624                         if (t == null)
625                                 return null;
626
627                         try {
628                                 return Activator.CreateInstance (t);
629                         } catch (InvalidOperationException) {
630                                 throw new ArgumentException ("It is illegal to invoke a method on a Type loaded via ReflectionOnly methods.");
631                         }
632                 }
633
634                 public
635 #if NET_4_0
636                 virtual
637 #endif
638                 Object CreateInstance (String typeName, Boolean ignoreCase,
639                                               BindingFlags bindingAttr, Binder binder,
640                                               Object[] args, CultureInfo culture,
641                                               Object[] activationAttributes)
642                 {
643                         Type t = GetType (typeName, false, ignoreCase);
644                         if (t == null)
645                                 return null;
646
647                         try {
648                                 return Activator.CreateInstance (t, bindingAttr, binder, args, culture, activationAttributes);
649                         } catch (InvalidOperationException) {
650                                 throw new ArgumentException ("It is illegal to invoke a method on a Type loaded via ReflectionOnly methods.");
651                         }
652                 }
653
654                 public Module[] GetLoadedModules ()
655                 {
656                         return GetLoadedModules (false);
657                 }
658
659                 public Module[] GetModules ()
660                 {
661                         return GetModules (false);
662                 }
663
664                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
665                 internal virtual extern Module[] GetModulesInternal ();
666
667
668                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
669                 internal extern string[] GetNamespaces ();
670                 
671                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
672                 public extern virtual String[] GetManifestResourceNames ();
673
674                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
675                 public extern static Assembly GetExecutingAssembly ();
676
677                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
678                 public extern static Assembly GetCallingAssembly ();
679
680                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
681                 internal static extern AssemblyName[] GetReferencedAssemblies (Assembly module);
682
683                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
684                 private extern bool GetManifestResourceInfoInternal (String name, ManifestResourceInfo info);
685
686                 public virtual ManifestResourceInfo GetManifestResourceInfo (String resourceName)
687                 {
688                         if (resourceName == null)
689                                 throw new ArgumentNullException ("resourceName");
690                         if (resourceName.Length == 0)
691                                 throw new ArgumentException ("String cannot have zero length.");
692                         ManifestResourceInfo result = new ManifestResourceInfo ();
693                         bool found = GetManifestResourceInfoInternal (resourceName, result);
694                         if (found)
695                                 return result;
696                         else
697                                 return null;
698                 }
699
700                 private class ResourceCloseHandler {
701 #pragma warning disable 169, 414
702                         Module module;
703 #pragma warning restore 169, 414                        
704
705                         public ResourceCloseHandler (Module module) {
706                                 this.module = module;
707                         }
708
709                         public void OnClose (object sender, EventArgs e) {
710                                 // The module dtor will take care of things
711                                 module = null;
712                         }
713                 }
714
715                 //
716                 // The following functions are only for the Mono Debugger.
717                 //
718
719                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
720                 internal static extern int MonoDebugger_GetMethodToken (MethodBase method);
721
722                 [MonoTODO ("Currently it always returns zero")]
723                 [ComVisible (false)]
724                 public
725 #if NET_4_0
726                 virtual
727 #endif
728                 long HostContext {
729                         get { return 0; }
730                 }
731
732
733                 internal virtual Module GetManifestModule () {
734                         return GetManifestModuleInternal ();
735                 }
736
737                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
738                 internal extern Module GetManifestModuleInternal ();
739
740                 [ComVisible (false)]
741                 public virtual extern bool ReflectionOnly {
742                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
743                         get;
744                 }
745
746                 public override bool Equals (object o)
747                 {
748                         if (((object) this) == o)
749                                 return true;
750
751                         if (o == null)
752                                 return false;
753                         
754                         Assembly other = (Assembly) o;
755                         return other._mono_assembly == _mono_assembly;
756                 }
757                 
758 #if NET_4_0
759                 public virtual IList<CustomAttributeData> GetCustomAttributesData () {
760                         return CustomAttributeData.GetCustomAttributes (this);
761                 }
762 #endif
763
764 #if !MOONLIGHT
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 #endif
846
847 #if NET_4_0
848                 static Exception CreateNIE ()
849                 {
850                         return new NotSupportedException ("Derived classes must implement it");
851                 }
852
853                 public virtual Type GetType (string name, bool throwOnError, bool ignoreCase)
854                 {
855                         throw CreateNIE ();
856                 }
857
858                 public virtual Module GetModule (String name)
859                 {
860                         throw CreateNIE ();
861                 }
862
863                 public virtual AssemblyName[] GetReferencedAssemblies ()
864                 {
865                         throw CreateNIE ();
866                 }
867
868                 public virtual Module[] GetModules (bool getResourceModules)
869                 {
870                         throw CreateNIE ();
871                 }
872
873                 public virtual Module[] GetLoadedModules (bool getResourceModules)
874                 {
875                         throw CreateNIE ();
876                 }
877
878                 public virtual Assembly GetSatelliteAssembly (CultureInfo culture)
879                 {
880                         throw CreateNIE ();
881                 }
882
883                 public virtual Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
884                 {
885                         throw CreateNIE ();
886                 }
887
888                 public virtual Module ManifestModule {
889                         get { throw CreateNIE (); }
890                 }
891
892                 public virtual bool GlobalAssemblyCache {
893                         get { throw CreateNIE (); }
894                 }
895 #endif
896         }
897 }
898
899 #pragma warning restore 659