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