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