svn path=/branches/mono-1-1-9/mcs/; revision=50439
[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                 
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 #if ONLY_1_1
201                 public new Type GetType ()
202                 {
203                         return base.GetType ();
204                 }
205 #endif
206
207                 public virtual bool IsDefined (Type attributeType, bool inherit)
208                 {
209                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
210                 }
211
212                 public virtual object [] GetCustomAttributes (bool inherit)
213                 {
214                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
215                 }
216
217                 public virtual object [] GetCustomAttributes (Type attributeType, bool inherit)
218                 {
219                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
220                 }
221
222                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
223                 private extern object GetFilesInternal (String name, bool getResourceModules);
224
225                 public virtual FileStream[] GetFiles ()
226                 {
227                         return GetFiles (false);
228                 }
229
230                 public virtual FileStream [] GetFiles (bool getResourceModules)
231                 {
232                         string[] names = (string[]) GetFilesInternal (null, getResourceModules);
233                         if (names == null)
234                                 return new FileStream [0];
235
236                         FileStream[] res = new FileStream [names.Length];
237                         for (int i = 0; i < names.Length; ++i)
238                                 res [i] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
239                         return res;
240                 }
241
242                 public virtual FileStream GetFile (String name)
243                 {
244                         if (name == null)
245                                 throw new ArgumentNullException ("name");
246                         if (name.Length == 0)
247                                 throw new ArgumentException ("name");
248
249                         string filename = (string)GetFilesInternal (name, true);
250                         if (filename != null)
251                                 return new FileStream (filename, FileMode.Open, FileAccess.Read);
252                         else
253                                 return null;
254                 }
255
256                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
257                 internal extern IntPtr GetManifestResourceInternal (String name, out int size, out Module module);
258
259                 public virtual Stream GetManifestResourceStream (String name)
260                 {
261                         if (name == null)
262                                 throw new ArgumentNullException ("name");
263                         if (name == "")
264                                 throw new ArgumentException ("name cannot have zero length.");
265
266                         ManifestResourceInfo info = GetManifestResourceInfo (name);
267                         if (info == null)
268                                 return null;
269
270                         if (info.ReferencedAssembly != null)
271                                 return info.ReferencedAssembly.GetManifestResourceStream (name);
272                         if ((info.FileName != null) && (info.ResourceLocation == 0)) {
273                                 string filename = Path.Combine (Path.GetDirectoryName (Location),
274                                                                                         info.FileName);
275                                 return new FileStream (filename, FileMode.Open, FileAccess.Read);
276                         }
277
278                         int size;
279                         Module module;
280                         IntPtr data = GetManifestResourceInternal (name, out size, out module);
281                         if (data == (IntPtr) 0)
282                                 return null;
283                         else {
284                                 IntPtrStream stream = new IntPtrStream (data, size);
285                                 /* 
286                                  * The returned pointer points inside metadata, so
287                                  * we have to increase the refcount of the module, and decrease
288                                  * it when the stream is finalized.
289                                  */
290                                 stream.Closed += new EventHandler (new ResourceCloseHandler (module).OnClose);
291                                 return stream;
292                         }
293                 }
294
295                 public virtual Stream GetManifestResourceStream (Type type, String name)
296                 {
297                         string ns;
298                         if (type != null)
299                                 ns = type.Namespace;
300                         else 
301                                 ns = null;
302
303                         if ((ns == null) || (ns == ""))
304                                 return GetManifestResourceStream (name);
305                         else
306                                 return GetManifestResourceStream (ns + "." + name);
307                 }
308
309                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
310                 private extern Type[] GetTypes (bool exportedOnly);
311                 
312                 public virtual Type[] GetTypes ()
313                 {
314                         return GetTypes (false);
315                 }
316
317                 public virtual Type[] GetExportedTypes ()
318                 {
319                         return GetTypes (true);
320                 }
321
322                 public virtual Type GetType (String name, Boolean throwOnError)
323                 {
324                         return GetType (name, throwOnError, false);
325                 }
326
327                 public virtual Type GetType (String name) {
328                         return GetType (name, false, false);
329                 }
330
331                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
332                 internal extern Type InternalGetType (Module module, String name, Boolean throwOnError, Boolean ignoreCase);
333
334                 public Type GetType (string name, bool throwOnError, bool ignoreCase)
335                 {
336                         if (name == null)
337                                 throw new ArgumentNullException (name);
338
339                         return InternalGetType (null, name, throwOnError, ignoreCase);
340                 }
341
342                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
343                 internal extern static void InternalGetAssemblyName (string assemblyFile, AssemblyName aname);
344                 
345                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
346                 static extern void FillName (Assembly ass, AssemblyName aname);
347
348                 [MonoTODO ("true == not supported")]
349                 public virtual AssemblyName GetName (Boolean copiedName)
350                 {
351                         // CodeBase, which is restricted, will be copied into the AssemblyName object so...
352                         if (SecurityManager.SecurityEnabled) {
353                                 GetCodeBase (); // this will ensure the Demand is made
354                         }
355                         return UnprotectedGetName ();
356                 }
357
358                 public virtual AssemblyName GetName ()
359                 {
360                         return GetName (false);
361                 }
362
363                 // the security runtime requires access to the assemblyname (e.g. to get the strongname)
364                 internal virtual AssemblyName UnprotectedGetName ()
365                 {
366                         AssemblyName aname = new AssemblyName ();
367                         FillName (this, aname);
368                         return aname;
369                 }
370
371                 public override string ToString ()
372                 {
373                         // note: ToString work without requiring CodeBase (so no checks are needed)
374                         AssemblyName aname = new AssemblyName ();
375                         FillName (this, aname);
376                         return aname.ToString ();
377                 }
378
379                 public static String CreateQualifiedName (String assemblyName, String typeName) 
380                 {
381                         return typeName + ", " + assemblyName;
382                 }
383
384                 public static Assembly GetAssembly (Type type)
385                 {
386                         if (type != null)
387                                 return type.Assembly;
388                         throw new ArgumentNullException ("type");
389                 }
390
391
392                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
393                 public static extern Assembly GetEntryAssembly();
394
395                 public Assembly GetSatelliteAssembly (CultureInfo culture)
396                 {
397                         return GetSatelliteAssembly (culture, null);
398                 }
399
400                 public Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
401                 {
402                         if (culture == null)
403                                 throw new ArgumentException ("culture");
404
405                         AssemblyName aname = GetName (true);
406                         if (version != null)
407                                 aname.Version = version;
408
409                         aname.CultureInfo = culture;
410                         aname.Name = aname.Name + ".resources";
411                         return Load (aname);
412                 }
413                 
414                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
415                 private extern static Assembly LoadFrom (String assemblyFile, bool refonly);
416
417                 public static Assembly LoadFrom (String assemblyFile)
418                 {
419                         return LoadFrom (assemblyFile, false);
420                 }
421
422                 public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence)
423                 {
424                         Assembly a = LoadFrom (assemblyFile, false);
425                         if ((a != null) && (securityEvidence != null)) {
426                                 // merge evidence (i.e. replace defaults with provided evidences)
427                                 a.Evidence.Merge (securityEvidence);
428                         }
429                         return a;
430                 }
431
432 #if NET_1_1
433
434                 [MonoTODO]
435                 public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
436                 {
437                         if (assemblyFile == null)
438                                 throw new ArgumentNullException ("assemblyFile");
439                         if (assemblyFile == String.Empty)
440                                 throw new ArgumentException ("Name can't be the empty string", "assemblyFile");
441                         throw new NotImplementedException ();
442                 }
443
444                 [MonoTODO]
445                 public static Assembly LoadFile (String path, Evidence securityEvidence) {
446                         if (path == null)
447                                 throw new ArgumentNullException ("path");
448                         if (path == String.Empty)
449                                 throw new ArgumentException ("Path can't be empty", "path");
450                         // FIXME: Make this do the right thing
451                         return LoadFrom (path, securityEvidence);
452                 }
453
454                 public static Assembly LoadFile (String path) {
455                         return LoadFile (path, null);
456                 }
457 #endif
458
459                 public static Assembly Load (String assemblyString)
460                 {
461                         return AppDomain.CurrentDomain.Load (assemblyString);
462                 }
463                 
464                 public static Assembly Load (String assemblyString, Evidence assemblySecurity)
465                 {
466                         return AppDomain.CurrentDomain.Load (assemblyString, assemblySecurity);
467                 }
468
469                 public static Assembly Load (AssemblyName assemblyRef)
470                 {
471                         return AppDomain.CurrentDomain.Load (assemblyRef);
472                 }
473
474                 public static Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
475                 {
476                         return AppDomain.CurrentDomain.Load (assemblyRef, assemblySecurity);
477                 }
478
479                 public static Assembly Load (Byte[] rawAssembly)
480                 {
481                         return AppDomain.CurrentDomain.Load (rawAssembly);
482                 }
483
484                 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore)
485                 {
486                         return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
487                 }
488
489                 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore,
490                                              Evidence securityEvidence)
491                 {
492                         return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore, securityEvidence);
493                 }
494
495 #if NET_2_0
496                 public static Assembly ReflectionOnlyLoad (byte[] rawAssembly)
497                 {
498                         return AppDomain.CurrentDomain.Load (rawAssembly, null, null, true);
499                 }
500
501                 public static Assembly ReflectionOnlyLoad (string assemblyName) 
502                 {
503                         return AppDomain.CurrentDomain.Load (assemblyName, null, true);
504                 }
505
506                 public static Assembly ReflectionOnlyLoadFrom (string assemblyFile) 
507                 {
508                         if (assemblyFile == null)
509                                 throw new ArgumentNullException ("assemblyFile");
510                         
511                         return LoadFrom (assemblyFile, true);
512                 }
513 #endif
514
515 #if NET_2_0
516                 [Obsolete ("")]
517 #endif
518                 public static Assembly LoadWithPartialName (string partialName)
519                 {
520                         return LoadWithPartialName (partialName, null);
521                 }
522
523                 [MonoTODO]
524                 public Module LoadModule (string moduleName, byte [] rawModule)
525                 {
526                         throw new NotImplementedException ();
527                 }
528
529                 [MonoTODO]
530                 public Module LoadModule (string moduleName, byte [] rawModule, byte [] rawSymbolStore)
531                 {
532                         throw new NotImplementedException ();
533                 }
534
535                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
536                 private static extern Assembly load_with_partial_name (string name, Evidence e);
537
538 #if NET_2_0
539                 [Obsolete ("")]
540 #endif
541                 public static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence)
542                 {
543                         return LoadWithPartialName (partialName, securityEvidence, true);
544                 }
545
546                 /**
547                  * LAMESPEC: It is possible for this method to throw exceptions IF the name supplied
548                  * is a valid gac name and contains filesystem entry charachters at the end of the name
549                  * ie System/// will throw an exception. However ////System will not as that is canocolized
550                  * out of the name.
551                  */
552
553                 // FIXME: LoadWithPartialName must look cache (no CAS) or read from disk (CAS)
554                 internal static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence, bool oldBehavior)
555                 {
556                         if (!oldBehavior)
557                                 throw new NotImplementedException ();
558
559                         if (partialName == null)
560                                 throw new NullReferenceException ();
561
562                         return load_with_partial_name (partialName, securityEvidence);
563                 }
564
565                 public Object CreateInstance (String typeName) 
566                 {
567                         return CreateInstance (typeName, false);
568                 }
569
570                 public Object CreateInstance (String typeName, Boolean ignoreCase)
571                 {
572                         Type t = GetType (typeName, false, ignoreCase);
573                         if (t == null)
574                                 return null;
575
576                         try {
577                                 return Activator.CreateInstance (t);
578                         } catch (InvalidOperationException) {
579                                 throw new ArgumentException ("It is illegal to invoke a method on a Type loaded via ReflectionOnly methods.");
580                         }
581                 }
582
583                 public Object CreateInstance (String typeName, Boolean ignoreCase,
584                                               BindingFlags bindingAttr, Binder binder,
585                                               Object[] args, CultureInfo culture,
586                                               Object[] activationAttributes)
587                 {
588                         Type t = GetType (typeName, false, ignoreCase);
589                         if (t == null)
590                                 return null;
591
592                         try {
593                                 return Activator.CreateInstance (t, bindingAttr, binder, args, culture, activationAttributes);
594                         } catch (InvalidOperationException) {
595                                 throw new ArgumentException ("It is illegal to invoke a method on a Type loaded via ReflectionOnly methods.");
596                         }
597                 }
598
599                 public Module[] GetLoadedModules ()
600                 {
601                         return GetLoadedModules (false);
602                 }
603
604                 [MonoTODO]
605                 public Module[] GetLoadedModules (bool getResourceModules)
606                 {
607                         // Currently, the two sets of modules are equal
608                         return GetModules (getResourceModules);
609                 }
610
611                 public Module[] GetModules ()
612                 {
613                         return GetModules (false);
614                 }
615
616                 public Module GetModule (String name)
617                 {
618                         if (name == null)
619                                 throw new ArgumentNullException ("name");
620                         if (name == "")
621                                 throw new ArgumentException ("Name can't be empty");
622
623                         Module[] modules = GetModules (true);
624                         foreach (Module module in modules) {
625                                 if (module.ScopeName == name)
626                                         return module;
627                         }
628
629                         return null;
630                 }
631
632                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
633                 internal extern Module[] GetModulesInternal ();
634
635                 public Module[] GetModules (bool getResourceModules) {
636                         Module[] modules = GetModulesInternal ();
637
638                         if (!getResourceModules) {
639                                 ArrayList result = new ArrayList (modules.Length);
640                                 foreach (Module m in modules)
641                                         if (!m.IsResource ())
642                                                 result.Add (m);
643                                 return (Module[])result.ToArray (typeof (Module));
644                         }
645                         else
646                                 return modules;
647                 }
648
649                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
650                 internal extern string[] GetNamespaces ();
651                 
652                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
653                 public extern virtual String[] GetManifestResourceNames ();
654
655                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
656                 public extern static Assembly GetExecutingAssembly ();
657
658                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
659                 public extern static Assembly GetCallingAssembly ();
660
661                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
662                 public extern AssemblyName[] GetReferencedAssemblies ();
663
664                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
665                 private extern bool GetManifestResourceInfoInternal (String name, ManifestResourceInfo info);
666
667                 public virtual ManifestResourceInfo GetManifestResourceInfo (String resourceName)
668                 {
669                         if (resourceName == null)
670                                 throw new ArgumentNullException ("resourceName");
671                         if (resourceName == "")
672                                 throw new ArgumentException ("String cannot have zero length.");
673                         ManifestResourceInfo result = new ManifestResourceInfo ();
674                         bool found = GetManifestResourceInfoInternal (resourceName, result);
675                         if (found)
676                                 return result;
677                         else
678                                 return null;
679                 }
680
681                 private class ResourceCloseHandler {
682
683                         Module module;
684
685                         public ResourceCloseHandler (Module module) {
686                                 this.module = module;
687                         }
688
689                         public void OnClose (object sender, EventArgs e) {
690                                 // The module dtor will take care of things
691                                 module = null;
692                         }
693                 }
694
695                 //
696                 // The following functions are only for the Mono Debugger.
697                 //
698
699                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
700                 internal static extern MethodBase MonoDebugger_GetMethod (Assembly assembly, int token);
701
702                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
703                 internal static extern int MonoDebugger_GetMethodToken (MethodBase method);
704
705                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
706                 internal static extern Type MonoDebugger_GetLocalTypeFromSignature (Assembly assembly, byte[] signature);
707
708                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
709                 internal static extern Type MonoDebugger_GetType (Assembly assembly, int token);
710
711                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
712                 internal static extern string MonoDebugger_CheckRuntimeVersion (string filename);
713
714                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
715                 internal static extern string MonoDebugger_GetMethodIndex (MethodBase method);
716
717                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
718                 internal static extern Type MonoDebugger_MakeArrayType (Type type, int rank);
719
720                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
721                 internal static extern int MonoDebugger_GetTypeToken (Type type);
722
723 #if NET_2_0
724                 [MonoTODO]
725                 [ComVisible (false)]
726                 public long HostContext {
727                         get { return 0; }
728                 }
729
730                 [ComVisible (false)]
731                 [Obsolete ("Please use Assembly.ManifestModule.GetPEKind() instead - this will be removed before Whidbey ships.")]
732                 public ImageFileMachine ImageFileMachine {
733                         get {
734                                 ImageFileMachine machine;
735                                 PortableExecutableKinds kind;
736                                 ModuleHandle handle = ManifestModule.ModuleHandle;
737                                 handle.GetPEKind (out kind, out machine);
738                                 return machine;
739                         }
740                 }
741
742                 [ComVisible (false)]
743                 public extern Module ManifestModule {
744                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
745                         get;
746                 }
747
748                 [ComVisible (false)]
749                 [Obsolete ("This method has been deprecated and will be removed before v2.0 RTM is released.")]
750                 public extern int MetadataToken {
751                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
752                         get;
753                 }
754
755                 [ComVisible (false)]
756                 [Obsolete ("Please use Assembly.ManifestModule.GetPEKind() instead - this will be removed before Whidbey ships.")]
757                 public PortableExecutableKinds PortableExecutableKinds {
758                         get {
759                                 ImageFileMachine machine;
760                                 PortableExecutableKinds kind;
761                                 ModuleHandle handle = ManifestModule.ModuleHandle;
762                                 handle.GetPEKind (out kind, out machine);
763                                 return kind;
764                         }
765                 }
766
767                 [ComVisible (false)]
768                 public virtual extern bool ReflectionOnly {
769                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
770                         get;
771                 }
772 #endif
773
774                 // Code Access Security
775
776                 internal void Resolve () 
777                 {
778                         lock (this) {
779                                 // FIXME: As we (currently) delay the resolution until the first CAS
780                                 // Demand it's too late to evaluate the Minimum permission set as a 
781                                 // condition to load the assembly into the AppDomain
782                                 LoadAssemblyPermissions ();
783                                 Evidence e = new Evidence (UnprotectedGetEvidence ()); // we need a copy to add PRE
784                                 e.AddHost (new PermissionRequestEvidence (_minimum, _optional, _refuse));
785                                 _granted = SecurityManager.ResolvePolicy (e,
786                                         _minimum, _optional, _refuse, out _denied);
787                         }
788                 }
789
790                 internal PermissionSet GrantedPermissionSet {
791                         get {
792                                 if (_granted == null) {
793                                         if (SecurityManager.ResolvingPolicyLevel != null) {
794                                                 if (SecurityManager.ResolvingPolicyLevel.IsFullTrustAssembly (this))
795                                                         return DefaultPolicies.FullTrust;
796                                                 else
797                                                         return null; // we can't resolve during resolution
798                                         }
799                                         Resolve ();
800                                 }
801                                 return _granted;
802                         }
803                 }
804
805                 internal PermissionSet DeniedPermissionSet {
806                         get {
807                                 // yes we look for granted, as denied may be null
808                                 if (_granted == null) {
809                                         if (SecurityManager.ResolvingPolicyLevel != null) {
810                                                 if (SecurityManager.ResolvingPolicyLevel.IsFullTrustAssembly (this))
811                                                         return null;
812                                                 else
813                                                         return DefaultPolicies.FullTrust; // deny unrestricted
814                                         }
815                                         Resolve ();
816                                 }
817                                 return _denied;
818                         }
819                 }
820
821                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
822                 extern internal static bool LoadPermissions (Assembly a, 
823                         ref IntPtr minimum, ref int minLength,
824                         ref IntPtr optional, ref int optLength,
825                         ref IntPtr refused, ref int refLength);
826
827                 // Support for SecurityAction.RequestMinimum, RequestOptional and RequestRefuse
828                 private void LoadAssemblyPermissions ()
829                 {
830                         IntPtr minimum = IntPtr.Zero, optional = IntPtr.Zero, refused = IntPtr.Zero;
831                         int minLength = 0, optLength = 0, refLength = 0;
832                         if (LoadPermissions (this, ref minimum, ref minLength, ref optional,
833                                 ref optLength, ref refused, ref refLength)) {
834
835                                 // Note: no need to cache these permission sets as they will only be created once
836                                 // at assembly resolution time.
837                                 if (minLength > 0) {
838                                         byte[] data = new byte [minLength];
839                                         Marshal.Copy (minimum, data, 0, minLength);
840                                         _minimum = SecurityManager.Decode (data);
841                                 }
842                                 if (optLength > 0) {
843                                         byte[] data = new byte [optLength];
844                                         Marshal.Copy (optional, data, 0, optLength);
845                                         _optional = SecurityManager.Decode (data);
846                                 }
847                                 if (refLength > 0) {
848                                         byte[] data = new byte [refLength];
849                                         Marshal.Copy (refused, data, 0, refLength);
850                                         _refuse = SecurityManager.Decode (data);
851                                 }
852                         }
853                 }
854         }
855 }