2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / corlib / System / AppDomain.cs
1 //
2 // System.AppDomain.cs
3 //
4 // Authors:
5 //   Paolo Molaro (lupus@ximian.com)
6 //   Dietmar Maurer (dietmar@ximian.com)
7 //   Miguel de Icaza (miguel@ximian.com)
8 //   Gonzalo Paniagua (gonzalo@ximian.com)
9 //   Patrik Torstensson
10 //   Sebastien Pouliot (sebastien@ximian.com)
11 //
12 // (C) 2001, 2002 Ximian, Inc.  http://www.ximian.com
13 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System.Collections;
36 using System.Globalization;
37 using System.IO;
38 using System.Reflection;
39 using System.Reflection.Emit;
40 using System.Threading;
41 using System.Runtime.CompilerServices;
42 using System.Runtime.InteropServices;
43 using System.Runtime.Remoting;
44 using System.Runtime.Remoting.Contexts;
45 using System.Runtime.Remoting.Channels;
46 using System.Runtime.Remoting.Messaging;
47 using System.Security;
48 using System.Security.Permissions;
49 using System.Security.Policy;
50 using System.Security.Principal;
51 using System.Configuration.Assemblies;
52
53 #if NET_2_0
54 using System.Collections.Generic;
55 using System.Runtime.ConstrainedExecution;
56 #endif
57
58 namespace System {
59
60 #if NET_2_0
61         [ComVisible (true)]
62         [ComDefaultInterface (typeof (_AppDomain))]
63 #endif
64         [ClassInterface(ClassInterfaceType.None)]
65         public sealed class AppDomain : MarshalByRefObject , _AppDomain , IEvidenceFactory
66         {
67         #pragma warning disable 169
68         #region Sync with object-internals.h
69                 IntPtr _mono_app_domain;
70                 #endregion
71         #pragma warning restore 169
72                 static string _process_guid;
73
74                 [ThreadStatic]
75                 static Hashtable type_resolve_in_progress;
76
77                 [ThreadStatic]
78                 static Hashtable assembly_resolve_in_progress;
79
80                 [ThreadStatic]
81                 static Hashtable assembly_resolve_in_progress_refonly;
82
83                 // CAS
84                 private Evidence _evidence;
85                 private PermissionSet _granted;
86
87                 // non-CAS
88                 private PrincipalPolicy _principalPolicy;
89
90                 [ThreadStatic]
91                 private static IPrincipal _principal;
92                 
93                 static AppDomain default_domain;
94
95                 private AppDomain ()
96                 {
97                 }
98
99                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
100                 private extern AppDomainSetup getSetup ();
101
102                 AppDomainSetup SetupInformationNoCopy {
103                         get { return getSetup (); }
104                 }
105
106                 public AppDomainSetup SetupInformation {
107                         get {
108                                 AppDomainSetup setup = getSetup ();
109                                 return new AppDomainSetup (setup);
110                         }
111                 }
112
113 #if NET_2_0
114                 [MonoTODO]
115                 public ApplicationTrust ApplicationTrust {
116                         get { throw new NotImplementedException (); }
117                 }
118 #endif
119
120                 public string BaseDirectory {
121                         get {
122                                 string path = SetupInformationNoCopy.ApplicationBase;
123                                 if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
124                                         // we cannot divulge local file informations
125                                         new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
126                                 }
127                                 return path;
128                         }
129                 }
130
131                 public string RelativeSearchPath {
132                         get {
133                                 string path = SetupInformationNoCopy.PrivateBinPath;
134                                 if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
135                                         // we cannot divulge local file informations
136                                         new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
137                                 }
138                                 return path;
139                         }
140                 }
141
142                 public string DynamicDirectory {
143                         get {
144                                 AppDomainSetup setup = SetupInformationNoCopy;
145                                 if (setup.DynamicBase == null)
146                                         return null;
147
148                                 string path = Path.Combine (setup.DynamicBase, setup.ApplicationName);
149                                 if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
150                                         // we cannot divulge local file informations
151                                         new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
152                                 }
153                                 return path;
154                         }
155                 }
156
157                 public bool ShadowCopyFiles {
158                         get {
159                                 return (SetupInformationNoCopy.ShadowCopyFiles == "true");
160                         }
161                 }
162
163                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
164                 private extern string getFriendlyName ();
165
166                 public string FriendlyName {
167                         get {
168                                 return getFriendlyName ();
169                         }
170                 }
171
172                 public Evidence Evidence {
173                         get {
174                                 // if the host (runtime) hasn't provided it's own evidence...
175                                 if (_evidence == null) {
176                                         // ... we will provide our own
177                                         lock (this) {
178                                                 // the executed assembly from the "default" appdomain
179                                                 // or null if we're not in the default appdomain or
180                                                 // if there is no entry assembly (embedded mono)
181                                                 Assembly a = Assembly.GetEntryAssembly ();
182                                                 if (a == null) {
183                                                         if (this == DefaultDomain)
184                                                                 // mono is embedded
185                                                                 return new Evidence ();
186                                                         else
187                                                                 _evidence = AppDomain.DefaultDomain.Evidence;
188                                                 } else {
189                                                         _evidence = Evidence.GetDefaultHostEvidence (a);
190                                                 }
191                                         }
192                                 }
193                                 return new Evidence (_evidence);        // return a copy
194                         }
195                 }
196
197                 internal IPrincipal DefaultPrincipal {
198                         get {
199                                 if (_principal == null) {
200                                         switch (_principalPolicy) {
201                                                 case PrincipalPolicy.UnauthenticatedPrincipal:
202                                                         _principal = new GenericPrincipal (
203                                                                 new GenericIdentity (String.Empty, String.Empty), null);
204                                                         break;
205                                                 case PrincipalPolicy.WindowsPrincipal:
206                                                         _principal = new WindowsPrincipal (WindowsIdentity.GetCurrent ());
207                                                         break;
208                                         }
209                                 }
210                                 return _principal; 
211                         }
212                 }
213
214                 // for AppDomain there is only an allowed (i.e. granted) set
215                 // http://msdn.microsoft.com/library/en-us/cpguide/html/cpcondetermininggrantedpermissions.asp
216                 internal PermissionSet GrantedPermissionSet {
217                         get { return _granted; }
218                 }
219
220                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
221                 private static extern AppDomain getCurDomain ();
222                 
223                 public static AppDomain CurrentDomain {
224                         get {
225                                 return getCurDomain ();
226                         }
227                 }
228
229                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
230                 private static extern AppDomain getRootDomain ();
231
232                 internal static AppDomain DefaultDomain {
233                         get {
234                                 if (default_domain == null) {
235                                         AppDomain rd = getRootDomain ();
236                                         if (rd == CurrentDomain)
237                                                 default_domain = rd;
238                                         else
239                                                 default_domain = (AppDomain) RemotingServices.GetDomainProxy (rd);
240                                 }
241                                 return default_domain;
242                         }
243                 }
244
245 #if NET_2_0
246                 [Obsolete ("AppDomain.AppendPrivatePath has been deprecated. Please investigate the use of AppDomainSetup.PrivateBinPath instead.")]
247 #endif
248                 [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
249                 public void AppendPrivatePath (string path)
250                 {
251                         if (path == null || path.Length == 0)
252                                 return;
253
254                         AppDomainSetup setup = SetupInformationNoCopy;
255
256                         string pp = setup.PrivateBinPath;
257                         if (pp == null || pp.Length == 0) {
258                                 setup.PrivateBinPath = path;
259                                 return;
260                         }
261
262                         pp = pp.Trim ();
263                         if (pp [pp.Length - 1] != Path.PathSeparator)
264                                 pp += Path.PathSeparator;
265
266                         setup.PrivateBinPath = pp + path;
267                 }
268
269 #if NET_2_0
270                 [Obsolete ("AppDomain.ClearPrivatePath has been deprecated. Please investigate the use of AppDomainSetup.PrivateBinPath instead.")]
271 #endif
272                 [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
273                 public void ClearPrivatePath ()
274                 {
275                         SetupInformationNoCopy.PrivateBinPath = String.Empty;
276                 }
277
278 #if NET_2_0
279                 [Obsolete ("Use AppDomainSetup.ShadowCopyDirectories")]
280 #endif
281                 [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
282                 public void ClearShadowCopyPath ()
283                 {
284                         SetupInformationNoCopy.ShadowCopyDirectories = String.Empty;
285                 }
286
287                 public ObjectHandle CreateComInstanceFrom (string assemblyName, string typeName)
288                 {
289                         return Activator.CreateComInstanceFrom (assemblyName, typeName);
290                 }
291
292 #if NET_1_1
293                 public ObjectHandle CreateComInstanceFrom (string assemblyFile, string typeName,
294                         byte [] hashValue, AssemblyHashAlgorithm hashAlgorithm)
295                 {
296                         return Activator.CreateComInstanceFrom (assemblyFile, typeName, hashValue ,hashAlgorithm);
297                 }
298 #endif
299
300                 public ObjectHandle CreateInstance (string assemblyName, string typeName)
301                 {
302                         if (assemblyName == null)
303                                 throw new ArgumentNullException ("assemblyName");
304
305                         return Activator.CreateInstance (assemblyName, typeName);
306                 }
307
308                 public ObjectHandle CreateInstance (string assemblyName, string typeName, object[] activationAttributes)
309                 {
310                         if (assemblyName == null)
311                                 throw new ArgumentNullException ("assemblyName");
312
313                         return Activator.CreateInstance (assemblyName, typeName, activationAttributes);
314                 }
315
316                 public ObjectHandle CreateInstance (string assemblyName, string typeName, bool ignoreCase, BindingFlags bindingAttr,
317                                                     Binder binder, object[] args, CultureInfo culture, object[] activationAttributes,
318                                                     Evidence securityAttributes)
319                 {
320                         if (assemblyName == null)
321                                 throw new ArgumentNullException ("assemblyName");
322
323                         return Activator.CreateInstance (assemblyName, typeName, ignoreCase, bindingAttr, binder, args,
324                                 culture, activationAttributes, securityAttributes);
325                 }
326
327                 public object CreateInstanceAndUnwrap (string assemblyName, string typeName)
328                 {
329                         ObjectHandle oh = CreateInstance (assemblyName, typeName);
330                         return (oh != null) ? oh.Unwrap () : null;
331                 }
332
333                 public object CreateInstanceAndUnwrap (string assemblyName, string typeName, object [] activationAttributes)
334                 {
335                         ObjectHandle oh = CreateInstance (assemblyName, typeName, activationAttributes);
336                         return (oh != null) ? oh.Unwrap () : null;
337                 }
338
339                 public object CreateInstanceAndUnwrap (string assemblyName, string typeName, bool ignoreCase,
340                                                        BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture,
341                                                        object[] activationAttributes, Evidence securityAttributes)
342                 {
343                         ObjectHandle oh = CreateInstance (assemblyName, typeName, ignoreCase, bindingAttr, binder, args,
344                                 culture, activationAttributes, securityAttributes);
345                         return (oh != null) ? oh.Unwrap () : null;
346                 }
347
348                 public ObjectHandle CreateInstanceFrom (string assemblyFile, string typeName)
349                 {
350                         if (assemblyFile == null)
351                                 throw new ArgumentNullException ("assemblyFile");
352
353                         return Activator.CreateInstanceFrom (assemblyFile, typeName);
354                 }
355
356                 public ObjectHandle CreateInstanceFrom (string assemblyFile, string typeName, object[] activationAttributes)
357                 {
358                         if (assemblyFile == null)
359                                 throw new ArgumentNullException ("assemblyFile");
360
361                         return Activator.CreateInstanceFrom (assemblyFile, typeName, activationAttributes);
362                 }
363
364                 public ObjectHandle CreateInstanceFrom (string assemblyFile, string typeName, bool ignoreCase,
365                                                         BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture,
366                                                         object[] activationAttributes, Evidence securityAttributes)
367                 {
368                         if (assemblyFile == null)
369                                 throw new ArgumentNullException ("assemblyFile");
370
371                         return Activator.CreateInstanceFrom (assemblyFile, typeName, ignoreCase, bindingAttr, binder, args,
372                                                              culture, activationAttributes, securityAttributes);
373                 }
374
375                 public object CreateInstanceFromAndUnwrap (string assemblyName, string typeName)
376                 {
377                         ObjectHandle oh = CreateInstanceFrom (assemblyName, typeName);
378                         return (oh != null) ? oh.Unwrap () : null;
379                 }
380
381                 public object CreateInstanceFromAndUnwrap (string assemblyName, string typeName, object [] activationAttributes)
382                 {
383                         ObjectHandle oh = CreateInstanceFrom (assemblyName, typeName, activationAttributes);
384                         return (oh != null) ? oh.Unwrap () : null;
385                 }
386
387                 public object CreateInstanceFromAndUnwrap (string assemblyName, string typeName, bool ignoreCase,
388                                                            BindingFlags bindingAttr, Binder binder, object[] args,
389                                                            CultureInfo culture, object[] activationAttributes,
390                                                            Evidence securityAttributes)
391                 {
392                         ObjectHandle oh = CreateInstanceFrom (assemblyName, typeName, ignoreCase, bindingAttr, binder, args,
393                                 culture, activationAttributes, securityAttributes);
394
395                         return (oh != null) ? oh.Unwrap () : null;
396                 }
397
398                 public AssemblyBuilder DefineDynamicAssembly (AssemblyName name, AssemblyBuilderAccess access)
399                 {
400                         return DefineDynamicAssembly (name, access, null, null, null, null, null, false);
401                 }
402
403                 public AssemblyBuilder DefineDynamicAssembly (AssemblyName name, AssemblyBuilderAccess access, Evidence evidence)
404                 {
405                         return DefineDynamicAssembly (name, access, null, evidence, null, null, null, false);
406                 }
407
408                 public AssemblyBuilder DefineDynamicAssembly (AssemblyName name, AssemblyBuilderAccess access, string dir)
409                 {
410                         return DefineDynamicAssembly (name, access, dir, null, null, null, null, false);
411                 }
412
413                 public AssemblyBuilder DefineDynamicAssembly (AssemblyName name, AssemblyBuilderAccess access, string dir,
414                                                               Evidence evidence)
415                 {
416                         return DefineDynamicAssembly (name, access, dir, evidence, null, null, null, false);
417                 }
418
419                 public AssemblyBuilder DefineDynamicAssembly (AssemblyName name, AssemblyBuilderAccess access,
420                                                               PermissionSet requiredPermissions,
421                                                               PermissionSet optionalPermissions,
422                                                               PermissionSet refusedPermissions)
423                 {
424                         return DefineDynamicAssembly (name, access, null, null, requiredPermissions, optionalPermissions,
425                                 refusedPermissions, false);
426                 }
427
428                 public AssemblyBuilder DefineDynamicAssembly (AssemblyName name, AssemblyBuilderAccess access, Evidence evidence,
429                                                               PermissionSet requiredPermissions,
430                                                               PermissionSet optionalPermissions,
431                                                               PermissionSet refusedPermissions)
432                 {
433                         return DefineDynamicAssembly (name, access, null, evidence, requiredPermissions, optionalPermissions,
434                                 refusedPermissions, false);
435                 }
436
437                 public AssemblyBuilder DefineDynamicAssembly (AssemblyName name, AssemblyBuilderAccess access, string dir,
438                                                               PermissionSet requiredPermissions,
439                                                               PermissionSet optionalPermissions,
440                                                               PermissionSet refusedPermissions)
441                 {
442                         return DefineDynamicAssembly (name, access, dir, null, requiredPermissions, optionalPermissions,
443                                 refusedPermissions, false);
444                 }
445
446                 public AssemblyBuilder DefineDynamicAssembly (AssemblyName name, AssemblyBuilderAccess access, string dir,
447                                                               Evidence evidence,
448                                                               PermissionSet requiredPermissions,
449                                                               PermissionSet optionalPermissions,
450                                                               PermissionSet refusedPermissions)
451                 {
452                         return DefineDynamicAssembly (name, access, dir, evidence, requiredPermissions, optionalPermissions,
453                                 refusedPermissions, false);
454                 }
455
456                 public AssemblyBuilder DefineDynamicAssembly (AssemblyName name, AssemblyBuilderAccess access, string dir,
457                                                               Evidence evidence,
458                                                               PermissionSet requiredPermissions,
459                                                               PermissionSet optionalPermissions,
460                                                               PermissionSet refusedPermissions, bool isSynchronized)
461                 {
462                         if (name == null)
463                                 throw new ArgumentNullException ("name");
464                         ValidateAssemblyName (name.Name);
465
466                         // FIXME: examine all other parameters
467                         
468                         AssemblyBuilder ab = new AssemblyBuilder (name, dir, access, false);
469                         ab.AddPermissionRequests (requiredPermissions, optionalPermissions, refusedPermissions);
470                         return ab;
471                 }
472
473 #if NET_2_0
474                 // NET 3.5 method
475                 public AssemblyBuilder DefineDynamicAssembly (AssemblyName name, AssemblyBuilderAccess access, string dir,
476                                                               Evidence evidence,
477                                                               PermissionSet requiredPermissions,
478                                                               PermissionSet optionalPermissions,
479                                                               PermissionSet refusedPermissions, bool isSynchronized, IEnumerable<CustomAttributeBuilder> assemblyAttributes)
480                 {
481                         AssemblyBuilder ab = DefineDynamicAssembly (name, access, dir, evidence, requiredPermissions, optionalPermissions, refusedPermissions, isSynchronized);
482                         if (assemblyAttributes != null)
483                                 foreach (CustomAttributeBuilder cb in assemblyAttributes) {
484                                         ab.SetCustomAttribute (cb);
485                                 }
486                         return ab;
487                 }
488
489                 // NET 3.5 method
490                 public AssemblyBuilder DefineDynamicAssembly (AssemblyName name, AssemblyBuilderAccess access, IEnumerable<CustomAttributeBuilder> assemblyAttributes) {
491                         return DefineDynamicAssembly (name, access, null, null, null, null, null, false, assemblyAttributes);
492                 }
493 #endif
494
495 #if NET_2_1
496                 // TODO: the last parameter is ignored for now
497                 public AssemblyBuilder DefineDynamicAssembly (AssemblyName name, AssemblyBuilderAccess access, bool emitSymbolInfo)
498                 {
499                         return DefineDynamicAssembly (name, access, null, null, null, null, null, false);
500                 }
501 #endif
502
503                 internal AssemblyBuilder DefineInternalDynamicAssembly (AssemblyName name, AssemblyBuilderAccess access)
504                 {
505                         return new AssemblyBuilder (name, null, access, true);
506                 }
507
508                 //
509                 // AppDomain.DoCallBack works because AppDomain is a MarshalByRefObject
510                 // so, when you call AppDomain.DoCallBack, that's a remote call
511                 //
512                 public void DoCallBack (CrossAppDomainDelegate callBackDelegate)
513                 {
514                         if (callBackDelegate != null)
515                                 callBackDelegate ();
516                 }
517
518                 public int ExecuteAssembly (string assemblyFile)
519                 {
520                         return ExecuteAssembly (assemblyFile, null, null);
521                 }
522
523                 public int ExecuteAssembly (string assemblyFile, Evidence assemblySecurity)
524                 {
525                         return ExecuteAssembly (assemblyFile, assemblySecurity, null);
526                 }
527
528                 public int ExecuteAssembly (string assemblyFile, Evidence assemblySecurity, string[] args)
529                 {
530                         Assembly a = Assembly.LoadFrom (assemblyFile, assemblySecurity);
531                         return ExecuteAssemblyInternal (a, args);
532                 }
533
534                 public int ExecuteAssembly (string assemblyFile, Evidence assemblySecurity, string[] args, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
535                 {
536                         Assembly a = Assembly.LoadFrom (assemblyFile, assemblySecurity, hashValue, hashAlgorithm);
537                         return ExecuteAssemblyInternal (a, args);
538                 }
539
540                 int ExecuteAssemblyInternal (Assembly a, string[] args)
541                 {
542                         if (a.EntryPoint == null)
543 #if NET_2_0
544                                 throw new MissingMethodException ("Entry point not found in assembly '" + a.FullName + "'.");
545 #else
546                                 throw new COMException ("Unspecified error.", -2147467259);
547 #endif
548                         return ExecuteAssembly (a, args);
549                 }
550
551                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
552                 private extern int ExecuteAssembly (Assembly a, string[] args);
553                 
554                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
555                 private extern Assembly [] GetAssemblies (bool refOnly);
556
557                 public Assembly [] GetAssemblies ()
558                 {
559                         return GetAssemblies (false);
560                 }
561
562                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
563                 public extern object GetData (string name);
564
565                 public new Type GetType()
566                 {
567                         return base.GetType ();
568                 }
569
570                 public override object InitializeLifetimeService ()
571                 {
572                         return null;
573                 }
574
575                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
576                 internal extern Assembly LoadAssembly (string assemblyRef, Evidence securityEvidence, bool refOnly);
577
578                 public Assembly Load (AssemblyName assemblyRef)
579                 {
580                         return Load (assemblyRef, null);
581                 }
582
583                 internal Assembly LoadSatellite (AssemblyName assemblyRef)
584                 {
585                         if (assemblyRef == null)
586                                 throw new ArgumentNullException ("assemblyRef");
587
588                         Assembly result = LoadAssembly (assemblyRef.FullName, null, false);
589                         if (result == null)
590                                 throw new FileNotFoundException (null, assemblyRef.Name);
591                         return result;
592                 }
593
594                 public Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
595                 {
596                         if (assemblyRef == null)
597                                 throw new ArgumentNullException ("assemblyRef");
598
599                         if (assemblyRef.Name == null || assemblyRef.Name.Length == 0) {
600                                 if (assemblyRef.CodeBase != null)
601                                         return Assembly.LoadFrom (assemblyRef.CodeBase, assemblySecurity);
602                                 else
603                                         throw new ArgumentException (Locale.GetText ("assemblyRef.Name cannot be empty."), "assemblyRef");
604                         }
605
606                         Assembly assembly = LoadAssembly (assemblyRef.FullName, assemblySecurity, false);
607                         if (assembly != null)
608                                 return assembly;
609
610                         if (assemblyRef.CodeBase == null)
611                                 throw new FileNotFoundException (null, assemblyRef.Name);
612
613                         string cb = assemblyRef.CodeBase;
614                         if (cb.ToLower (CultureInfo.InvariantCulture).StartsWith ("file://"))
615                                 cb = new Mono.Security.Uri (cb).LocalPath;
616
617                         try {
618                                 assembly = Assembly.LoadFrom (cb, assemblySecurity);
619                         } catch {
620                                 throw new FileNotFoundException (null, assemblyRef.Name);
621                         }
622                         AssemblyName aname = assembly.GetName ();
623                         // Name, version, culture, publickeytoken. Anything else?
624                         if (assemblyRef.Name != aname.Name)
625                                 throw new FileNotFoundException (null, assemblyRef.Name);
626
627                         if (assemblyRef.Version != new Version () && assemblyRef.Version != aname.Version)
628                                 throw new FileNotFoundException (null, assemblyRef.Name);
629
630                         if (assemblyRef.CultureInfo != null && assemblyRef.CultureInfo.Equals (aname))
631                                 throw new FileNotFoundException (null, assemblyRef.Name);
632
633                         byte [] pt = assemblyRef.GetPublicKeyToken ();
634                         if (pt != null) {
635                                 byte [] loaded_pt = aname.GetPublicKeyToken ();
636                                 if (loaded_pt == null || (pt.Length != loaded_pt.Length))
637                                         throw new FileNotFoundException (null, assemblyRef.Name);
638                                 for (int i = pt.Length - 1; i >= 0; i--)
639                                         if (loaded_pt [i] != pt [i])
640                                                 throw new FileNotFoundException (null, assemblyRef.Name);
641                         }
642                         return assembly;
643                 }
644
645                 public Assembly Load (string assemblyString)
646                 {
647                         return Load (assemblyString, null, false);
648                 }
649
650                 public Assembly Load (string assemblyString, Evidence assemblySecurity)
651                 {
652                         return Load (assemblyString, assemblySecurity, false);
653                 }
654                 
655                 internal Assembly Load (string assemblyString, Evidence assemblySecurity, bool refonly)
656                 {
657                         if (assemblyString == null)
658                                 throw new ArgumentNullException ("assemblyString");
659                                 
660                         if (assemblyString.Length == 0)
661                                 throw new ArgumentException ("assemblyString cannot have zero length");
662
663                         Assembly assembly = LoadAssembly (assemblyString, assemblySecurity, refonly);
664                         if (assembly == null)
665                                 throw new FileNotFoundException (null, assemblyString);
666                         return assembly;
667                 }
668
669                 public Assembly Load (byte[] rawAssembly)
670                 {
671                         return Load (rawAssembly, null, null);
672                 }
673
674                 public Assembly Load (byte[] rawAssembly, byte[] rawSymbolStore)
675                 {
676                         return Load (rawAssembly, rawSymbolStore, null);
677                 }
678
679                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
680                 internal extern Assembly LoadAssemblyRaw (byte[] rawAssembly, byte[] rawSymbolStore, Evidence securityEvidence, bool refonly);
681
682                 public Assembly Load (byte[] rawAssembly, byte[] rawSymbolStore, Evidence securityEvidence)
683                 {
684                         return Load (rawAssembly, rawSymbolStore, securityEvidence, false);
685                 }
686
687                 internal Assembly Load (byte [] rawAssembly, byte [] rawSymbolStore, Evidence securityEvidence, bool refonly)
688                 {
689                         if (rawAssembly == null)
690                                 throw new ArgumentNullException ("rawAssembly");
691
692                         Assembly assembly = LoadAssemblyRaw (rawAssembly, rawSymbolStore, securityEvidence, refonly);
693                         assembly.FromByteArray = true;
694                         return assembly;
695                 }
696
697                 [SecurityPermission (SecurityAction.Demand, ControlPolicy = true)]
698                 public void SetAppDomainPolicy (PolicyLevel domainPolicy)
699                 {
700                         if (domainPolicy == null)
701                                 throw new ArgumentNullException ("domainPolicy");
702                         if (_granted != null) {
703                                 throw new PolicyException (Locale.GetText (
704                                         "An AppDomain policy is already specified."));
705                         }
706                         if (IsFinalizingForUnload ())
707                                 throw new AppDomainUnloadedException ();
708
709                         PolicyStatement ps = domainPolicy.Resolve (_evidence);
710                         _granted = ps.PermissionSet;
711                 }
712
713 #if NET_2_0
714                 [Obsolete ("Use AppDomainSetup.SetCachePath")]
715 #endif
716                 [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
717                 public void SetCachePath (string path)
718                 {
719                         SetupInformationNoCopy.CachePath = path;
720                 }
721
722                 [SecurityPermission (SecurityAction.Demand, ControlPrincipal = true)]
723                 public void SetPrincipalPolicy (PrincipalPolicy policy)
724                 {
725                         if (IsFinalizingForUnload ())
726                                 throw new AppDomainUnloadedException ();
727
728                         _principalPolicy = policy;
729                         _principal = null;
730                 }
731
732 #if NET_2_0
733                 [Obsolete ("Use AppDomainSetup.ShadowCopyFiles")]
734 #endif
735                 [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
736                 public void SetShadowCopyFiles()
737                 {
738                         SetupInformationNoCopy.ShadowCopyFiles = "true";
739                 }
740
741 #if NET_2_0
742                 [Obsolete ("Use AppDomainSetup.ShadowCopyDirectories")]
743 #endif
744                 [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
745                 public void SetShadowCopyPath (string path)
746                 {
747                         SetupInformationNoCopy.ShadowCopyDirectories = path;
748                 }
749
750                 [SecurityPermission (SecurityAction.Demand, ControlPrincipal = true)]
751                 public void SetThreadPrincipal (IPrincipal principal)
752                 {
753                         if (principal == null)
754                                 throw new ArgumentNullException ("principal");
755                         if (_principal != null)
756                                 throw new PolicyException (Locale.GetText ("principal already present."));
757                         if (IsFinalizingForUnload ())
758                                 throw new AppDomainUnloadedException ();
759
760                         _principal = principal;
761                 }
762
763                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
764                 private static extern AppDomain InternalSetDomainByID (int domain_id);
765  
766                 // Changes the active domain and returns the old domain
767                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
768                 private static extern AppDomain InternalSetDomain (AppDomain context);
769
770                 // Notifies the runtime that this thread references 'domain'.
771                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
772                 internal static extern void InternalPushDomainRef (AppDomain domain);
773
774                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
775                 internal static extern void InternalPushDomainRefByID (int domain_id);
776
777                 // Undoes the effect of the last PushDomainRef call
778                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
779                 internal static extern void InternalPopDomainRef ();
780
781                 // Changes the active context and returns the old context
782                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
783                 internal static extern Context InternalSetContext (Context context);
784
785                 // Returns the current context
786                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
787                 internal static extern Context InternalGetContext ();
788
789                 // Returns the current context
790                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
791                 internal static extern Context InternalGetDefaultContext ();
792
793                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
794                 internal static extern string InternalGetProcessGuid (string newguid);
795
796                 // This method is handled specially by the runtime
797                 // It is the only managed method which is allowed to set the current
798                 // appdomain
799                 internal static object InvokeInDomain (AppDomain domain, MethodInfo method, object obj, object [] args)
800                 {
801                         AppDomain current = CurrentDomain;
802                         bool pushed = false;
803
804                         try {
805                                 Exception exc;
806                                 InternalPushDomainRef (domain);
807                                 pushed = true;
808                                 InternalSetDomain (domain);
809                                 object o = ((MonoMethod) method).InternalInvoke (obj, args, out exc);
810                                 if (exc != null)
811                                         throw exc;
812                                 return o;
813                         }
814                         finally {
815                                 InternalSetDomain (current);
816                                 if (pushed)
817                                         InternalPopDomainRef ();
818                         }
819                 }
820
821                 internal static object InvokeInDomainByID (int domain_id, MethodInfo method, object obj, object [] args)
822                 {
823                         AppDomain current = CurrentDomain;
824                         bool pushed = false;
825
826                         try {
827                                 Exception exc;
828                                 InternalPushDomainRefByID (domain_id);
829                                 pushed = true;
830                                 InternalSetDomainByID (domain_id);
831                                 object o = ((MonoMethod) method).InternalInvoke (obj, args, out exc);
832                                 if (exc != null)
833                                         throw exc;
834                                 return o;
835                         }
836                         finally {
837                                 InternalSetDomain (current);
838                                 if (pushed)
839                                         InternalPopDomainRef ();
840                         }
841                 }
842
843                 internal static String GetProcessGuid ()
844                 {
845                         if (_process_guid == null) {
846                                 _process_guid = InternalGetProcessGuid (Guid.NewGuid().ToString ());
847                         }
848                         return _process_guid;
849                 }
850
851                 public static AppDomain CreateDomain (string friendlyName)
852                 {
853                         return CreateDomain (friendlyName, null, null);
854                 }
855                 
856                 public static AppDomain CreateDomain (string friendlyName, Evidence securityInfo)
857                 {
858                         return CreateDomain (friendlyName, securityInfo, null);
859                 }
860
861                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
862                 private static extern AppDomain createDomain (string friendlyName, AppDomainSetup info);
863
864                 [MonoLimitationAttribute ("Currently it does not allow the setup in the other domain")]
865                 [SecurityPermission (SecurityAction.Demand, ControlAppDomain = true)]
866                 public static AppDomain CreateDomain (string friendlyName, Evidence securityInfo, AppDomainSetup info)
867                 {
868                         if (friendlyName == null)
869                                 throw new System.ArgumentNullException ("friendlyName");
870
871                         AppDomain def = AppDomain.DefaultDomain;
872                         if (info == null) {
873                                 // if null, get default domain's SetupInformation       
874                                 if (def == null)
875                                         info = new AppDomainSetup ();   // we're default!
876                                 else
877                                         info = def.SetupInformation;
878                         }
879                         else
880                                 info = new AppDomainSetup (info);       // copy
881
882                         // todo: allow setup in the other domain
883                         if (def != null) {
884                                 if (!info.Equals (def.SetupInformation)) {
885                                         // If not specified use default domain's app base.
886                                         if (info.ApplicationBase == null)
887                                                 info.ApplicationBase = def.SetupInformation.ApplicationBase;
888                                         if (info.ConfigurationFile == null)
889                                                 info.ConfigurationFile = Path.GetFileName (def.SetupInformation.ConfigurationFile);
890                                 }
891                         } else if (info.ConfigurationFile == null)
892                                 info.ConfigurationFile = "[I don't have a config file]";
893
894                         AppDomain ad = (AppDomain) RemotingServices.GetDomainProxy (createDomain (friendlyName, info));
895                         if (securityInfo == null) {
896                                 // get default domain's Evidence (unless we're are the default!)
897                                 if (def == null)
898                                         ad._evidence = null;            // we'll get them later (GetEntryAssembly)
899                                 else
900                                         ad._evidence = def.Evidence;    // new (shallow) copy
901                         }
902                         else
903                                 ad._evidence = new Evidence (securityInfo);     // copy
904
905 #if NET_2_0
906                         if (info.AppDomainInitializer != null) {
907                                 if (!info.AppDomainInitializer.Method.IsStatic)
908                                         throw new ArgumentException ("Non-static methods cannot be invoked as an appdomain initializer");
909
910                                 Loader loader = new Loader (
911                                         info.AppDomainInitializer.Method.DeclaringType.Assembly.Location);
912                                 ad.DoCallBack (loader.Load);
913
914                                 Initializer initializer = new Initializer (
915                                         info.AppDomainInitializer,
916                                         info.AppDomainInitializerArguments);
917                                 ad.DoCallBack (initializer.Initialize);
918                         }
919 #endif
920
921                         return ad;
922                 }
923
924 #if NET_2_0
925                 [Serializable]
926                 class Loader {
927
928                         string assembly;
929
930                         public Loader (string assembly)
931                         {
932                                 this.assembly = assembly;
933                         }
934
935                         public void Load ()
936                         {
937                                 Assembly.LoadFrom (assembly);
938                         }
939                 }
940
941                 [Serializable]
942                 class Initializer {
943
944                         AppDomainInitializer initializer;
945                         string [] arguments;
946
947                         public Initializer (AppDomainInitializer initializer, string [] arguments)
948                         {
949                                 this.initializer = initializer;
950                                 this.arguments = arguments;
951                         }
952
953                         public void Initialize ()
954                         {
955                                 initializer (arguments);
956                         }
957                 }
958 #endif
959
960                 public static AppDomain CreateDomain (string friendlyName, Evidence securityInfo,string appBasePath,
961                                                       string appRelativeSearchPath, bool shadowCopyFiles)
962                 {
963                         return CreateDomain (friendlyName, securityInfo, CreateDomainSetup (appBasePath, appRelativeSearchPath, shadowCopyFiles));
964                 }
965
966                 static AppDomainSetup CreateDomainSetup (string appBasePath, string appRelativeSearchPath, bool shadowCopyFiles)
967                 {
968                         AppDomainSetup info = new AppDomainSetup ();
969
970                         info.ApplicationBase = appBasePath;
971                         info.PrivateBinPath = appRelativeSearchPath;
972
973                         if (shadowCopyFiles)
974                                 info.ShadowCopyFiles = "true";
975                         else
976 #if NET_2_0
977                                 info.ShadowCopyFiles = "false";
978 #else
979                                 info.ShadowCopyFiles = null;
980 #endif
981
982
983                         return info;
984                 }
985
986 #if NET_2_0
987                 [MonoTODO]
988                 public static AppDomain CreateDomain (string friendlyName, Evidence securityInfo, AppDomainSetup info,
989                                                       PermissionSet grantSet, params StrongName [] fullTrustAssemblies)
990                 {
991                         throw new NotImplementedException ();
992                 }
993 #endif
994
995                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
996                 private static extern bool InternalIsFinalizingForUnload (int domain_id);
997
998                 public bool IsFinalizingForUnload()
999                 {
1000                         return InternalIsFinalizingForUnload (getDomainID ());
1001                 }
1002
1003                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
1004                 static extern void InternalUnload (int domain_id);
1005
1006                 // We do this because if the domain is a transparant proxy this
1007                 // will still return the correct domain id.
1008                 private int getDomainID ()
1009                 {
1010                         return Thread.GetDomainID ();
1011                 }
1012
1013                 [SecurityPermission (SecurityAction.Demand, ControlAppDomain = true)]
1014 #if NET_2_0
1015                 [ReliabilityContractAttribute (Consistency.MayCorruptAppDomain, Cer.MayFail)]
1016 #endif
1017                 public static void Unload (AppDomain domain)
1018                 {
1019                         if (domain == null)
1020                                 throw new ArgumentNullException ("domain");
1021
1022                         InternalUnload (domain.getDomainID());
1023                 }
1024
1025                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
1026                 [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
1027                 public extern void SetData (string name, object data);
1028
1029 #if NET_2_0
1030                 [MonoTODO]
1031                 public void SetData (string name, object data, IPermission permission)
1032                 {
1033                         throw new NotImplementedException ();
1034                 }
1035 #endif
1036
1037 #if NET_2_0
1038                 [Obsolete ("Use AppDomainSetup.DynamicBase")]
1039 #endif
1040                 [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
1041                 public void SetDynamicBase (string path)
1042                 {
1043                         SetupInformationNoCopy.DynamicBase = path;
1044                 }
1045
1046 #if NET_2_0
1047                 [Obsolete ("AppDomain.GetCurrentThreadId has been deprecated"
1048                         + " because it does not provide a stable Id when managed"
1049                         + " threads are running on fibers (aka lightweight"
1050                         + " threads). To get a stable identifier for a managed"
1051                         + " thread, use the ManagedThreadId property on Thread.'")]
1052 #endif
1053                 public static int GetCurrentThreadId ()
1054                 {
1055                         return Thread.CurrentThreadId;
1056                 }
1057
1058                 public override string ToString ()
1059                 {
1060                         return getFriendlyName ();
1061                 }
1062
1063                 private static void ValidateAssemblyName (string name)
1064                 {
1065                         if (name == null || name.Length == 0)
1066                                 throw new ArgumentException ("The Name of " +
1067                                         "AssemblyName cannot be null or a " +
1068                                         "zero-length string.");
1069
1070                         bool isValid = true;
1071
1072                         for (int i = 0; i < name.Length; i++) {
1073                                 char c = name [i];
1074
1075                                 // do not allow leading whitespace
1076                                 if (i == 0 && char.IsWhiteSpace (c)) {
1077                                         isValid = false;
1078                                         break;
1079                                 }
1080
1081                                 // do not allow /,\ or : in name
1082                                 if (c == '/' || c == '\\' || c == ':') {
1083                                         isValid = false;
1084                                         break;
1085                                 }
1086                         }
1087
1088                         if (!isValid)
1089                                 throw new ArgumentException ("The Name of " +
1090                                         "AssemblyName cannot start with " +
1091                                         "whitespace, or contain '/', '\\' " +
1092                                         " or ':'.");
1093                 }
1094
1095                 // The following methods are called from the runtime. Don't change signatures.
1096 #pragma warning disable 169             
1097                 private void DoAssemblyLoad (Assembly assembly)
1098                 {
1099                         if (AssemblyLoad == null)
1100                                 return;
1101
1102                         AssemblyLoad (this, new AssemblyLoadEventArgs (assembly));
1103                 }
1104
1105                 private Assembly DoAssemblyResolve (string name, bool refonly)
1106                 {
1107                         ResolveEventHandler del;
1108 #if NET_2_0
1109                         if (refonly)
1110                                 del = ReflectionOnlyAssemblyResolve;
1111                         else
1112                                 del = AssemblyResolve;
1113 #else
1114                         del = AssemblyResolve;
1115 #endif
1116                         if (del == null)
1117                                 return null;
1118                         
1119                         /* Prevent infinite recursion */
1120                         Hashtable ht;
1121                         if (refonly) {
1122                                 ht = assembly_resolve_in_progress_refonly;
1123                                 if (ht == null) {
1124                                         ht = new Hashtable ();
1125                                         assembly_resolve_in_progress_refonly = ht;
1126                                 }
1127                         } else {
1128                                 ht = assembly_resolve_in_progress;
1129                                 if (ht == null) {
1130                                         ht = new Hashtable ();
1131                                         assembly_resolve_in_progress = ht;
1132                                 }
1133                         }
1134
1135                         string s = (string) ht [name];
1136                         if (s != null)
1137                                 return null;
1138                         ht [name] = name;
1139                         try {
1140                                 Delegate[] invocation_list = del.GetInvocationList ();
1141
1142                                 foreach (Delegate eh in invocation_list) {
1143                                         ResolveEventHandler handler = (ResolveEventHandler) eh;
1144                                         Assembly assembly = handler (this, new ResolveEventArgs (name));
1145                                         if (assembly != null)
1146                                                 return assembly;
1147                                 }
1148                                 return null;
1149                         }
1150                         finally {
1151                                 ht.Remove (name);
1152                         }
1153                 }
1154
1155                 internal Assembly DoTypeResolve (Object name_or_tb)
1156                 {
1157                         if (TypeResolve == null)
1158                                 return null;
1159
1160                         string name;
1161
1162                         if (name_or_tb is TypeBuilder)
1163                                 name = ((TypeBuilder) name_or_tb).FullName;
1164                         else
1165                                 name = (string) name_or_tb;
1166
1167                         /* Prevent infinite recursion */
1168                         Hashtable ht = type_resolve_in_progress;
1169                         if (ht == null) {
1170                                 ht = new Hashtable ();
1171                                 type_resolve_in_progress = ht;
1172                         }
1173
1174                         if (ht.Contains (name))
1175                                 return null;
1176                         else
1177                                 ht [name] = name;
1178
1179                         try {
1180                                 foreach (Delegate d in TypeResolve.GetInvocationList ()) {
1181                                         ResolveEventHandler eh = (ResolveEventHandler) d;
1182                                         Assembly assembly = eh (this, new ResolveEventArgs (name));
1183                                         if (assembly != null)
1184                                                 return assembly;
1185                                 }
1186                                 return null;
1187                         }
1188                         finally {
1189                                 ht.Remove (name);
1190                         }
1191                 }
1192
1193                 private void DoDomainUnload ()
1194                 {
1195                         if (DomainUnload != null)
1196                                 DomainUnload(this, null);
1197                 }
1198
1199                 internal byte[] GetMarshalledDomainObjRef ()
1200                 {
1201                         ObjRef oref = RemotingServices.Marshal (AppDomain.CurrentDomain, null, typeof (AppDomain));
1202                         return CADSerializer.SerializeObject (oref).GetBuffer();
1203                 }
1204
1205                 internal void ProcessMessageInDomain (byte[] arrRequest, CADMethodCallMessage cadMsg,
1206                                                       out byte[] arrResponse, out CADMethodReturnMessage cadMrm)
1207                 {
1208                         IMessage reqDomMsg;
1209
1210                         if (null != arrRequest)
1211                                 reqDomMsg = CADSerializer.DeserializeMessage (new MemoryStream(arrRequest), null);
1212                         else
1213                                 reqDomMsg = new MethodCall (cadMsg);
1214
1215                         IMessage retDomMsg = ChannelServices.SyncDispatchMessage (reqDomMsg);
1216
1217                         cadMrm = CADMethodReturnMessage.Create (retDomMsg);
1218                         if (null == cadMrm) {
1219                                 arrResponse = CADSerializer.SerializeMessage (retDomMsg).GetBuffer();
1220                         } 
1221                         else
1222                                 arrResponse = null;
1223                 }
1224 #pragma warning restore 169
1225
1226                 // End of methods called from the runtime
1227                 
1228 #if BOOTSTRAP_WITH_OLDLIB
1229                 // older MCS/corlib returns:
1230                 // _AppDomain.cs(138) error CS0592: Attribute 'SecurityPermission' is not valid on this declaration type.
1231                 // It is valid on 'assembly' 'class' 'constructor' 'method' 'struct'  declarations only.
1232                 public event AssemblyLoadEventHandler AssemblyLoad;
1233
1234                 public event ResolveEventHandler AssemblyResolve;
1235
1236                 public event EventHandler DomainUnload;
1237
1238                 public event EventHandler ProcessExit;
1239
1240                 public event ResolveEventHandler ResourceResolve;
1241
1242                 public event ResolveEventHandler TypeResolve;
1243
1244                 public event UnhandledExceptionEventHandler UnhandledException;
1245 #else
1246                 [method: SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
1247                 public event AssemblyLoadEventHandler AssemblyLoad;
1248
1249                 [method: SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
1250                 public event ResolveEventHandler AssemblyResolve;
1251
1252                 [method: SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
1253                 public event EventHandler DomainUnload;
1254
1255                 [method: SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
1256                 public event EventHandler ProcessExit;
1257
1258                 [method: SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
1259                 public event ResolveEventHandler ResourceResolve;
1260
1261                 [method: SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
1262                 public event ResolveEventHandler TypeResolve;
1263
1264                 [method: SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
1265                 public event UnhandledExceptionEventHandler UnhandledException;
1266 #endif
1267
1268 #if NET_4_0
1269                 [MonoTODO]
1270                 public bool IsHomogenous {
1271                         get { return false; }
1272                 }
1273 #endif
1274
1275 #if NET_2_0
1276
1277                 public event ResolveEventHandler ReflectionOnlyAssemblyResolve;
1278
1279         #pragma warning disable 649
1280                 private ActivationContext _activation;
1281                 private ApplicationIdentity _applicationIdentity;
1282                 private AppDomainManager _domain_manager;
1283         #pragma warning restore 649
1284
1285                 // properties
1286
1287                 public ActivationContext ActivationContext {
1288                         get { return _activation; }
1289                 }
1290
1291                 public ApplicationIdentity ApplicationIdentity {
1292                         get { return _applicationIdentity; }
1293                 }
1294
1295                 // default is null
1296                 public AppDomainManager DomainManager {
1297                         get { return _domain_manager; }
1298                 }
1299
1300                 public int Id {
1301                         [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
1302                         get { return getDomainID (); }
1303                 }
1304
1305                 // methods
1306
1307                 [MonoTODO ("This routine only returns the parameter currently")]
1308                 [ComVisible (false)]
1309                 public string ApplyPolicy (string assemblyName)
1310                 {
1311                         if (assemblyName == null)
1312                                 throw new ArgumentNullException ("assemblyName");
1313                         if (assemblyName.Length == 0) // String.Empty
1314                                 throw new ArgumentException ("assemblyName");
1315                         return assemblyName;
1316                 }
1317
1318                 // static methods
1319
1320                 public static AppDomain CreateDomain (string friendlyName, Evidence securityInfo, string appBasePath,
1321                         string appRelativeSearchPath, bool shadowCopyFiles, AppDomainInitializer adInit, string[] adInitArgs)
1322                 {
1323                         AppDomainSetup info = CreateDomainSetup (appBasePath, appRelativeSearchPath, shadowCopyFiles);
1324
1325                         info.AppDomainInitializerArguments = adInitArgs;
1326                         info.AppDomainInitializer = adInit;
1327
1328                         return CreateDomain (friendlyName, securityInfo, info);
1329                 }
1330
1331                 public int ExecuteAssemblyByName (string assemblyName)
1332                 {
1333                         return ExecuteAssemblyByName (assemblyName, null, null);
1334                 }
1335
1336                 public int ExecuteAssemblyByName (string assemblyName, Evidence assemblySecurity)
1337                 {
1338                         return ExecuteAssemblyByName (assemblyName, assemblySecurity, null);
1339                 }
1340
1341                 public int ExecuteAssemblyByName (string assemblyName, Evidence assemblySecurity, params string[] args)
1342                 {
1343                         Assembly a = Assembly.Load (assemblyName, assemblySecurity);
1344
1345                         return ExecuteAssemblyInternal (a, args);
1346                 }
1347
1348                 public int ExecuteAssemblyByName (AssemblyName assemblyName, Evidence assemblySecurity, params string[] args)
1349                 {
1350                         Assembly a = Assembly.Load (assemblyName, assemblySecurity);
1351
1352                         return ExecuteAssemblyInternal (a, args);
1353                 }
1354
1355                 public bool IsDefaultAppDomain ()
1356                 {
1357                         return Object.ReferenceEquals (this, DefaultDomain);
1358                 }
1359
1360                 public Assembly[] ReflectionOnlyGetAssemblies ()
1361                 {
1362                         return GetAssemblies (true);
1363                 }
1364 #endif
1365
1366 #if NET_1_1
1367                 void _AppDomain.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1368                 {
1369                         throw new NotImplementedException ();
1370                 }
1371
1372                 void _AppDomain.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1373                 {
1374                         throw new NotImplementedException ();
1375                 }
1376
1377                 void _AppDomain.GetTypeInfoCount (out uint pcTInfo)
1378                 {
1379                         throw new NotImplementedException ();
1380                 }
1381
1382                 void _AppDomain.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
1383                         IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1384                 {
1385                         throw new NotImplementedException ();
1386                 }
1387 #endif
1388         }
1389 }