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