[System] Process.WaitForExit now triggers event Exited.
[mono.git] / mcs / class / corlib / System.Reflection / Assembly.cs
1 //
2 // System.Reflection/Assembly.cs
3 //
4 // Author:
5 //   Paolo Molaro (lupus@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.  http://www.ximian.com
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Security;
32 using System.Security.Policy;
33 using System.Security.Permissions;
34 using System.Runtime.Serialization;
35 using System.Reflection;
36 using System.IO;
37 using System.Globalization;
38 using System.Runtime.CompilerServices;
39 using System.Runtime.InteropServices;
40 using System.Collections.Generic;
41 using System.Configuration.Assemblies;
42 using System.Threading;
43 using System.Text;
44 using System.Diagnostics.Contracts;
45
46 using Mono.Security;
47
48 namespace System.Reflection {
49
50         [ComVisible (true)]
51         [ComDefaultInterfaceAttribute (typeof (_Assembly))]
52         [Serializable]
53         [ClassInterface(ClassInterfaceType.None)]
54         [StructLayout (LayoutKind.Sequential)]
55 #if MOBILE
56         public partial class Assembly : ICustomAttributeProvider {
57 #else
58         public abstract class Assembly : ICustomAttributeProvider, _Assembly, IEvidenceFactory, ISerializable {
59 #endif
60                 internal class ResolveEventHolder {
61                         public event ModuleResolveEventHandler ModuleResolve;
62                 }
63
64                 internal class UnmanagedMemoryStreamForModule : UnmanagedMemoryStream
65                 {
66                         Module module;
67
68                         public unsafe UnmanagedMemoryStreamForModule (byte* pointer, long length, Module module)
69                                 : base (pointer, length)
70                         {
71                                 this.module = module;
72                         }
73
74                         protected override void Dispose (bool disposing)
75                         {
76                                 if (_isOpen) {
77                                         /* 
78                                          * The returned pointer points inside metadata, so
79                                          * we have to increase the refcount of the module, and decrease
80                                          * it when the stream is finalized.
81                                          */
82                                         module = null;
83                                 }
84
85                                 base.Dispose (disposing);
86                         }
87                 }
88
89                 // Note: changes to fields must be reflected in _MonoReflectionAssembly struct (object-internals.h)
90 #pragma warning disable 649
91                 private IntPtr _mono_assembly;
92 #pragma warning restore 649
93
94                 private ResolveEventHolder resolve_event_holder;
95 #if !MOBILE
96                 private Evidence _evidence;
97                 internal PermissionSet _minimum;        // for SecurityAction.RequestMinimum
98                 internal PermissionSet _optional;       // for SecurityAction.RequestOptional
99                 internal PermissionSet _refuse;         // for SecurityAction.RequestRefuse
100                 private PermissionSet _granted;         // for the resolved assembly granted permissions
101                 private PermissionSet _denied;          // for the resolved assembly denied permissions
102 #else
103                 object _evidence, _minimum, _optional, _refuse, _granted, _denied;
104 #endif
105                 private bool fromByteArray;
106                 private string assemblyName;
107
108                 protected
109                 Assembly () 
110                 {
111                         resolve_event_holder = new ResolveEventHolder ();
112                 }
113
114                 //
115                 // We can't store the event directly in this class, since the
116                 // compiler would silently insert the fields before _mono_assembly
117                 //
118                 public event ModuleResolveEventHandler ModuleResolve {
119                         [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
120                         add {
121                                 resolve_event_holder.ModuleResolve += value;
122                         }
123                         [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
124                         remove {
125                                 resolve_event_holder.ModuleResolve -= value;
126                         }
127                 }
128
129                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
130                 private extern string get_code_base (bool escaped);
131
132                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
133                 private extern string get_fullname ();
134
135                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
136                 private extern string get_location ();
137
138                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
139                 private extern string InternalImageRuntimeVersion ();
140
141                 // SECURITY: this should be the only caller to icall get_code_base
142                 private string GetCodeBase (bool escaped)
143                 {
144                         string cb = get_code_base (escaped);
145 #if !NET_2_1
146                         if (SecurityManager.SecurityEnabled) {
147                                 // we cannot divulge local file informations
148                                 if (String.Compare ("FILE://", 0, cb, 0, 7, true, CultureInfo.InvariantCulture) == 0) {
149                                         string file = cb.Substring (7);
150                                         new FileIOPermission (FileIOPermissionAccess.PathDiscovery, file).Demand ();
151                                 }
152                         }
153 #endif
154                         return cb;
155                 }
156
157                 public virtual string CodeBase {
158                         get { return GetCodeBase (false); }
159                 }
160
161                 public virtual string EscapedCodeBase {
162                         get { return GetCodeBase (true); }
163                 }
164
165                 public virtual string FullName {
166                         get {
167                                 //
168                                 // FIXME: This is wrong, but it gets us going
169                                 // in the compiler for now
170                                 //
171                                 return ToString ();
172                         }
173                 }
174
175                 public virtual extern MethodInfo EntryPoint {
176                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
177                         get;
178                 }
179
180                 public virtual Evidence Evidence {
181                         [SecurityPermission (SecurityAction.Demand, ControlEvidence = true)]
182                         get { return UnprotectedGetEvidence (); }
183                 }
184
185                 // note: the security runtime requires evidences but may be unable to do so...
186                 internal Evidence UnprotectedGetEvidence ()
187                 {
188 #if MOBILE
189                         return null;
190 #else
191                         // if the host (runtime) hasn't provided it's own evidence...
192                         if (_evidence == null) {
193                                 // ... we will provide our own
194                                 lock (this) {
195                                         _evidence = Evidence.GetDefaultHostEvidence (this);
196                                 }
197                         }
198                         return _evidence;
199 #endif
200                 }
201
202                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
203                 internal extern bool get_global_assembly_cache ();
204
205                 internal bool FromByteArray {
206                         set { fromByteArray = value; }
207                 }
208
209                 public virtual String Location {
210                         get {
211                                 if (fromByteArray)
212                                         return String.Empty;
213
214                                 string loc = get_location ();
215 #if !NET_2_1
216                                 if ((loc != String.Empty) && SecurityManager.SecurityEnabled) {
217                                         // we cannot divulge local file informations
218                                         new FileIOPermission (FileIOPermissionAccess.PathDiscovery, loc).Demand ();
219                                 }
220 #endif
221                                 return loc;
222                         }
223                 }
224
225                 [ComVisible (false)]
226                 public virtual string ImageRuntimeVersion {
227                         get {
228                                 return InternalImageRuntimeVersion ();
229                         }
230                 }
231
232                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
233                 {
234                         throw new NotImplementedException ();
235                 }
236
237                 public virtual bool IsDefined (Type attributeType, bool inherit)
238                 {
239                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
240                 }
241
242                 public virtual object [] GetCustomAttributes (bool inherit)
243                 {
244                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
245                 }
246
247                 public virtual object [] GetCustomAttributes (Type attributeType, bool inherit)
248                 {
249                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
250                 }
251
252                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
253                 private extern object GetFilesInternal (String name, bool getResourceModules);
254
255                 public virtual FileStream[] GetFiles ()
256                 {
257                         return GetFiles (false);
258                 }
259
260                 public virtual FileStream [] GetFiles (bool getResourceModules)
261                 {
262                         string[] names = (string[]) GetFilesInternal (null, getResourceModules);
263                         if (names == null)
264                                 return EmptyArray<FileStream>.Value;
265
266                         string location = Location;
267
268                         FileStream[] res;
269                         if (location != String.Empty) {
270                                 res = new FileStream [names.Length + 1];
271                                 res [0] = new FileStream (location, FileMode.Open, FileAccess.Read);
272                                 for (int i = 0; i < names.Length; ++i)
273                                         res [i + 1] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
274                         } else {
275                                 res = new FileStream [names.Length];
276                                 for (int i = 0; i < names.Length; ++i)
277                                         res [i] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
278                         }
279                         return res;
280                 }
281
282                 public virtual FileStream GetFile (String name)
283                 {
284                         if (name == null)
285                                 throw new ArgumentNullException (null, "Name cannot be null.");
286                         if (name.Length == 0)
287                                 throw new ArgumentException ("Empty name is not valid");
288
289                         string filename = (string)GetFilesInternal (name, true);
290                         if (filename != null)
291                                 return new FileStream (filename, FileMode.Open, FileAccess.Read);
292                         else
293                                 return null;
294                 }
295
296                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
297                 internal extern IntPtr GetManifestResourceInternal (String name, out int size, out Module module);
298
299                 public virtual Stream GetManifestResourceStream (String name)
300                 {
301                         if (name == null)
302                                 throw new ArgumentNullException ("name");
303                         if (name.Length == 0)
304                                 throw new ArgumentException ("String cannot have zero length.",
305                                         "name");
306
307                         ManifestResourceInfo info = GetManifestResourceInfo (name);
308                         if (info == null) {
309                                 Assembly a = AppDomain.CurrentDomain.DoResourceResolve (name, this);
310                                 if (a != null && a != this)
311                                         return a.GetManifestResourceStream (name);
312                                 else
313                                         return null;
314                         }
315
316                         if (info.ReferencedAssembly != null)
317                                 return info.ReferencedAssembly.GetManifestResourceStream (name);
318                         if ((info.FileName != null) && (info.ResourceLocation == 0)) {
319                                 if (fromByteArray)
320                                         throw new FileNotFoundException (info.FileName);
321
322                                 string location = Path.GetDirectoryName (Location);
323                                 string filename = Path.Combine (location, info.FileName);
324                                 return new FileStream (filename, FileMode.Open, FileAccess.Read);
325                         }
326
327                         int size;
328                         Module module;
329                         IntPtr data = GetManifestResourceInternal (name, out size, out module);
330                         if (data == (IntPtr) 0)
331                                 return null;
332                         else {
333                                 UnmanagedMemoryStream stream;
334                                 unsafe {
335                                         stream = new UnmanagedMemoryStreamForModule ((byte*) data, size, module);
336                                 }
337                                 return stream;
338                         }
339                 }
340
341                 public virtual Stream GetManifestResourceStream (Type type, String name)
342                 {
343                         StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
344                         return GetManifestResourceStream(type, name, false, ref stackMark);
345                 }
346
347                 internal Stream GetManifestResourceStream (Type type, String name, bool skipSecurityCheck, ref StackCrawlMark stackMark)
348                 {
349                         StringBuilder sb = new StringBuilder ();
350                         if (type == null) {
351                                 if (name == null)
352                                                 throw new ArgumentNullException ("type");
353                         } else {
354                                 String nameSpace = type.Namespace;
355                                 if (nameSpace != null) {
356                                         sb.Append (nameSpace);
357                                         if (name != null)
358                                                 sb.Append (Type.Delimiter);
359                                 }
360                         }
361
362                         if (name != null)
363                                 sb.Append(name);
364
365                         return GetManifestResourceStream (sb.ToString());
366                 }
367
368                 internal unsafe Stream GetManifestResourceStream(String name, ref StackCrawlMark stackMark, bool skipSecurityCheck)
369                 {
370                         return GetManifestResourceStream (null, name, skipSecurityCheck, ref stackMark);
371                 }
372
373                 internal String GetSimpleName()
374                 {
375                         AssemblyName aname = GetName (true);
376                         return aname.Name;
377                 }
378
379                 internal byte[] GetPublicKey()
380                 {
381                         AssemblyName aname = GetName (true);
382                         return aname.GetPublicKey ();
383                 }
384
385                 internal Version GetVersion()
386                 {
387                         AssemblyName aname = GetName (true);
388                         return aname.Version;
389                 }
390
391                 private AssemblyNameFlags GetFlags()
392                 {
393                         AssemblyName aname = GetName (true);
394                         return aname.Flags;
395                 }
396
397                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
398                 internal virtual extern Type[] GetTypes (bool exportedOnly);
399                 
400                 public virtual Type[] GetTypes ()
401                 {
402                         return GetTypes (false);
403                 }
404
405                 public virtual Type[] GetExportedTypes ()
406                 {
407                         return GetTypes (true);
408                 }
409
410                 public virtual Type GetType (String name, Boolean throwOnError)
411                 {
412                         return GetType (name, throwOnError, false);
413                 }
414
415                 public virtual Type GetType (String name) {
416                         return GetType (name, false, false);
417                 }
418
419                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
420                 internal extern Type InternalGetType (Module module, String name, Boolean throwOnError, Boolean ignoreCase);
421
422                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
423                 internal extern static void InternalGetAssemblyName (string assemblyFile, AssemblyName aname);
424
425                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
426                 static extern void FillName (Assembly ass, AssemblyName aname);
427
428                 [MonoTODO ("copiedName == true is not supported")]
429                 public virtual AssemblyName GetName (Boolean copiedName)
430                 {
431 #if !MOBILE
432                         // CodeBase, which is restricted, will be copied into the AssemblyName object so...
433                         if (SecurityManager.SecurityEnabled) {
434                                 GetCodeBase (true); // this will ensure the Demand is made
435                         }
436 #endif
437                         return UnprotectedGetName ();
438                 }
439
440                 public virtual AssemblyName GetName ()
441                 {
442                         return GetName (false);
443                 }
444
445                 // the security runtime requires access to the assemblyname (e.g. to get the strongname)
446                 internal virtual AssemblyName UnprotectedGetName ()
447                 {
448                         AssemblyName aname = new AssemblyName ();
449                         FillName (this, aname);
450                         return aname;
451                 }
452
453                 public override string ToString ()
454                 {
455                         // note: ToString work without requiring CodeBase (so no checks are needed)
456
457                         if (assemblyName != null)
458                                 return assemblyName;
459
460                         assemblyName = get_fullname ();
461                         return assemblyName;
462                 }
463
464                 public static String CreateQualifiedName (String assemblyName, String typeName) 
465                 {
466                         return typeName + ", " + assemblyName;
467                 }
468
469                 public static Assembly GetAssembly (Type type)
470                 {
471                         if (type != null)
472                                 return type.Assembly;
473                         throw new ArgumentNullException ("type");
474                 }
475
476
477                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
478                 public static extern Assembly GetEntryAssembly();
479
480                 internal Assembly GetSatelliteAssembly (CultureInfo culture, Version version, bool throwOnError)
481                 {
482                         if (culture == null)
483                                 throw new ArgumentNullException("culture");
484                         Contract.EndContractBlock();
485
486                         StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
487                         String name = GetSimpleName() + ".resources";
488                         return InternalGetSatelliteAssembly(name, culture, version, true, ref stackMark);
489                 }
490
491                 internal RuntimeAssembly InternalGetSatelliteAssembly (String name, CultureInfo culture, Version version, bool throwOnFileNotFound, ref StackCrawlMark stackMark)
492                 {
493                         AssemblyName an = new AssemblyName ();
494
495                         an.SetPublicKey (GetPublicKey ());
496                         an.Flags = GetFlags () | AssemblyNameFlags.PublicKey;
497
498                         if (version == null)
499                                 an.Version = GetVersion ();
500                         else
501                                 an.Version = version;
502
503                         an.CultureInfo = culture;
504                         an.Name = name;
505
506                         Assembly assembly;
507
508                         try {
509                                 assembly = AppDomain.CurrentDomain.LoadSatellite (an, false);
510                                 if (assembly != null)
511                                         return (RuntimeAssembly)assembly;
512                         } catch (FileNotFoundException) {
513                                 assembly = null;
514                                 // ignore
515                         }
516
517                         // Try the assembly directory
518                         string location = Path.GetDirectoryName (Location);
519                         string fullName = Path.Combine (location, Path.Combine (culture.Name, an.Name + ".dll"));
520                         if (!throwOnFileNotFound && !File.Exists (fullName))
521                                 return null;
522
523                         return (RuntimeAssembly)LoadFrom (fullName);
524                 }
525
526 #if !MOBILE
527                 Type _Assembly.GetType ()
528                 {
529                         // Required or object::GetType becomes virtual final
530                         return base.GetType ();
531                 }               
532 #endif
533
534                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
535                 private extern static Assembly LoadFrom (String assemblyFile, bool refonly);
536
537                 public static Assembly LoadFrom (String assemblyFile)
538                 {
539                         return LoadFrom (assemblyFile, false);
540                 }
541
542                 [Obsolete]
543                 public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence)
544                 {
545                         Assembly a = LoadFrom (assemblyFile, false);
546 #if !NET_2_1
547                         if ((a != null) && (securityEvidence != null)) {
548                                 // merge evidence (i.e. replace defaults with provided evidences)
549                                 a.Evidence.Merge (securityEvidence);
550                         }
551 #endif
552                         return a;
553                 }
554
555                 [Obsolete]
556                 [MonoTODO("This overload is not currently implemented")]
557                 // FIXME: What are we missing?
558                 public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
559                 {
560                         throw new NotImplementedException ();
561                 }
562
563                 [MonoTODO]
564                 public static Assembly LoadFrom (String assemblyFile, byte [] hashValue, AssemblyHashAlgorithm hashAlgorithm)
565                 {
566                         throw new NotImplementedException ();
567                 }
568
569                 public static Assembly UnsafeLoadFrom (String assemblyFile)
570                 {
571                         return LoadFrom (assemblyFile);
572                 }
573
574                 [Obsolete]
575                 public static Assembly LoadFile (String path, Evidence securityEvidence)
576                 {
577                         if (path == null)
578                                 throw new ArgumentNullException ("path");
579                         if (path == String.Empty)
580                                 throw new ArgumentException ("Path can't be empty", "path");
581                         // FIXME: Make this do the right thing
582                         return LoadFrom (path, securityEvidence);
583                 }
584
585                 public static Assembly LoadFile (String path)
586                 {
587                         return LoadFile (path, null);
588                 }
589
590                 public static Assembly Load (String assemblyString)
591                 {
592                         return AppDomain.CurrentDomain.Load (assemblyString);
593                 }
594
595                 [Obsolete]
596                 public static Assembly Load (String assemblyString, Evidence assemblySecurity)
597                 {
598                         return AppDomain.CurrentDomain.Load (assemblyString, assemblySecurity);
599                 }
600
601                 public static Assembly Load (AssemblyName assemblyRef)
602                 {
603                         return AppDomain.CurrentDomain.Load (assemblyRef);
604                 }
605
606                 [Obsolete]
607                 public static Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
608                 {
609                         return AppDomain.CurrentDomain.Load (assemblyRef, assemblySecurity);
610                 }
611
612                 public static Assembly Load (Byte[] rawAssembly)
613                 {
614                         return AppDomain.CurrentDomain.Load (rawAssembly);
615                 }
616
617                 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore)
618                 {
619                         return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
620                 }
621
622                 [Obsolete]
623                 public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore,
624                                              Evidence securityEvidence)
625                 {
626                         return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore, securityEvidence);
627                 }
628
629                 [MonoLimitation ("Argument securityContextSource is ignored")]
630                 public static Assembly Load (byte [] rawAssembly, byte [] rawSymbolStore, SecurityContextSource securityContextSource)
631                 {
632                         return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
633                 }
634
635                 public static Assembly ReflectionOnlyLoad (byte[] rawAssembly)
636                 {
637                         return AppDomain.CurrentDomain.Load (rawAssembly, null, null, true);
638                 }
639
640                 public static Assembly ReflectionOnlyLoad (string assemblyString) 
641                 {
642                         return AppDomain.CurrentDomain.Load (assemblyString, null, true);
643                 }
644
645                 public static Assembly ReflectionOnlyLoadFrom (string assemblyFile) 
646                 {
647                         if (assemblyFile == null)
648                                 throw new ArgumentNullException ("assemblyFile");
649                         
650                         return LoadFrom (assemblyFile, true);
651                 }
652
653                 [Obsolete]
654                 public static Assembly LoadWithPartialName (string partialName)
655                 {
656                         return LoadWithPartialName (partialName, null);
657                 }
658
659                 [MonoTODO ("Not implemented")]
660                 public Module LoadModule (string moduleName, byte [] rawModule)
661                 {
662                         throw new NotImplementedException ();
663                 }
664
665                 [MonoTODO ("Not implemented")]
666                 public
667                 virtual
668                 Module LoadModule (string moduleName, byte [] rawModule, byte [] rawSymbolStore)
669                 {
670                         throw new NotImplementedException ();
671                 }
672
673                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
674                 private static extern Assembly load_with_partial_name (string name, Evidence e);
675
676                 [Obsolete]
677                 public static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence)
678                 {
679                         return LoadWithPartialName (partialName, securityEvidence, true);
680                 }
681
682                 /**
683                  * LAMESPEC: It is possible for this method to throw exceptions IF the name supplied
684                  * is a valid gac name and contains filesystem entry charachters at the end of the name
685                  * ie System/// will throw an exception. However ////System will not as that is canocolized
686                  * out of the name.
687                  */
688
689                 // FIXME: LoadWithPartialName must look cache (no CAS) or read from disk (CAS)
690                 internal static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence, bool oldBehavior)
691                 {
692                         if (!oldBehavior)
693                                 throw new NotImplementedException ();
694
695                         if (partialName == null)
696                                 throw new NullReferenceException ();
697
698                         return load_with_partial_name (partialName, securityEvidence);
699                 }
700
701                 public Object CreateInstance (String typeName) 
702                 {
703                         return CreateInstance (typeName, false);
704                 }
705
706                 public Object CreateInstance (String typeName, Boolean ignoreCase)
707                 {
708                         Type t = GetType (typeName, false, ignoreCase);
709                         if (t == null)
710                                 return null;
711
712                         try {
713                                 return Activator.CreateInstance (t);
714                         } catch (InvalidOperationException) {
715                                 throw new ArgumentException ("It is illegal to invoke a method on a Type loaded via ReflectionOnly methods.");
716                         }
717                 }
718
719                 public
720                 virtual
721                 Object CreateInstance (String typeName, Boolean ignoreCase,
722                                               BindingFlags bindingAttr, Binder binder,
723                                               Object[] args, CultureInfo culture,
724                                               Object[] activationAttributes)
725                 {
726                         Type t = GetType (typeName, false, ignoreCase);
727                         if (t == null)
728                                 return null;
729
730                         try {
731                                 return Activator.CreateInstance (t, bindingAttr, binder, args, culture, activationAttributes);
732                         } catch (InvalidOperationException) {
733                                 throw new ArgumentException ("It is illegal to invoke a method on a Type loaded via ReflectionOnly methods.");
734                         }
735                 }
736
737                 public Module[] GetLoadedModules ()
738                 {
739                         return GetLoadedModules (false);
740                 }
741
742                 public Module[] GetModules ()
743                 {
744                         return GetModules (false);
745                 }
746
747                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
748                 internal virtual extern Module[] GetModulesInternal ();
749                 
750                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
751                 public extern virtual String[] GetManifestResourceNames ();
752
753                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
754                 public extern static Assembly GetExecutingAssembly ();
755
756                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
757                 public extern static Assembly GetCallingAssembly ();
758
759                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
760                 internal static extern AssemblyName[] GetReferencedAssemblies (Assembly module);
761
762                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
763                 private extern bool GetManifestResourceInfoInternal (String name, ManifestResourceInfo info);
764
765                 public virtual ManifestResourceInfo GetManifestResourceInfo (String resourceName)
766                 {
767                         if (resourceName == null)
768                                 throw new ArgumentNullException ("resourceName");
769                         if (resourceName.Length == 0)
770                                 throw new ArgumentException ("String cannot have zero length.");
771                         ManifestResourceInfo result = new ManifestResourceInfo (null, null, 0);
772                         bool found = GetManifestResourceInfoInternal (resourceName, result);
773                         if (found)
774                                 return result;
775                         else
776                                 return null;
777                 }
778
779                 [MonoTODO ("Currently it always returns zero")]
780                 [ComVisible (false)]
781                 public
782                 virtual
783                 long HostContext {
784                         get { return 0; }
785                 }
786
787
788                 internal virtual Module GetManifestModule () {
789                         return GetManifestModuleInternal ();
790                 }
791
792                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
793                 internal extern Module GetManifestModuleInternal ();
794
795                 [ComVisible (false)]
796                 public virtual extern bool ReflectionOnly {
797                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
798                         get;
799                 }
800                 
801                 public override int GetHashCode ()
802                 {
803                         return base.GetHashCode ();
804                 }
805
806                 public override bool Equals (object o)
807                 {
808                         if (((object) this) == o)
809                                 return true;
810
811                         if (o == null)
812                                 return false;
813                         
814                         Assembly other = (Assembly) o;
815                         return other._mono_assembly == _mono_assembly;
816                 }
817
818 #if !NET_2_1
819                 // Code Access Security
820
821                 internal void Resolve () 
822                 {
823                         lock (this) {
824                                 // FIXME: As we (currently) delay the resolution until the first CAS
825                                 // Demand it's too late to evaluate the Minimum permission set as a 
826                                 // condition to load the assembly into the AppDomain
827                                 LoadAssemblyPermissions ();
828                                 Evidence e = new Evidence (UnprotectedGetEvidence ()); // we need a copy to add PRE
829                                 e.AddHost (new PermissionRequestEvidence (_minimum, _optional, _refuse));
830                                 _granted = SecurityManager.ResolvePolicy (e,
831                                         _minimum, _optional, _refuse, out _denied);
832                         }
833                 }
834
835                 internal PermissionSet GrantedPermissionSet {
836                         get {
837                                 if (_granted == null) {
838                                         if (SecurityManager.ResolvingPolicyLevel != null) {
839                                                 if (SecurityManager.ResolvingPolicyLevel.IsFullTrustAssembly (this))
840                                                         return DefaultPolicies.FullTrust;
841                                                 else
842                                                         return null; // we can't resolve during resolution
843                                         }
844                                         Resolve ();
845                                 }
846                                 return _granted;
847                         }
848                 }
849
850                 internal PermissionSet DeniedPermissionSet {
851                         get {
852                                 // yes we look for granted, as denied may be null
853                                 if (_granted == null) {
854                                         if (SecurityManager.ResolvingPolicyLevel != null) {
855                                                 if (SecurityManager.ResolvingPolicyLevel.IsFullTrustAssembly (this))
856                                                         return null;
857                                                 else
858                                                         return DefaultPolicies.FullTrust; // deny unrestricted
859                                         }
860                                         Resolve ();
861                                 }
862                                 return _denied;
863                         }
864                 }
865
866                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
867                 extern internal static bool LoadPermissions (Assembly a, 
868                         ref IntPtr minimum, ref int minLength,
869                         ref IntPtr optional, ref int optLength,
870                         ref IntPtr refused, ref int refLength);
871
872                 // Support for SecurityAction.RequestMinimum, RequestOptional and RequestRefuse
873                 private void LoadAssemblyPermissions ()
874                 {
875                         IntPtr minimum = IntPtr.Zero, optional = IntPtr.Zero, refused = IntPtr.Zero;
876                         int minLength = 0, optLength = 0, refLength = 0;
877                         if (LoadPermissions (this, ref minimum, ref minLength, ref optional,
878                                 ref optLength, ref refused, ref refLength)) {
879
880                                 // Note: no need to cache these permission sets as they will only be created once
881                                 // at assembly resolution time.
882                                 if (minLength > 0) {
883                                         byte[] data = new byte [minLength];
884                                         Marshal.Copy (minimum, data, 0, minLength);
885                                         _minimum = SecurityManager.Decode (data);
886                                 }
887                                 if (optLength > 0) {
888                                         byte[] data = new byte [optLength];
889                                         Marshal.Copy (optional, data, 0, optLength);
890                                         _optional = SecurityManager.Decode (data);
891                                 }
892                                 if (refLength > 0) {
893                                         byte[] data = new byte [refLength];
894                                         Marshal.Copy (refused, data, 0, refLength);
895                                         _refuse = SecurityManager.Decode (data);
896                                 }
897                         }
898                 }
899                 
900                 public virtual PermissionSet PermissionSet {
901                         get { return this.GrantedPermissionSet; }
902                 }
903                 
904                 public virtual SecurityRuleSet SecurityRuleSet {
905                         get { throw CreateNIE (); }
906                 }
907
908 #endif
909
910                 static Exception CreateNIE ()
911                 {
912                         return new NotImplementedException ("Derived classes must implement it");
913                 }
914                 
915                 public virtual IList<CustomAttributeData> GetCustomAttributesData ()
916                 {
917                         return CustomAttributeData.GetCustomAttributes (this);
918                 }
919
920                 [MonoTODO]
921                 public bool IsFullyTrusted {
922                         get { return true; }
923                 }
924
925                 public virtual Type GetType (string name, bool throwOnError, bool ignoreCase)
926                 {
927                         throw CreateNIE ();
928                 }
929
930                 public virtual Module GetModule (String name)
931                 {
932                         throw CreateNIE ();
933                 }
934
935                 public virtual AssemblyName[] GetReferencedAssemblies ()
936                 {
937                         throw CreateNIE ();
938                 }
939
940                 public virtual Module[] GetModules (bool getResourceModules)
941                 {
942                         throw CreateNIE ();
943                 }
944
945                 [MonoTODO ("Always returns the same as GetModules")]
946                 public virtual Module[] GetLoadedModules (bool getResourceModules)
947                 {
948                         throw CreateNIE ();
949                 }
950
951                 public virtual Assembly GetSatelliteAssembly (CultureInfo culture)
952                 {
953                         throw CreateNIE ();
954                 }
955
956                 public virtual Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
957                 {
958                         throw CreateNIE ();
959                 }
960
961                 public virtual Module ManifestModule {
962                         get { throw CreateNIE (); }
963                 }
964
965                 public virtual bool GlobalAssemblyCache {
966                         get { throw CreateNIE (); }
967                 }
968
969                 public virtual bool IsDynamic {
970                         get { return false; }
971                 }
972
973                 public static bool operator == (Assembly left, Assembly right)
974                 {
975                         if ((object)left == (object)right)
976                                 return true;
977                         if ((object)left == null ^ (object)right == null)
978                                 return false;
979                         return left.Equals (right);
980                 }
981
982                 public static bool operator != (Assembly left, Assembly right)
983                 {
984                         if ((object)left == (object)right)
985                                 return false;
986                         if ((object)left == null ^ (object)right == null)
987                                 return true;
988                         return !left.Equals (right);
989                 }
990
991                 public virtual IEnumerable<TypeInfo> DefinedTypes {
992                         get {
993                                 foreach (var type in GetTypes ()) {
994                                         yield return type.GetTypeInfo ();
995                                 }
996                         }
997                 }
998
999                 public virtual IEnumerable<Type> ExportedTypes {
1000                         get { return GetExportedTypes (); }
1001                 }
1002
1003                 public virtual IEnumerable<Module> Modules {
1004                         get { return GetModules (); }
1005                 }
1006
1007                 public virtual IEnumerable<CustomAttributeData> CustomAttributes {
1008                         get { return GetCustomAttributesData (); }
1009                 }
1010         }
1011 }