Move some MOBILE bits under DISABLE_REMOTING as a remoting enabled runtime will need...
[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 //   Marek Safar (marek.safar@gmail.com)
8 //
9 // (C) 2001 Nick Drochak II
10 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
11 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 // Copyright (C) 2013 Xamarin Inc (http://www.xamarin.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System.Globalization;
35 using System.Reflection;
36 using System.Runtime.CompilerServices;
37 using System.Runtime.InteropServices;
38 using System.Security.Permissions;
39 using System.Security.Policy;
40 using System.Configuration.Assemblies;
41 using System.Text;
42 using System.Runtime.Remoting;
43 using System.Runtime.Remoting.Activation;
44
45 namespace System 
46 {
47         [ClassInterface (ClassInterfaceType.None)]
48         [ComVisible (true)]
49         [ComDefaultInterface (typeof (_Activator))]
50         public sealed class Activator : _Activator
51         {
52                 const BindingFlags _flags = BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.Instance;
53                 const BindingFlags _accessFlags = BindingFlags.DeclaredOnly | BindingFlags.FlattenHierarchy | BindingFlags.IgnoreCase | 
54                                                                                         BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public |
55                                                                                         BindingFlags.Static;
56
57                 private Activator ()
58                 {
59                 }
60
61                 [MonoTODO ("No COM support")]
62                 public static ObjectHandle CreateComInstanceFrom (string assemblyName, string typeName)
63                 {
64                         if (assemblyName == null)
65                                 throw new ArgumentNullException ("assemblyName");
66
67                         if (typeName == null)
68                                 throw new ArgumentNullException ("typeName");
69
70                         if (assemblyName.Length == 0)
71                                 throw new ArgumentException ("assemblyName");
72
73                         throw new NotImplementedException();
74                 }
75
76                 [MonoTODO("Mono does not support COM")]
77                 public static ObjectHandle CreateComInstanceFrom (string assemblyName, string typeName,
78                                                                   byte []hashValue, AssemblyHashAlgorithm hashAlgorithm)
79                 {
80                         if (assemblyName == null)
81                                 throw new ArgumentNullException ("assemblyName");
82
83                         if (typeName == null)
84                                 throw new ArgumentNullException ("typeName");
85
86                         if (assemblyName.Length == 0)
87                                 throw new ArgumentException ("assemblyName");
88
89                         throw new NotImplementedException();
90                 }
91
92                 public static ObjectHandle CreateInstanceFrom (string assemblyFile, string typeName)
93                 {
94                         return CreateInstanceFrom (assemblyFile, typeName, null);
95                 }
96
97                 public static ObjectHandle CreateInstanceFrom (string assemblyFile, string typeName, object [] activationAttributes)
98                 {
99                         return Activator.CreateInstanceFrom (assemblyFile, typeName, false, _flags, null, null, null,
100                                 activationAttributes, null);
101                 }
102
103 #if NET_4_0
104                 [Obsolete]
105 #endif
106                 public static ObjectHandle CreateInstanceFrom (string assemblyFile, string typeName, bool ignoreCase,
107                                                                BindingFlags bindingAttr, Binder binder, object [] args,
108                                                                CultureInfo culture, object [] activationAttributes,
109                                                                Evidence securityInfo)
110                 {
111                         Assembly assembly = Assembly.LoadFrom (assemblyFile, securityInfo);
112                         if (assembly == null)
113                                 return null;
114
115                         Type type = assembly.GetType (typeName, true, ignoreCase);
116                         if (type == null)
117                                 return null;
118
119                         object obj = CreateInstance (type, bindingAttr, binder, args, culture, activationAttributes);
120                         return (obj != null) ? new ObjectHandle (obj) : null;
121                 }
122
123                 public static ObjectHandle CreateInstance (string assemblyName, string typeName)
124                 {
125                         if (assemblyName == null)
126                                 assemblyName = Assembly.GetCallingAssembly ().GetName ().Name;
127
128                         return Activator.CreateInstance (assemblyName, typeName, null);
129                 }
130
131                 public static ObjectHandle CreateInstance (string assemblyName, string typeName, object [] activationAttributes)
132                 {
133                         if (assemblyName == null)
134                                 assemblyName = Assembly.GetCallingAssembly ().GetName ().Name;
135
136                         return Activator.CreateInstance (assemblyName, typeName, false, _flags, null, null, null,
137                                 activationAttributes, null);
138                 }
139
140 #if NET_4_0
141                 [Obsolete]
142 #endif
143                 public static ObjectHandle CreateInstance (string assemblyName, string typeName, bool ignoreCase,
144                                                            BindingFlags bindingAttr, Binder binder, object [] args,
145                                                            CultureInfo culture, object [] activationAttributes, Evidence securityInfo)
146                 {
147                         Assembly assembly = null;
148                         if(assemblyName == null)
149                                 assembly = Assembly.GetCallingAssembly ();
150                         else
151                                 assembly = Assembly.Load (assemblyName, securityInfo);
152                         Type type = assembly.GetType (typeName, true, ignoreCase);
153                         object obj = CreateInstance (type, bindingAttr, binder, args, culture, activationAttributes);
154                         return (obj != null) ? new ObjectHandle (obj) : null;
155                 }
156
157                 [MonoNotSupported ("no ClickOnce in mono")]
158                 public static ObjectHandle CreateInstance (ActivationContext activationContext)
159                 {
160                         throw new NotImplementedException ();
161                 }
162
163                 [MonoNotSupported ("no ClickOnce in mono")]
164                 public static ObjectHandle CreateInstance (ActivationContext activationContext, string [] activationCustomData)
165                 {
166                         throw new NotImplementedException ();
167                 }
168
169                 // Cross-domain instance creation
170
171                 public static ObjectHandle CreateInstanceFrom (AppDomain domain, string assemblyFile, string typeName)
172                 {
173                         if (domain == null)
174                                 throw new ArgumentNullException ("domain");
175                         return domain.CreateInstanceFrom (assemblyFile, typeName);
176                 }
177
178
179 #if NET_4_0
180                 [Obsolete]
181 #endif
182                 public static ObjectHandle CreateInstanceFrom (AppDomain domain, string assemblyFile, string typeName,
183                                                                bool ignoreCase, BindingFlags bindingAttr, Binder binder,
184                                                                object [] args, CultureInfo culture,
185                                                                object [] activationAttributes,
186                                                                Evidence securityAttributes)
187                 {
188                         if (domain == null)
189                                 throw new ArgumentNullException ("domain");
190
191                         return domain.CreateInstanceFrom (assemblyFile, typeName, ignoreCase, bindingAttr, binder, args, culture, activationAttributes, securityAttributes);
192                 }
193
194                 public static ObjectHandle CreateInstance (AppDomain domain, string assemblyName, string typeName)
195                 {
196                         if (domain == null)
197                                 throw new ArgumentNullException ("domain");
198                         return domain.CreateInstance (assemblyName, typeName);
199                 }
200
201 #if NET_4_0
202                 [Obsolete]
203 #endif
204                 public static ObjectHandle CreateInstance (AppDomain domain, string assemblyName, string typeName,
205                                                            bool ignoreCase, BindingFlags bindingAttr, Binder binder,
206                                                            object [] args, CultureInfo culture,
207                                                            object [] activationAttributes,
208                                                            Evidence securityAttributes)
209                 {
210                         if (domain == null)
211                                 throw new ArgumentNullException ("domain");
212                         return domain.CreateInstance (assemblyName, typeName, ignoreCase, bindingAttr, binder, args, culture, activationAttributes, securityAttributes);
213                 }
214
215                 public static T CreateInstance <T> ()
216                 {
217                         return (T) CreateInstance (typeof (T));
218                 }
219
220                 public static object CreateInstance (Type type)
221                 {
222                         return CreateInstance (type, false);
223                 }
224
225                 public static object CreateInstance (Type type, params object [] args)
226                 {
227                         return CreateInstance (type, args, EmptyArray<object>.Value);
228                 }
229
230                 public static object CreateInstance (Type type, object [] args, object [] activationAttributes)
231                 {
232                         return CreateInstance (type, BindingFlags.Default, Binder.DefaultBinder, args, null, activationAttributes);
233                 }
234
235                 public static object CreateInstance (Type type, BindingFlags bindingAttr, Binder binder, object [] args,
236                                                      CultureInfo culture)
237                 {
238                         return CreateInstance (type, bindingAttr, binder, args, culture, EmptyArray<object>.Value);
239                 }
240
241                 public static object CreateInstance (Type type, BindingFlags bindingAttr, Binder binder, object [] args,
242                                                      CultureInfo culture, object [] activationAttributes)
243                 {
244                         CheckType (type);
245
246                         if (type.ContainsGenericParameters)
247                                 throw new ArgumentException (type + " is an open generic type", "type");
248
249                         // It seems to apply the same rules documented for InvokeMember: "If the type of lookup
250                         // is omitted, BindingFlags.Public | BindingFlags.Instance will apply".
251                         if ((bindingAttr & _accessFlags) == 0)
252                                 bindingAttr |= BindingFlags.Public | BindingFlags.Instance;
253
254                         if (binder == null)
255                                 binder = Binder.DefaultBinder;
256
257                         object state;
258                         ConstructorInfo ctor = (ConstructorInfo) binder.BindToMethod (bindingAttr, type.GetConstructors (bindingAttr), ref args, null, null, null, out state);
259
260                         if (ctor == null) {
261                                 // Not sure about this
262                                 if (type.IsValueType && (args == null || args.Length == 0)) {
263                                         return CreateInstanceInternal (type);
264                                 }
265
266                                 var sb = new StringBuilder ();
267                                 if (args != null) {
268                                         for (int i = 0; i < args.Length; i++) {
269                                                 if (i > 0)
270                                                         sb.Append (", ");
271
272                                                 var argument = args [i];
273                                                 var arg_type = argument != null ? argument.GetType () : null;
274                                                 sb.Append (arg_type != null ? arg_type.ToString () : "(unknown)");
275                                         }
276                                 }
277
278                                 throw new MissingMethodException (String.Format (Locale.GetText ("No constructor found for {0}::.ctor({1})"),
279                                                                                  type.FullName, sb));
280                         }
281
282                         CheckAbstractType (type);
283
284                         if (activationAttributes != null && activationAttributes.Length > 0) {
285 #if DISABLE_REMOTING
286                                 throw new NotSupportedException ("Activation attributes are not supported");
287 #else
288                                 if (!type.IsMarshalByRef) {
289                                         string msg = Locale.GetText ("Type '{0}' doesn't derive from MarshalByRefObject.", type.FullName);
290                                         throw new NotSupportedException (msg);
291                                 }
292                                 object newOb = ActivationServices.CreateProxyFromAttributes (type, activationAttributes);
293                                 if (newOb != null) {
294                                         // This returns null
295                                         ctor.Invoke (newOb, bindingAttr, binder, args, culture);
296                                         return newOb;
297                                 }
298 #endif
299                         }
300
301                         return ctor.Invoke (bindingAttr, binder, args, culture);
302                 }
303
304                 public static object CreateInstance (Type type, bool nonPublic)
305                 { 
306                         CheckType (type);
307
308                         if (type.ContainsGenericParameters)
309                                 throw new ArgumentException (type + " is an open generic type", "type");
310
311                         MonoType monoType = type.UnderlyingSystemType as MonoType;
312                         if (monoType == null)
313                                 throw new ArgumentException ("Type must be a type provided by the runtime");
314
315                         CheckAbstractType (monoType);
316
317                         var ctor = monoType.GetDefaultConstructor ();
318                         if (!nonPublic && ctor != null && !ctor.IsPublic) {
319                                 ctor = null;
320                         }
321
322                         if (ctor == null) {
323                                 if (type.IsValueType)
324                                         return CreateInstanceInternal (type);
325
326                                 throw new MissingMethodException (Locale.GetText ("Default constructor not found for type " + type.FullName));
327                         }
328
329                         return ctor.InternalInvoke (null, null);
330                 }
331
332                 private static void CheckType (Type type)
333                 {
334                         if (type == null)
335                                 throw new ArgumentNullException ("type");
336
337                         if ((type == typeof (TypedReference)) || (type == typeof (ArgIterator)) || (type == typeof (void)) ||
338                                 (type == typeof (RuntimeArgumentHandle))) {
339                                 string msg = Locale.GetText ("CreateInstance cannot be used to create this type ({0}).", type.FullName);
340                                 throw new NotSupportedException (msg);
341                         }
342                 }
343
344                 private static void CheckAbstractType (Type type)
345                 {
346                         if (type.IsAbstract) {
347                                 string msg = Locale.GetText ("Cannot create an abstract class '{0}'.", type.FullName);
348                                 throw new MissingMethodException (msg);
349                         }
350                 }
351
352                 [SecurityPermission (SecurityAction.LinkDemand, RemotingConfiguration = true)]
353                 public static object GetObject (Type type, string url)
354                 {
355                         if (type == null)
356                                 throw new ArgumentNullException ("type");
357
358                         return RemotingServices.Connect (type, url);
359                 }
360
361                 [SecurityPermission (SecurityAction.LinkDemand, RemotingConfiguration = true)]
362                 public static object GetObject (Type type, string url, object state)
363                 {
364                         if (type == null)
365                                 throw new ArgumentNullException ("type");
366
367                         return RemotingServices.Connect (type, url, state);
368                 }
369
370                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
371                 internal static extern object CreateInstanceInternal (Type type);
372
373                 void _Activator.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
374                 {
375                         throw new NotImplementedException ();
376                 }
377
378                 void _Activator.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
379                 {
380                         throw new NotImplementedException ();
381                 }
382
383                 void _Activator.GetTypeInfoCount (out uint pcTInfo)
384                 {
385                         throw new NotImplementedException ();
386                 }
387
388                 void _Activator.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
389                         IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
390                 {
391                         throw new NotImplementedException ();
392                 }
393
394 #if NET_4_0
395                 public static ObjectHandle CreateInstance (string assemblyName, string typeName, bool ignoreCase,
396                                                            BindingFlags bindingAttr, Binder binder, object [] args,
397                                                            CultureInfo culture, object [] activationAttributes)
398                 {
399                         Assembly assembly = null;
400                         if(assemblyName == null)
401                                 assembly = Assembly.GetCallingAssembly ();
402                         else
403                                 assembly = Assembly.Load (assemblyName);
404                         Type type = assembly.GetType (typeName, true, ignoreCase);
405                         object obj = CreateInstance (type, bindingAttr, binder, args, culture, activationAttributes);
406                         return (obj != null) ? new ObjectHandle (obj) : null;
407                 }
408
409                 public static ObjectHandle CreateInstance (AppDomain domain, string assemblyName, string typeName,
410                                                            bool ignoreCase, BindingFlags bindingAttr, Binder binder,
411                                                            object [] args, CultureInfo culture,
412                                                            object [] activationAttributes)
413                 {
414                         if (domain == null)
415                                 throw new ArgumentNullException ("domain");
416                         return domain.CreateInstance (assemblyName, typeName, ignoreCase, bindingAttr, binder, args, culture, activationAttributes);
417                 }
418
419                 public static ObjectHandle CreateInstanceFrom (string assemblyFile, string typeName, bool ignoreCase,
420                                                                BindingFlags bindingAttr, Binder binder, object [] args,
421                                                                CultureInfo culture, object [] activationAttributes)
422                 {
423                         Assembly assembly = Assembly.LoadFrom (assemblyFile);
424                         if (assembly == null)
425                                 return null;
426
427                         Type type = assembly.GetType (typeName, true, ignoreCase);
428                         if (type == null)
429                                 return null;
430
431                         object obj = CreateInstance (type, bindingAttr, binder, args, culture, activationAttributes);
432                         return (obj != null) ? new ObjectHandle (obj) : null;
433                 }
434
435                 public static ObjectHandle CreateInstanceFrom (AppDomain domain, string assemblyFile, string typeName,
436                                                                bool ignoreCase, BindingFlags bindingAttr, Binder binder,
437                                                                object [] args, CultureInfo culture,
438                                                                object [] activationAttributes)
439                 {
440                         if (domain == null)
441                                 throw new ArgumentNullException ("domain");
442
443                         return domain.CreateInstanceFrom (assemblyFile, typeName, ignoreCase, bindingAttr, binder, args, culture, activationAttributes);
444                 }
445 #endif
446         }
447 }