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