2002-07-10 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / corlib / System / Activator.cs
1 //
2 // System.Activator.cs
3 //
4 // Authors:
5 //   Nick Drochak II (ndrochak@gol.com)
6 //   Gonzalo Paniagua (gonzalo@ximian.com)
7 //
8 // (C) 2001 Nick Drochak II
9 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
10 //
11
12 using System.Runtime.Remoting;
13 using System.Reflection;
14 using System.Globalization;
15 using System.Security.Policy;
16
17 namespace System 
18 {
19         public sealed class Activator
20         {
21                 private static BindingFlags _flags = BindingFlags.CreateInstance |
22                                                      BindingFlags.Public |
23                                                      BindingFlags.Instance;
24
25                 private Activator () {}
26
27                 [MonoTODO]
28                 public static ObjectHandle CreateComInstanceFrom (string assemblyName, string typeName)
29                 {
30                         throw new NotImplementedException(); 
31                 }
32
33                 public static ObjectHandle CreateInstanceFrom (string assemblyFile, string typeName)
34                 {
35                         return CreateInstanceFrom (assemblyFile, typeName, null);
36                 }
37
38                 public static ObjectHandle CreateInstanceFrom (string assemblyFile,
39                                                                string typeName,
40                                                                object [] activationAttributes)
41                 {
42                         return Activator.CreateInstanceFrom (assemblyFile,
43                                                              typeName,
44                                                              false,
45                                                              _flags,
46                                                              null,
47                                                              null,
48                                                              null,
49                                                              activationAttributes,
50                                                              null);
51                 }
52                 
53                 [MonoTODO]
54                 public static ObjectHandle CreateInstanceFrom (string assemblyFile,
55                                                                string typeName,
56                                                                bool ignoreCase,
57                                                                BindingFlags bindingAttr,
58                                                                Binder binder,
59                                                                object [] args,
60                                                                CultureInfo culture,
61                                                                object [] activationAttributes,
62                                                                Evidence securityInfo)
63                 {
64                         //TODO: when Assembly implements security, use it.
65                         //Assembly assembly = Assembly.LoadFrom (assemblyFile, securityInfo);
66                         Assembly assembly = Assembly.LoadFrom (assemblyFile);
67                         if (assembly == null)
68                                 return null;
69
70                         Type type = assembly.GetType (typeName, true, ignoreCase);
71                         if (type == null)
72                                 return null;
73
74                         object obj = CreateInstance (type, bindingAttr, binder, args, culture, activationAttributes);
75                         return (obj != null) ? new ObjectHandle (obj) : null;
76                 }
77                 
78                 public static ObjectHandle CreateInstance (string assemblyName, string typeName)
79                 {
80                         return Activator.CreateInstance (assemblyName, typeName, null);
81                 }
82                 
83                 public static ObjectHandle CreateInstance (string assemblyName,
84                                                            string typeName,
85                                                            object [] activationAttributes)
86                 {
87                         return Activator.CreateInstance (assemblyName,
88                                                          typeName,
89                                                          false,
90                                                          _flags,
91                                                          null,
92                                                          null,
93                                                          null,
94                                                          activationAttributes,
95                                                          null);
96                 }
97                 
98                 [MonoTODO]
99                 public static ObjectHandle CreateInstance (string assemblyName,
100                                                            string typeName,
101                                                            bool ignoreCase,
102                                                            BindingFlags bindingAttr,
103                                                            Binder binder,
104                                                            object [] args,
105                                                            CultureInfo culture,
106                                                            object [] activationAttributes,
107                                                            Evidence securityInfo)
108                 {
109                         //TODO: when Assembly implements security, use it.
110                         //Assembly assembly = Assembly.Load (assemblyFile, securityInfo);
111                         Assembly assembly = Assembly.Load (assemblyName);
112                         Type type = assembly.GetType (typeName, true, ignoreCase);
113                         object obj = CreateInstance (type, bindingAttr, binder, args, culture, activationAttributes);
114                         return (obj != null) ? new ObjectHandle (obj) : null;
115                 }
116                 
117                 public static object CreateInstance (Type type)
118                 {
119                         return CreateInstance (type, false);
120                 }
121                 
122                 public static object CreateInstance (Type type, object [] args)
123                 {
124                         return CreateInstance (type, args, new object [0]);
125                 }
126
127                 [MonoTODO]
128                 public static object CreateInstance (Type type, object [] args, object [] activationAttributes)
129                 {
130                         // activationAttributes?
131                         if (type == null)
132                                 throw new ArgumentNullException ("type");
133
134                         int length = 0;
135                         if (args != null)
136                                 length = args.Length;
137
138                         Type [] atypes = new Type [length];
139                         for (int i = 0; i < length; ++i) {
140                                 atypes [i] = args [i].GetType ();
141                         }
142                         ConstructorInfo ctor = type.GetConstructor (atypes);
143                         if (ctor == null)
144                                 return null;
145
146                         return ctor.Invoke (args);
147                 }
148
149                 public static object CreateInstance (Type type,
150                                                      BindingFlags bindingAttr,
151                                                      Binder binder,
152                                                      object [] args,
153                                                      CultureInfo culture)
154                 {
155                         return CreateInstance (type, bindingAttr, binder, args, culture, new object [0]);
156                 }
157
158                 [MonoTODO]
159                 public static object CreateInstance (Type type,
160                                                      BindingFlags bindingAttr,
161                                                      Binder binder,
162                                                      object [] args,
163                                                      CultureInfo culture,
164                                                      object [] activationAttributes)
165                 {
166                         // activationAttributes?
167                         int length = 0;
168                         if (args != null)
169                                 length = args.Length;
170
171                         Type[] atypes = new Type [length];
172                         for (int i = 0; i < length; ++i) {
173                                 atypes [i] = args [i].GetType ();
174                         }
175                         //FIXME: when GetConstructor works with this parameter list, use it
176                         //ConstructorInfo ctor = type.GetConstructor (bindingAttr, binder, atypes, null);
177                         ConstructorInfo ctor = type.GetConstructor (atypes);
178                         if (ctor == null)
179                                 return null;
180
181                         //FIXME: when Invoke allows this parameter list, use it
182                         //return ctor.Invoke (args, bindingAttr, binder, args, culture);
183                         return ctor.Invoke (args);
184                 }
185
186                 public static object CreateInstance (Type type, bool nonPublic)
187                 { 
188                         if (type == null)
189                                 throw new ArgumentNullException ("type");
190                                 
191                         ConstructorInfo ctor = type.GetConstructor (Type.EmptyTypes);
192                         if (ctor.IsPublic && nonPublic == true)
193                                 return null;
194
195                         if (ctor == null)
196                                 return null;
197
198                         return ctor.Invoke (null);
199                 }
200
201                 [MonoTODO]
202                 public static object GetObject (Type type, string url)
203                 {
204                         throw new NotImplementedException(); 
205                 }
206
207                 [MonoTODO]
208                 public static object GetObject (Type type, string url, object state)
209                 { 
210                         throw new NotImplementedException(); 
211                 }
212         }
213 }
214