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)
10 // Sebastien Pouliot (sebastien@ximian.com)
12 // (C) 2001, 2002 Ximian, Inc. http://www.ximian.com
13 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
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.
35 using System.Collections;
36 using System.Globalization;
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;
54 using System.Runtime.ConstrainedExecution;
62 [ClassInterface(ClassInterfaceType.None)]
63 public sealed class AppDomain : MarshalByRefObject , _AppDomain , IEvidenceFactory
65 IntPtr _mono_app_domain;
66 static string _process_guid;
69 static Hashtable type_resolve_in_progress;
72 static Hashtable assembly_resolve_in_progress;
75 private Evidence _evidence;
76 private PermissionSet _granted;
79 private PrincipalPolicy _principalPolicy;
82 private static IPrincipal _principal;
88 [MethodImplAttribute (MethodImplOptions.InternalCall)]
89 private extern AppDomainSetup getSetup ();
91 AppDomainSetup SetupInformationNoCopy {
92 get { return getSetup (); }
95 public AppDomainSetup SetupInformation {
97 AppDomainSetup setup = getSetup ();
98 return new AppDomainSetup (setup);
102 public string BaseDirectory {
104 string path = SetupInformationNoCopy.ApplicationBase;
105 if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
106 // we cannot divulge local file informations
107 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
113 public string RelativeSearchPath {
115 string path = SetupInformationNoCopy.PrivateBinPath;
116 if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
117 // we cannot divulge local file informations
118 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
124 public string DynamicDirectory {
126 AppDomainSetup setup = SetupInformationNoCopy;
127 if (setup.DynamicBase == null)
130 string path = Path.Combine (setup.DynamicBase, setup.ApplicationName);
131 if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
132 // we cannot divulge local file informations
133 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
139 public bool ShadowCopyFiles {
141 return (SetupInformationNoCopy.ShadowCopyFiles == "true");
145 [MethodImplAttribute (MethodImplOptions.InternalCall)]
146 private extern string getFriendlyName ();
148 public string FriendlyName {
150 return getFriendlyName ();
154 public Evidence Evidence {
156 // if the host (runtime) hasn't provided it's own evidence...
157 if (_evidence == null) {
158 // ... we will provide our own
160 // the executed assembly from the "default" appdomain
161 // or null if we're not in the default appdomain
162 Assembly a = Assembly.GetEntryAssembly ();
164 _evidence = AppDomain.DefaultDomain.Evidence;
166 _evidence = Evidence.GetDefaultHostEvidence (a);
169 return new Evidence (_evidence); // return a copy
173 internal IPrincipal DefaultPrincipal {
175 if (_principal == null) {
176 switch (_principalPolicy) {
177 case PrincipalPolicy.UnauthenticatedPrincipal:
178 _principal = new GenericPrincipal (
179 new GenericIdentity (String.Empty, String.Empty), null);
181 case PrincipalPolicy.WindowsPrincipal:
182 _principal = new WindowsPrincipal (WindowsIdentity.GetCurrent ());
190 // for AppDomain there is only an allowed (i.e. granted) set
191 // http://msdn.microsoft.com/library/en-us/cpguide/html/cpcondetermininggrantedpermissions.asp
192 internal PermissionSet GrantedPermissionSet {
193 get { return _granted; }
196 [MethodImplAttribute (MethodImplOptions.InternalCall)]
197 private static extern AppDomain getCurDomain ();
199 public static AppDomain CurrentDomain {
201 return getCurDomain ();
205 [MethodImplAttribute (MethodImplOptions.InternalCall)]
206 private static extern AppDomain getRootDomain ();
208 internal static AppDomain DefaultDomain {
210 return getRootDomain ();
217 [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
218 public void AppendPrivatePath (string path)
220 if (path == null || path.Length == 0)
223 AppDomainSetup setup = SetupInformationNoCopy;
225 string pp = setup.PrivateBinPath;
226 if (pp == null || pp.Length == 0) {
227 setup.PrivateBinPath = path;
232 if (pp [pp.Length - 1] != Path.PathSeparator)
233 pp += Path.PathSeparator;
235 setup.PrivateBinPath = pp + path;
241 [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
242 public void ClearPrivatePath ()
244 SetupInformationNoCopy.PrivateBinPath = String.Empty;
247 [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
248 public void ClearShadowCopyPath ()
250 SetupInformationNoCopy.ShadowCopyDirectories = String.Empty;
253 public ObjectHandle CreateComInstanceFrom (string assemblyName, string typeName)
255 return Activator.CreateComInstanceFrom (assemblyName, typeName);
259 public ObjectHandle CreateComInstanceFrom (string assemblyName, string typeName,
260 byte [] hashValue, AssemblyHashAlgorithm hashAlgorithm)
262 return Activator.CreateComInstanceFrom (assemblyName, typeName, hashValue ,hashAlgorithm);
266 public ObjectHandle CreateInstance (string assemblyName, string typeName)
268 if (assemblyName == null)
269 throw new ArgumentNullException ("assemblyName");
271 return Activator.CreateInstance (assemblyName, typeName);
274 public ObjectHandle CreateInstance (string assemblyName, string typeName, object[] activationAttributes)
276 if (assemblyName == null)
277 throw new ArgumentNullException ("assemblyName");
279 return Activator.CreateInstance (assemblyName, typeName, activationAttributes);
282 public ObjectHandle CreateInstance (string assemblyName, string typeName, bool ignoreCase, BindingFlags bindingAttr,
283 Binder binder, object[] args, CultureInfo culture, object[] activationAttributes,
284 Evidence securityAttributes)
286 if (assemblyName == null)
287 throw new ArgumentNullException ("assemblyName");
289 return Activator.CreateInstance (assemblyName, typeName, ignoreCase, bindingAttr, binder, args,
290 culture, activationAttributes, securityAttributes);
293 public object CreateInstanceAndUnwrap (string assemblyName, string typeName)
295 ObjectHandle oh = CreateInstance (assemblyName, typeName);
296 return (oh != null) ? oh.Unwrap () : null;
299 public object CreateInstanceAndUnwrap (string assemblyName, string typeName, object [] activationAttributes)
301 ObjectHandle oh = CreateInstance (assemblyName, typeName, activationAttributes);
302 return (oh != null) ? oh.Unwrap () : null;
305 public object CreateInstanceAndUnwrap (string assemblyName, string typeName, bool ignoreCase,
306 BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture,
307 object[] activationAttributes, Evidence securityAttributes)
309 ObjectHandle oh = CreateInstance (assemblyName, typeName, ignoreCase, bindingAttr, binder, args,
310 culture, activationAttributes, securityAttributes);
311 return (oh != null) ? oh.Unwrap () : null;
314 public ObjectHandle CreateInstanceFrom (string assemblyName, string typeName)
316 if (assemblyName == null)
317 throw new ArgumentNullException ("assemblyName");
319 return Activator.CreateInstanceFrom (assemblyName, typeName);
322 public ObjectHandle CreateInstanceFrom (string assemblyName, string typeName, object[] activationAttributes)
324 if (assemblyName == null)
325 throw new ArgumentNullException ("assemblyName");
327 return Activator.CreateInstanceFrom (assemblyName, typeName, activationAttributes);
330 public ObjectHandle CreateInstanceFrom (string assemblyName, string typeName, bool ignoreCase,
331 BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture,
332 object[] activationAttributes, Evidence securityAttributes)
334 if (assemblyName == null)
335 throw new ArgumentNullException ("assemblyName");
337 return Activator.CreateInstanceFrom (assemblyName, typeName, ignoreCase, bindingAttr, binder, args,
338 culture, activationAttributes, securityAttributes);
341 public object CreateInstanceFromAndUnwrap (string assemblyName, string typeName)
343 ObjectHandle oh = CreateInstanceFrom (assemblyName, typeName);
344 return (oh != null) ? oh.Unwrap () : null;
347 public object CreateInstanceFromAndUnwrap (string assemblyName, string typeName, object [] activationAttributes)
349 ObjectHandle oh = CreateInstanceFrom (assemblyName, typeName, activationAttributes);
350 return (oh != null) ? oh.Unwrap () : null;
353 public object CreateInstanceFromAndUnwrap (string assemblyName, string typeName, bool ignoreCase,
354 BindingFlags bindingAttr, Binder binder, object[] args,
355 CultureInfo culture, object[] activationAttributes,
356 Evidence securityAttributes)
358 ObjectHandle oh = CreateInstanceFrom (assemblyName, typeName, ignoreCase, bindingAttr, binder, args,
359 culture, activationAttributes, securityAttributes);
361 return (oh != null) ? oh.Unwrap () : null;
364 public AssemblyBuilder DefineDynamicAssembly (AssemblyName name, AssemblyBuilderAccess access)
366 return DefineDynamicAssembly (name, access, null, null, null, null, null, false);
369 public AssemblyBuilder DefineDynamicAssembly (AssemblyName name, AssemblyBuilderAccess access, Evidence evidence)
371 return DefineDynamicAssembly (name, access, null, evidence, null, null, null, false);
374 public AssemblyBuilder DefineDynamicAssembly (AssemblyName name, AssemblyBuilderAccess access, string dir)
376 return DefineDynamicAssembly (name, access, dir, null, null, null, null, false);
379 public AssemblyBuilder DefineDynamicAssembly (AssemblyName name, AssemblyBuilderAccess access, string dir,
382 return DefineDynamicAssembly (name, access, dir, evidence, null, null, null, false);
385 public AssemblyBuilder DefineDynamicAssembly (AssemblyName name, AssemblyBuilderAccess access,
386 PermissionSet requiredPermissions,
387 PermissionSet optionalPermissions,
388 PermissionSet refusedPermissions)
390 return DefineDynamicAssembly (name, access, null, null, requiredPermissions, optionalPermissions,
391 refusedPermissions, false);
394 public AssemblyBuilder DefineDynamicAssembly (AssemblyName name, AssemblyBuilderAccess access, Evidence evidence,
395 PermissionSet requiredPermissions,
396 PermissionSet optionalPermissions,
397 PermissionSet refusedPermissions)
399 return DefineDynamicAssembly (name, access, null, evidence, requiredPermissions, optionalPermissions,
400 refusedPermissions, false);
403 public AssemblyBuilder DefineDynamicAssembly (AssemblyName name, AssemblyBuilderAccess access, string dir,
404 PermissionSet requiredPermissions,
405 PermissionSet optionalPermissions,
406 PermissionSet refusedPermissions)
408 return DefineDynamicAssembly (name, access, dir, null, requiredPermissions, optionalPermissions,
409 refusedPermissions, false);
412 public AssemblyBuilder DefineDynamicAssembly (AssemblyName name, AssemblyBuilderAccess access, string dir,
414 PermissionSet requiredPermissions,
415 PermissionSet optionalPermissions,
416 PermissionSet refusedPermissions)
418 return DefineDynamicAssembly (name, access, dir, evidence, requiredPermissions, optionalPermissions,
419 refusedPermissions, false);
422 public AssemblyBuilder DefineDynamicAssembly (AssemblyName name, AssemblyBuilderAccess access, string dir,
424 PermissionSet requiredPermissions,
425 PermissionSet optionalPermissions,
426 PermissionSet refusedPermissions, bool isSynchronized)
428 // FIXME: examine all other parameters
430 AssemblyBuilder ab = new AssemblyBuilder (name, dir, access, false);
431 ab.AddPermissionRequests (requiredPermissions, optionalPermissions, refusedPermissions);
435 internal AssemblyBuilder DefineInternalDynamicAssembly (AssemblyName name, AssemblyBuilderAccess access)
437 return new AssemblyBuilder (name, null, access, true);
440 public void DoCallBack (CrossAppDomainDelegate theDelegate)
442 if (theDelegate != null)
446 public int ExecuteAssembly (string assemblyFile)
448 return ExecuteAssembly (assemblyFile, null, null);
451 public int ExecuteAssembly (string assemblyFile, Evidence assemblySecurity)
453 return ExecuteAssembly (assemblyFile, assemblySecurity, null);
456 [MethodImplAttribute (MethodImplOptions.InternalCall)]
457 public extern int ExecuteAssembly (string assemblyFile, Evidence assemblySecurity, string[] args);
459 [MonoTODO ("No support for ExecuteAssembly")]
460 public int ExecuteAssembly (string assemblyFile, Evidence assemblySecurity, string[] args, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
462 throw new NotImplementedException ();
465 [MethodImplAttribute (MethodImplOptions.InternalCall)]
466 private extern Assembly [] GetAssemblies (bool refOnly);
468 public Assembly [] GetAssemblies ()
470 return GetAssemblies (false);
473 [MethodImplAttribute (MethodImplOptions.InternalCall)]
474 public extern object GetData (string name);
476 public new Type GetType()
478 return base.GetType ();
481 public override object InitializeLifetimeService ()
486 [MethodImplAttribute (MethodImplOptions.InternalCall)]
487 internal extern Assembly LoadAssembly (string assemblyRef, Evidence securityEvidence, bool refOnly);
489 public Assembly Load (AssemblyName assemblyRef)
491 return Load (assemblyRef, null);
494 public Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
496 if (assemblyRef == null)
497 throw new ArgumentNullException ("assemblyRef");
499 if (assemblyRef.Name == null || assemblyRef.Name.Length == 0) {
500 if (assemblyRef.CodeBase != null)
501 return Assembly.LoadFrom (assemblyRef.CodeBase, assemblySecurity);
503 throw new ArgumentException (Locale.GetText ("assemblyRef.Name cannot be empty."), "assemblyRef");
506 return LoadAssembly (assemblyRef.FullName, assemblySecurity, false);
509 public Assembly Load (string assemblyString)
511 if (assemblyString == null)
512 throw new ArgumentNullException ("assemblyString");
514 return LoadAssembly (assemblyString, null, false);
517 public Assembly Load (string assemblyString, Evidence assemblySecurity)
519 return Load (assemblyString, assemblySecurity, false);
522 internal Assembly Load (string assemblyString, Evidence assemblySecurity, bool refonly)
524 if (assemblyString == null)
525 throw new ArgumentNullException ("assemblyString");
527 return LoadAssembly (assemblyString, assemblySecurity, refonly);
530 public Assembly Load (byte[] rawAssembly)
532 return Load (rawAssembly, null, null);
535 public Assembly Load (byte[] rawAssembly, byte[] rawSymbolStore)
537 return Load (rawAssembly, rawSymbolStore, null);
540 [MethodImplAttribute (MethodImplOptions.InternalCall)]
541 internal extern Assembly LoadAssemblyRaw (byte[] rawAssembly, byte[] rawSymbolStore, Evidence securityEvidence, bool refonly);
543 public Assembly Load (byte[] rawAssembly, byte[] rawSymbolStore, Evidence securityEvidence)
545 return Load (rawAssembly, rawSymbolStore, securityEvidence, false);
548 internal Assembly Load (byte [] rawAssembly, byte [] rawSymbolStore, Evidence securityEvidence, bool refonly)
550 if (rawAssembly == null)
551 throw new ArgumentNullException ("rawAssembly");
553 Assembly assembly = LoadAssemblyRaw (rawAssembly, rawSymbolStore, securityEvidence, refonly);
554 assembly.FromByteArray = true;
558 [SecurityPermission (SecurityAction.Demand, ControlPolicy = true)]
559 public void SetAppDomainPolicy (PolicyLevel domainPolicy)
561 if (domainPolicy == null)
562 throw new ArgumentNullException ("domainPolicy");
563 if (_granted != null) {
564 throw new PolicyException (Locale.GetText (
565 "An AppDomain policy is already specified."));
567 if (IsFinalizingForUnload ())
568 throw new AppDomainUnloadedException ();
570 PolicyStatement ps = domainPolicy.Resolve (_evidence);
571 _granted = ps.PermissionSet;
574 [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
575 public void SetCachePath (string path)
577 SetupInformationNoCopy.CachePath = path;
580 [SecurityPermission (SecurityAction.Demand, ControlPrincipal = true)]
581 public void SetPrincipalPolicy (PrincipalPolicy policy)
583 if (IsFinalizingForUnload ())
584 throw new AppDomainUnloadedException ();
586 _principalPolicy = policy;
590 [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
591 public void SetShadowCopyFiles()
593 SetupInformationNoCopy.ShadowCopyFiles = "true";
596 [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
597 public void SetShadowCopyPath (string path)
599 SetupInformationNoCopy.ShadowCopyDirectories = path;
602 [SecurityPermission (SecurityAction.Demand, ControlPrincipal = true)]
603 public void SetThreadPrincipal (IPrincipal principal)
605 if (principal == null)
606 throw new ArgumentNullException ("principal");
607 if (_principal != null)
608 throw new PolicyException (Locale.GetText ("principal already present."));
609 if (IsFinalizingForUnload ())
610 throw new AppDomainUnloadedException ();
612 _principal = principal;
615 [MethodImplAttribute (MethodImplOptions.InternalCall)]
616 private static extern AppDomain InternalSetDomainByID (int domain_id);
618 // Changes the active domain and returns the old domain
619 [MethodImplAttribute (MethodImplOptions.InternalCall)]
620 private static extern AppDomain InternalSetDomain (AppDomain context);
622 // Notifies the runtime that this thread references 'domain'.
623 [MethodImplAttribute (MethodImplOptions.InternalCall)]
624 internal static extern void InternalPushDomainRef (AppDomain domain);
626 [MethodImplAttribute (MethodImplOptions.InternalCall)]
627 internal static extern void InternalPushDomainRefByID (int domain_id);
629 // Undoes the effect of the last PushDomainRef call
630 [MethodImplAttribute (MethodImplOptions.InternalCall)]
631 internal static extern void InternalPopDomainRef ();
633 // Changes the active context and returns the old context
634 [MethodImplAttribute (MethodImplOptions.InternalCall)]
635 internal static extern Context InternalSetContext (Context context);
637 // Returns the current context
638 [MethodImplAttribute (MethodImplOptions.InternalCall)]
639 internal static extern Context InternalGetContext ();
641 // Returns the current context
642 [MethodImplAttribute (MethodImplOptions.InternalCall)]
643 internal static extern Context InternalGetDefaultContext ();
645 [MethodImplAttribute (MethodImplOptions.InternalCall)]
646 internal static extern string InternalGetProcessGuid (string newguid);
648 // This method is handled specially by the runtime
649 // It is the only managed method which is allowed to set the current
651 internal static object InvokeInDomain (AppDomain domain, MethodInfo method, object obj, object [] args)
653 AppDomain current = CurrentDomain;
657 InternalPushDomainRef (domain);
659 InternalSetDomain (domain);
660 return ((MonoMethod) method).InternalInvoke (obj, args);
663 InternalSetDomain (current);
665 InternalPopDomainRef ();
669 internal static object InvokeInDomainByID (int domain_id, MethodInfo method, object obj, object [] args)
671 AppDomain current = CurrentDomain;
675 InternalPushDomainRefByID (domain_id);
677 InternalSetDomainByID (domain_id);
678 return ((MonoMethod) method).InternalInvoke (obj, args);
681 InternalSetDomain (current);
683 InternalPopDomainRef ();
687 internal static String GetProcessGuid ()
689 if (_process_guid == null) {
690 _process_guid = InternalGetProcessGuid (Guid.NewGuid().ToString ());
692 return _process_guid;
695 public static AppDomain CreateDomain (string friendlyName)
697 return CreateDomain (friendlyName, null, null);
700 public static AppDomain CreateDomain (string friendlyName, Evidence securityInfo)
702 return CreateDomain (friendlyName, securityInfo, null);
705 [MethodImplAttribute (MethodImplOptions.InternalCall)]
706 private static extern AppDomain createDomain (string friendlyName, AppDomainSetup info);
708 [MonoTODO ("Currently it does not allow the setup in the other domain")]
709 [SecurityPermission (SecurityAction.Demand, ControlAppDomain = true)]
710 public static AppDomain CreateDomain (string friendlyName, Evidence securityInfo, AppDomainSetup info)
712 if (friendlyName == null)
713 throw new System.ArgumentNullException ("friendlyName");
716 // if null, get default domain's SetupInformation
717 AppDomain def = AppDomain.DefaultDomain;
719 info = new AppDomainSetup (); // we're default!
721 info = def.SetupInformation;
724 info = new AppDomainSetup (info); // copy
726 // todo: allow setup in the other domain
728 AppDomain ad = (AppDomain) RemotingServices.GetDomainProxy (createDomain (friendlyName, info));
729 if (securityInfo == null) {
730 // get default domain's Evidence (unless we're are the default!)
731 AppDomain def = AppDomain.DefaultDomain;
733 ad._evidence = null; // we'll get them later (GetEntryAssembly)
735 ad._evidence = def.Evidence; // new (shallow) copy
738 ad._evidence = new Evidence (securityInfo); // copy
743 public static AppDomain CreateDomain (string friendlyName, Evidence securityInfo,string appBasePath,
744 string appRelativeSearchPath, bool shadowCopyFiles)
746 AppDomainSetup info = new AppDomainSetup ();
748 info.ApplicationBase = appBasePath;
749 info.PrivateBinPath = appRelativeSearchPath;
752 info.ShadowCopyFiles = "true";
754 info.ShadowCopyFiles = "false";
756 return CreateDomain (friendlyName, securityInfo, info);
759 [MethodImplAttribute (MethodImplOptions.InternalCall)]
760 private static extern bool InternalIsFinalizingForUnload (int domain_id);
762 public bool IsFinalizingForUnload()
764 return InternalIsFinalizingForUnload (getDomainID ());
767 [MethodImplAttribute (MethodImplOptions.InternalCall)]
768 static extern void InternalUnload (int domain_id);
770 // We do this because if the domain is a transparant proxy this
771 // will still return the correct domain id.
772 private int getDomainID ()
774 return Thread.GetDomainID ();
777 [SecurityPermission (SecurityAction.Demand, ControlAppDomain = true)]
779 [ReliabilityContractAttribute (Consistency.MayCorruptAppDomain, Cer.MayFail)]
781 public static void Unload (AppDomain domain)
784 throw new ArgumentNullException ("domain");
786 InternalUnload (domain.getDomainID());
789 [MethodImplAttribute (MethodImplOptions.InternalCall)]
790 [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
791 public extern void SetData (string name, object data);
793 [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
794 public void SetDynamicBase (string path)
796 SetupInformationNoCopy.DynamicBase = path;
802 public static int GetCurrentThreadId ()
804 return Thread.CurrentThreadId;
807 public override string ToString ()
809 return getFriendlyName ();
812 // The following methods are called from the runtime. Don't change signatures.
813 private void DoAssemblyLoad (Assembly assembly)
815 if (AssemblyLoad == null)
818 AssemblyLoad (this, new AssemblyLoadEventArgs (assembly));
821 private Assembly DoAssemblyResolve (string name, bool refonly)
824 if (refonly && ReflectionOnlyAssemblyResolve == null)
827 if (AssemblyResolve == null)
830 /* Prevent infinite recursion */
831 Hashtable ht = assembly_resolve_in_progress;
833 ht = new Hashtable ();
834 assembly_resolve_in_progress = ht;
837 Assembly ass = (Assembly) ht [name];
839 if (ass != null && (ass.ReflectionOnly == refonly))
849 Delegate [] invocation_list = refonly ? ReflectionOnlyAssemblyResolve.GetInvocationList () :
850 AssemblyResolve.GetInvocationList ();
852 Delegate [] invocation_list = AssemblyResolve.GetInvocationList ();
855 foreach (Delegate eh in invocation_list) {
856 ResolveEventHandler handler = (ResolveEventHandler) eh;
857 Assembly assembly = handler (this, new ResolveEventArgs (name));
858 if (assembly != null)
868 internal Assembly DoTypeResolve (Object name_or_tb)
870 if (TypeResolve == null)
875 if (name_or_tb is TypeBuilder)
876 name = ((TypeBuilder) name_or_tb).FullName;
878 name = (string) name_or_tb;
880 /* Prevent infinite recursion */
881 Hashtable ht = type_resolve_in_progress;
883 ht = new Hashtable ();
884 type_resolve_in_progress = ht;
887 if (ht.Contains (name))
893 foreach (Delegate d in TypeResolve.GetInvocationList ()) {
894 ResolveEventHandler eh = (ResolveEventHandler) d;
895 Assembly assembly = eh (this, new ResolveEventArgs (name));
896 if (assembly != null)
906 private void DoDomainUnload ()
908 if (DomainUnload != null)
909 DomainUnload(this, null);
912 internal byte[] GetMarshalledDomainObjRef ()
914 ObjRef oref = RemotingServices.Marshal (AppDomain.CurrentDomain, null, typeof (AppDomain));
915 return CADSerializer.SerializeObject (oref).GetBuffer();
918 internal void ProcessMessageInDomain (byte[] arrRequest, CADMethodCallMessage cadMsg,
919 out byte[] arrResponse, out CADMethodReturnMessage cadMrm)
923 if (null != arrRequest)
924 reqDomMsg = CADSerializer.DeserializeMessage (new MemoryStream(arrRequest), null);
926 reqDomMsg = new MethodCall (cadMsg);
928 IMessage retDomMsg = ChannelServices.SyncDispatchMessage (reqDomMsg);
930 cadMrm = CADMethodReturnMessage.Create (retDomMsg);
931 if (null == cadMrm) {
932 arrResponse = CADSerializer.SerializeMessage (retDomMsg).GetBuffer();
938 // End of methods called from the runtime
940 #if BOOTSTRAP_WITH_OLDLIB
941 // older MCS/corlib returns:
942 // _AppDomain.cs(138) error CS0592: Attribute 'SecurityPermission' is not valid on this declaration type.
943 // It is valid on 'assembly' 'class' 'constructor' 'method' 'struct' declarations only.
944 public event AssemblyLoadEventHandler AssemblyLoad;
946 public event ResolveEventHandler AssemblyResolve;
948 public event EventHandler DomainUnload;
950 public event EventHandler ProcessExit;
952 public event ResolveEventHandler ResourceResolve;
954 public event ResolveEventHandler TypeResolve;
956 public event UnhandledExceptionEventHandler UnhandledException;
958 [method: SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
959 public event AssemblyLoadEventHandler AssemblyLoad;
961 [method: SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
962 public event ResolveEventHandler AssemblyResolve;
964 [method: SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
965 public event EventHandler DomainUnload;
967 [method: SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
968 public event EventHandler ProcessExit;
970 [method: SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
971 public event ResolveEventHandler ResourceResolve;
973 [method: SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
974 public event ResolveEventHandler TypeResolve;
976 [method: SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
977 public event UnhandledExceptionEventHandler UnhandledException;
980 /* Avoid warnings for events used only by the runtime */
981 private void DummyUse () {
982 ProcessExit += (EventHandler)null;
983 ResourceResolve += (ResolveEventHandler)null;
984 UnhandledException += (UnhandledExceptionEventHandler)null;
989 public event ResolveEventHandler ReflectionOnlyAssemblyResolve;
991 private ActivationContext _activation;
992 private ApplicationIdentity _applicationIdentity;
993 private AppDomainManager _domain_manager;
997 public ActivationContext ActivationContext {
998 get { return _activation; }
1001 public ApplicationIdentity ApplicationIdentity {
1002 get { return _applicationIdentity; }
1006 public AppDomainManager DomainManager {
1007 get { return _domain_manager; }
1011 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
1012 get { return getDomainID (); }
1017 [MonoTODO ("This routine only returns the parameter currently")]
1018 [ComVisible (false)]
1019 public string ApplyPolicy (string assemblyName)
1021 if (assemblyName == null)
1022 throw new ArgumentNullException ("assemblyName");
1023 if (assemblyName.Length == 0) // String.Empty
1024 throw new ArgumentException ("assemblyName");
1025 return assemblyName;
1030 [MonoTODO ("add support for new delegate")]
1031 public static AppDomain CreateDomain (string friendlyName, Evidence securityInfo, string appBasePath,
1032 string appRelativeSearchPath, bool shadowCopy, AppDomainInitializer adInit, string[] adInitArgs)
1034 return CreateDomain (friendlyName, securityInfo, appBasePath, appRelativeSearchPath, shadowCopy);
1037 [MonoTODO ("resolve assemblyName to location")]
1038 public int ExecuteAssemblyByName (string assemblyName)
1040 return ExecuteAssemblyByName (assemblyName, null, null);
1043 [MonoTODO ("resolve assemblyName to location")]
1044 public int ExecuteAssemblyByName (string assemblyName, Evidence assemblySecurity)
1046 return ExecuteAssemblyByName (assemblyName, assemblySecurity, null);
1049 [MonoTODO ("resolve assemblyName to location")]
1050 public int ExecuteAssemblyByName (string assemblyName, Evidence assemblySecurity, string[] args)
1052 if (assemblyName == null)
1053 throw new ArgumentNullException ("assemblyName");
1055 AssemblyName an = new AssemblyName (assemblyName);
1056 return ExecuteAssemblyByName (an, assemblySecurity, args);
1059 [MonoTODO ("assemblyName may not have a codebase")]
1060 public int ExecuteAssemblyByName (AssemblyName assemblyName, Evidence assemblySecurity, string[] args)
1062 if (assemblyName == null)
1063 throw new ArgumentNullException ("assemblyName");
1065 return ExecuteAssembly (assemblyName.CodeBase, assemblySecurity, args);
1068 public bool IsDefaultAppDomain ()
1070 return Object.ReferenceEquals (this, DefaultDomain);
1073 public Assembly[] ReflectionOnlyGetAssemblies ()
1075 return GetAssemblies (true);
1080 void _AppDomain.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1082 throw new NotImplementedException ();
1085 void _AppDomain.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1087 throw new NotImplementedException ();
1090 void _AppDomain.GetTypeInfoCount (out uint pcTInfo)
1092 throw new NotImplementedException ();
1095 void _AppDomain.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
1096 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1098 throw new NotImplementedException ();