ce11c4a924eec7994b2385f3852e61b51c1a51d3
[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 namespace System.Reflection {
44
45         [Serializable]
46         [ClassInterface(ClassInterfaceType.None)]
47         public class Assembly : System.Reflection.ICustomAttributeProvider,
48                 System.Security.IEvidenceFactory, System.Runtime.Serialization.ISerializable {
49                 internal class ResolveEventHolder {
50                         public event ModuleResolveEventHandler ModuleResolve;
51                 }
52
53                 // Note: changes to fields must be reflected in _MonoReflectionAssembly struct (object-internals.h)
54                 private IntPtr _mono_assembly;
55
56                 private ResolveEventHolder resolve_event_holder;
57                 private Evidence _evidence;
58                 internal PermissionSet _minimum;        // for SecurityAction.RequestMinimum
59                 internal PermissionSet _optional;       // for SecurityAction.RequestOptional
60                 internal PermissionSet _refuse;         // for SecurityAction.RequestRefuse
61                 private PermissionSet _granted;         // for the resolved assembly granted permissions
62                 private PermissionSet _denied;          // for the resolved assembly denied permissions
63                 
64                 internal Assembly () 
65                 {
66                         resolve_event_holder = new ResolveEventHolder ();
67                 }
68
69                 //
70                 // We can't store the event directly in this class, since the
71                 // compiler would silently insert the fields before _mono_assembly
72                 //
73                 public event ModuleResolveEventHandler ModuleResolve {
74                         add {
75                                 resolve_event_holder.ModuleResolve += value;
76                         }
77                         remove {
78                                 resolve_event_holder.ModuleResolve -= value;
79                         }
80                 }
81
82                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
83                 private extern string get_code_base ();
84                 
85                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
86                 private extern string get_location ();
87
88                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
89                 private extern string InternalImageRuntimeVersion ();
90
91                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
92                 private extern bool get_global_assembly_cache ();
93
94                 public virtual string CodeBase {
95                         get {
96                                 return get_code_base ();
97                         }
98                 }
99
100                 internal virtual string CopiedCodeBase {
101                         get {
102                                 return get_code_base ();
103                         }
104                 } 
105
106                 [MonoTODO]
107                 public virtual string EscapedCodeBase {
108                         get {
109                                 //FIXME: escape characters -> Uri
110                                 return get_code_base ();
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                 public extern static Assembly LoadFrom (String assemblyFile);
364
365                 public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence)
366                 {
367                         Assembly a = LoadFrom (assemblyFile);
368                         if ((a != null) && (securityEvidence != null)) {
369                                 // merge evidence (i.e. replace defaults with provided evidences)
370                                 a.Evidence.Merge (securityEvidence);
371                         }
372                         return a;
373                 }
374
375 #if NET_1_1
376
377                 [MonoTODO]
378                 public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
379                 {
380                         if (assemblyFile == null)
381                                 throw new ArgumentNullException ("assemblyFile");
382                         if (assemblyFile == String.Empty)
383                                 throw new ArgumentException ("Name can't be the empty string", "assemblyFile");
384                         throw new NotImplementedException ();
385                 }
386
387                 [MonoTODO]
388                 public static Assembly LoadFile (String path, Evidence securityEvidence) {
389                         if (path == null)
390                                 throw new ArgumentNullException ("path");
391                         if (path == String.Empty)
392                                 throw new ArgumentException ("Path can't be empty", "path");
393                         // FIXME: Make this do the right thing
394                         return LoadFrom (path, securityEvidence);
395                 }
396
397                 public static Assembly LoadFile (String path) {
398                         return LoadFile (path, null);
399                 }
400 #endif
401
402                 public static Assembly Load (String assemblyString)
403                 {
404                         return AppDomain.CurrentDomain.Load (assemblyString);
405                 }
406                 
407                 public static Assembly Load (String assemblyString, Evidence assemblySecurity)
408                 {
409                         return AppDomain.CurrentDomain.Load (assemblyString, assemblySecurity);
410                 }
411
412                 public static Assembly Load (AssemblyName assemblyRef)
413                 {
414                         return AppDomain.CurrentDomain.Load (assemblyRef);
415                 }
416
417                 public static Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
418                 {
419                         return AppDomain.CurrentDomain.Load (assemblyRef, assemblySecurity);
420                 }
421
422                 public static Assembly Load (Byte[] rawAssembly)
423                 {
424                         return AppDomain.CurrentDomain.Load (rawAssembly);
425                 }
426
427                 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore)
428                 {
429                         return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
430                 }
431
432                 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore,
433                                              Evidence securityEvidence)
434                 {
435                         return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore, securityEvidence);
436                 }
437
438 #if NET_2_0
439                 [MonoTODO]
440                 public static Assembly ReflectionOnlyLoad (byte[] rawAssembly)
441                 {
442                         throw new NotImplementedException ();
443                 }
444
445                 [MonoTODO]
446                 public static Assembly ReflectionOnlyLoad (string assemblyString) {
447                         throw new NotImplementedException ();
448                 }
449
450                 [MonoTODO]
451                 public static Assembly ReflectionOnlyLoadFrom (string assemblyFile) {
452                         throw new NotImplementedException ();
453                 }
454 #endif
455
456 #if NET_2_0
457                 [Obsolete ("")]
458 #endif
459                 public static Assembly LoadWithPartialName (string partialName)
460                 {
461                         return LoadWithPartialName (partialName, null);
462                 }
463
464                 [MonoTODO]
465                 public Module LoadModule (string moduleName, byte [] rawModule)
466                 {
467                         throw new NotImplementedException ();
468                 }
469
470                 [MonoTODO]
471                 public Module LoadModule (string moduleName, byte [] rawModule, byte [] rawSymbolStore)
472                 {
473                         throw new NotImplementedException ();
474                 }
475
476                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
477                 private static extern Assembly load_with_partial_name (string name, Evidence e);
478
479 #if NET_2_0
480                 [Obsolete ("")]
481 #endif
482                 public static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence)
483                 {
484                         return LoadWithPartialName (partialName, securityEvidence, true);
485                 }
486
487                 /**
488                  * LAMESPEC: It is possible for this method to throw exceptions IF the name supplied
489                  * is a valid gac name and contains filesystem entry charachters at the end of the name
490                  * ie System/// will throw an exception. However ////System will not as that is canocolized
491                  * out of the name.
492                  */
493 #if NET_2_0
494                 [Obsolete ("")]
495                 [ComVisible (false)]
496                 [MonoTODO]
497                 public
498 #else
499                 internal
500 #endif
501                 static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence, bool oldBehavior)
502                 {
503                         if (!oldBehavior)
504                                 throw new NotImplementedException ();
505
506                         if (partialName == null)
507                                 throw new NullReferenceException ();
508
509                         int ci = partialName.IndexOf (',');
510                         if (ci > 0)
511                                 partialName = partialName.Substring (0, ci);
512
513                         return load_with_partial_name (partialName, securityEvidence);
514                 }
515
516                 public Object CreateInstance (String typeName) 
517                 {
518                         return CreateInstance (typeName, false);
519                 }
520
521                 public Object CreateInstance (String typeName, Boolean ignoreCase)
522                 {
523                         Type t = GetType (typeName, false, ignoreCase);
524                         if (t == null)
525                                 return null;
526
527                         return Activator.CreateInstance (t);
528                 }
529
530                 public Object CreateInstance (String typeName, Boolean ignoreCase,
531                                               BindingFlags bindingAttr, Binder binder,
532                                               Object[] args, CultureInfo culture,
533                                               Object[] activationAttributes)
534                 {
535                         Type t = GetType (typeName, false, ignoreCase);
536                         if (t == null)
537                                 return null;
538
539                         return Activator.CreateInstance (t, bindingAttr, binder, args, culture, activationAttributes);
540                 }
541
542                 public Module[] GetLoadedModules ()
543                 {
544                         return GetLoadedModules (false);
545                 }
546
547                 [MonoTODO]
548                 public Module[] GetLoadedModules (bool getResourceModules)
549                 {
550                         // Currently, the two sets of modules are equal
551                         return GetModules (getResourceModules);
552                 }
553
554                 public Module[] GetModules ()
555                 {
556                         return GetModules (false);
557                 }
558
559                 public Module GetModule (String name)
560                 {
561                         if (name == null)
562                                 throw new ArgumentNullException ("name");
563                         if (name == "")
564                                 throw new ArgumentException ("Name can't be empty");
565
566                         Module[] modules = GetModules (true);
567                         foreach (Module module in modules) {
568                                 if (module.ScopeName == name)
569                                         return module;
570                         }
571
572                         return null;
573                 }
574
575                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
576                 internal extern Module[] GetModulesInternal ();
577
578                 public Module[] GetModules (bool getResourceModules) {
579                         Module[] modules = GetModulesInternal ();
580
581                         if (!getResourceModules) {
582                                 ArrayList result = new ArrayList (modules.Length);
583                                 foreach (Module m in modules)
584                                         if (!m.IsResource ())
585                                                 result.Add (m);
586                                 return (Module[])result.ToArray (typeof (Module));
587                         }
588                         else
589                                 return modules;
590                 }
591
592                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
593                 internal extern string[] GetNamespaces ();
594                 
595                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
596                 public extern virtual String[] GetManifestResourceNames ();
597
598                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
599                 public extern static Assembly GetExecutingAssembly ();
600
601                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
602                 public extern static Assembly GetCallingAssembly ();
603
604                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
605                 public extern AssemblyName[] GetReferencedAssemblies ();
606
607                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
608                 private extern bool GetManifestResourceInfoInternal (String name, ManifestResourceInfo info);
609
610                 public virtual ManifestResourceInfo GetManifestResourceInfo (String resourceName)
611                 {
612                         if (resourceName == null)
613                                 throw new ArgumentNullException ("resourceName");
614                         if (resourceName == "")
615                                 throw new ArgumentException ("String cannot have zero length.");
616                         ManifestResourceInfo result = new ManifestResourceInfo ();
617                         bool found = GetManifestResourceInfoInternal (resourceName, result);
618                         if (found)
619                                 return result;
620                         else
621                                 return null;
622                 }
623
624                 private class ResourceCloseHandler {
625
626                         Module module;
627
628                         public ResourceCloseHandler (Module module) {
629                                 this.module = module;
630                         }
631
632                         public void OnClose (object sender, EventArgs e) {
633                                 // The module dtor will take care of things
634                                 module = null;
635                         }
636                 }
637
638                 //
639                 // The following functions are only for the Mono Debugger.
640                 //
641
642                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
643                 internal static extern MethodBase MonoDebugger_GetMethod (Assembly assembly, int token);
644
645                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
646                 internal static extern int MonoDebugger_GetMethodToken (Assembly assembly, MethodBase method);
647
648                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
649                 internal static extern Type MonoDebugger_GetLocalTypeFromSignature (Assembly assembly, byte[] signature);
650
651                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
652                 internal static extern Type MonoDebugger_GetType (Assembly assembly, int token);
653
654                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
655                 internal static extern string MonoDebugger_CheckRuntimeVersion (string filename);
656
657                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
658                 internal static extern string MonoDebugger_GetMethodIndex (MethodBase method);
659
660                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
661                 internal static extern Type MonoDebugger_MakeArrayType (Type type, int rank);
662
663                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
664                 internal static extern int MonoDebugger_GetTypeToken (Type type);
665
666 #if NET_2_0
667                 [MonoTODO]
668                 [ComVisible (false)]
669                 public long HostContext {
670                         get { return 0; }
671                 }
672
673                 [ComVisible (false)]
674                 public ImageFileMachine ImageFileMachine {
675                         get {
676                                 ImageFileMachine machine;
677                                 PortableExecutableKind kind;
678                                 ModuleHandle handle = ManifestModule.ModuleHandle;
679                                 handle.GetPEKind (out kind, out machine);
680                                 return machine;
681                         }
682                 }
683
684                 [ComVisible (false)]
685                 public extern Module ManifestModule {
686                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
687                         get;
688                 }
689
690                 [ComVisible (false)]
691                 public extern int MetadataToken {
692                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
693                         get;
694                 }
695
696                 [ComVisible (false)]
697                 public PortableExecutableKind PortableExecutableKind {
698                         get {
699                                 ImageFileMachine machine;
700                                 PortableExecutableKind kind;
701                                 ModuleHandle handle = ManifestModule.ModuleHandle;
702                                 handle.GetPEKind (out kind, out machine);
703                                 return kind;
704                         }
705                 }
706
707                 [MonoTODO ("see ReflectionOnlyLoad")]
708                 [ComVisible (false)]
709                 public virtual bool ReflectionOnly {
710                         get { return false; }
711                 }
712 #endif
713
714                 // Code Access Security
715
716                 internal void Resolve () 
717                 {
718                         lock (this) {
719                                 // FIXME: As we (currently) delay the resolution until the first CAS
720                                 // Demand it's too late to evaluate the Minimum permission set as a 
721                                 // condition to load the assembly into the AppDomain
722                                 LoadAssemblyPermissions ();
723                                 _granted = SecurityManager.ResolvePolicy (Evidence, _minimum, _optional,
724                                         _refuse, out _denied);
725                         }
726 #if false
727                         Console.WriteLine ("*** ASSEMBLY RESOLVE INPUT ***");
728                         if (_minimum != null)
729                                 Console.WriteLine ("Minimum: {0}", _minimum);
730                         if (_optional != null)
731                                 Console.WriteLine ("Optional: {0}", _optional);
732                         if (_refuse != null)
733                                 Console.WriteLine ("Refuse: {0}", _refuse);
734                         Console.WriteLine ("*** ASSEMBLY RESOLVE RESULTS ***");
735                         Console.WriteLine ("Granted: {0}", _granted);
736                         if (_denied != null)
737                                 Console.WriteLine ("Denied: {0}", _denied);
738                         Console.WriteLine ("*** ASSEMBLY RESOLVE END ***");
739 #endif
740                 }
741
742                 internal PermissionSet GrantedPermissionSet {
743                         get {
744                                 if (_granted == null) {
745                                         Resolve ();
746                                 }
747                                 return _granted;
748                         }
749                 }
750
751                 internal PermissionSet DeniedPermissionSet {
752                         get {
753                                 // yes we look for granted, as denied may be null
754                                 if (_granted == null) {
755                                         Resolve ();
756                                 }
757                                 return _denied;
758                         }
759                 }
760
761                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
762                 extern internal static bool LoadPermissions (Assembly a, 
763                         ref IntPtr minimum, ref int minLength,
764                         ref IntPtr optional, ref int optLength,
765                         ref IntPtr refused, ref int refLength);
766
767                 // Support for SecurityAction.RequestMinimum, RequestOptional and RequestRefuse
768                 private void LoadAssemblyPermissions ()
769                 {
770                         IntPtr minimum = IntPtr.Zero, optional = IntPtr.Zero, refused = IntPtr.Zero;
771                         int minLength = 0, optLength = 0, refLength = 0;
772                         if (LoadPermissions (this, ref minimum, ref minLength, ref optional,
773                                 ref optLength, ref refused, ref refLength)) {
774
775                                 // Note: no need to cache these permission sets as they will only be created once
776                                 // at assembly resolution time.
777                                 if (minLength > 0) {
778                                         byte[] data = new byte [minLength];
779                                         Marshal.Copy (minimum, data, 0, minLength);
780                                         _minimum = SecurityManager.Decode (data);
781                                 }
782                                 if (optLength > 0) {
783                                         byte[] data = new byte [optLength];
784                                         Marshal.Copy (optional, data, 0, optLength);
785                                         _optional = SecurityManager.Decode (data);
786                                 }
787                                 if (refLength > 0) {
788                                         byte[] data = new byte [refLength];
789                                         Marshal.Copy (refused, data, 0, refLength);
790                                         _refuse = SecurityManager.Decode (data);
791                                 }
792                         }
793                 }
794         }
795 }