added some AppDomain methods
[mono.git] / mcs / class / corlib / System / AppDomain.cs
1
2 //
3 // System/AppDomain.cs
4 //
5 // Authors:
6 //   Paolo Molaro (lupus@ximian.com)
7 //   Dietmar Maurer (dietmar@ximian.com)
8 //
9 // (C) 2001 Ximian, Inc.  http://www.ximian.com
10 //
11
12 using System;
13 using System.Collections;
14 using System.Reflection;
15 using System.Reflection.Emit;
16 using System.Runtime.CompilerServices;
17 using System.Security.Policy;
18
19 namespace System {
20
21         public sealed class AppDomain /* : MarshalByRefObject , _AppDomain, IEvidenceFactory */ {
22
23                 private Hashtable loaded_assemblies = new Hashtable ();
24
25                 private Hashtable data_hash = new Hashtable ();
26                 
27                 private AppDomainSetup adsetup;
28
29                 private string friendly_name;
30
31                 private Evidence evidence;
32
33                 public AppDomainSetup SetupInformation {
34
35                         get {
36                                 return adsetup;
37                         }
38                 }
39
40                 public string BaseDirectory {
41
42                         get {
43                                 return adsetup.ApplicationBase;
44                         }
45                 }
46
47                 public string RelativeSearchPath {
48
49                         get {
50                                 return adsetup.PrivateBinPath;
51                         }
52                 }
53
54                 public string DynamicDirectory {
55
56                         get {
57                                 // fixme: dont know what to return here
58                                 return null;
59                         }
60                 }
61
62                 public string FriendlyName {
63
64                         get {
65                                 return friendly_name;
66                         }
67                 }
68
69                 public Evidence Evidence {
70
71                         get {
72                                 return evidence;
73                         }
74                 }
75
76                 
77                 public static AppDomain CreateDomain(string friendlyName)
78                 {
79                         return CreateDomain (friendlyName, new Evidence (), new AppDomainSetup ());
80                 }
81                 
82                 public static AppDomain CreateDomain(string friendlyName, Evidence securityInfo)
83                 {
84                         return CreateDomain (friendlyName, securityInfo, new AppDomainSetup ());
85                 }
86                 
87                 public static AppDomain CreateDomain(string friendlyName, Evidence securityInfo, AppDomainSetup info)
88                 {
89                         if (friendlyName == null || securityInfo == null || info == null)
90                                 throw new System.ArgumentNullException();
91
92                         AppDomain ad = new AppDomain ();
93
94                         ad.friendly_name = friendlyName;
95                         ad.evidence = securityInfo;
96                         ad.adsetup = info;
97                         
98                         return ad;
99                 }
100
101                 public static AppDomain CreateDomain(string friendlyName, Evidence securityInfo, string appBasePath,
102                                                      string appRelativeSearchPath, bool shadowCopyFiles)
103                 {
104                         AppDomainSetup info = new AppDomainSetup ();
105
106                         info.ApplicationBase = appBasePath;
107                         info.PrivateBinPath = appRelativeSearchPath;
108
109                         if (shadowCopyFiles)
110                                 info.ShadowCopyFiles = "true";
111                         else
112                                 info.ShadowCopyFiles = "false";
113
114                         return CreateDomain (friendlyName, securityInfo, info);
115                 }
116                 
117                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
118                 public static extern Assembly LoadFrom(String assemblyFile, Evidence securityEvidence);
119
120                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
121                 private static extern AppDomain getCurDomain ();
122                 
123                 public static AppDomain CurrentDomain
124                 {
125                         get {
126                                 return getCurDomain ();
127                         }
128                 }
129
130                 public AssemblyBuilder DefineDynamicAssembly (AssemblyName name,
131                                                               AssemblyBuilderAccess access)
132                 {
133                         AssemblyBuilder ab = new AssemblyBuilder (name, access);
134                         return ab;
135                 }
136
137                 public Assembly Load (AssemblyName assemblyRef)
138                 {
139                         return Load (assemblyRef, new Evidence ());
140                 }
141
142                 public Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
143                 {
144                         Assembly res;
145                         
146                         if ((res = (Assembly)loaded_assemblies [assemblyRef]) != null)
147                                 return res;
148
149                         // fixme: we should pass the whole assemblyRef instead of the name,
150                         // and maybe also the adsetup
151                         res = LoadFrom (assemblyRef.Name, assemblySecurity);
152
153                         loaded_assemblies [assemblyRef] = res;
154                         
155                         return res;
156                 }
157
158                 public Assembly Load (string assemblyString)
159                 {
160                         AssemblyName an = new AssemblyName ();
161                         an.Name = assemblyString;
162                         
163                         return Load (an, new Evidence ());                      
164                 }
165
166                 public Assembly Load (string assemblyString, Evidence assemblySecurity)
167                 {
168                         AssemblyName an = new AssemblyName ();
169                         an.Name = assemblyString;
170                         
171                         return Load (an, assemblySecurity);                     
172                 }
173
174                 public Assembly Load (byte[] rawAssembly)
175                 {
176                         return Load (rawAssembly, null, new Evidence ());
177                 }
178
179                 public Assembly Load (byte[] rawAssembly, byte[] rawSymbolStore)
180                 {
181                         return Load (rawAssembly, rawSymbolStore, new Evidence ());
182                 }
183
184                 public Assembly Load (byte[] rawAssembly, byte[] rawSymbolStore, Evidence securityEvidence)
185                 {
186                         throw new NotImplementedException ();
187                 }
188
189                 public Assembly[] GetAssemblies ()
190                 {
191                         int x = loaded_assemblies.Count;
192                         return null;
193                         Assembly[] res = new Assembly [loaded_assemblies.Count];
194
195                         IDictionaryEnumerator it = loaded_assemblies.GetEnumerator ();
196                         int i = 0;
197
198                         
199                         while (it.MoveNext ())
200                                 res [i++] = (Assembly)it.Value;
201
202                         return res;
203                 }
204
205                 // fixme: how does marshalling work ?
206                 public object GetData (string name)
207                 {
208                         switch (name) {
209                         case "APPBASE": return adsetup.ApplicationBase;
210                         case "APP_CONFIG_FILE": return adsetup.ConfigurationFile;
211                         case "DYNAMIC_BASE": return adsetup.DynamicBase;
212                         case "APP_NAME": return adsetup.ApplicationName;
213                         case "CACHE_BASE": return adsetup.CachePath;
214                         case "PRIVATE_BINPATH": return adsetup.PrivateBinPath;
215                         case "BINPATH_PROBE_ONLY": return adsetup.PrivateBinPathProbe;
216                         case "SHADOW_COPY_DIRS": return adsetup.ShadowCopyDirectories;
217                         case "FORCE_CACHE_INSTALL": return adsetup.ShadowCopyFiles;
218                         }
219
220                         return data_hash [name];
221                 }
222
223                 // fixme: how does marshalling work ?
224                 public void SetData (string name, object data)
225                 {
226                         // LAMESPEC: why can't we set adsetup properties ??
227
228                         data_hash [name] = data;
229                 }
230
231         }
232 }