5 // Nick Drochak II (ndrochak@gol.com)
6 // Gonzalo Paniagua (gonzalo@ximian.com)
8 // (C) 2001 Nick Drochak II
9 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Globalization;
33 using System.Reflection;
34 using System.Runtime.CompilerServices;
35 using System.Runtime.InteropServices;
36 using System.Security.Permissions;
37 using System.Security.Policy;
38 using System.Configuration.Assemblies;
41 using System.Runtime.Remoting;
42 using System.Runtime.Remoting.Activation;
47 [ClassInterface (ClassInterfaceType.None)]
49 [ComDefaultInterface (typeof (_Activator))]
50 public sealed class Activator : _Activator
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 |
62 [MonoTODO ("No COM support")]
63 public static ObjectHandle CreateComInstanceFrom (string assemblyName, string typeName)
65 if (assemblyName == null)
66 throw new ArgumentNullException ("assemblyName");
69 throw new ArgumentNullException ("typeName");
71 if (assemblyName.Length == 0)
72 throw new ArgumentException ("assemblyName");
74 throw new NotImplementedException();
77 [MonoTODO("Mono does not support COM")]
78 public static ObjectHandle CreateComInstanceFrom (string assemblyName, string typeName,
79 byte []hashValue, AssemblyHashAlgorithm hashAlgorithm)
81 if (assemblyName == null)
82 throw new ArgumentNullException ("assemblyName");
85 throw new ArgumentNullException ("typeName");
87 if (assemblyName.Length == 0)
88 throw new ArgumentException ("assemblyName");
90 throw new NotImplementedException();
93 public static ObjectHandle CreateInstanceFrom (string assemblyFile, string typeName)
95 return CreateInstanceFrom (assemblyFile, typeName, null);
98 public static ObjectHandle CreateInstanceFrom (string assemblyFile, string typeName, object [] activationAttributes)
100 return Activator.CreateInstanceFrom (assemblyFile, typeName, false, _flags, null, null, null,
101 activationAttributes, null);
107 public static ObjectHandle CreateInstanceFrom (string assemblyFile, string typeName, bool ignoreCase,
108 BindingFlags bindingAttr, Binder binder, object [] args,
109 CultureInfo culture, object [] activationAttributes,
110 Evidence securityInfo)
112 Assembly assembly = Assembly.LoadFrom (assemblyFile, securityInfo);
113 if (assembly == null)
116 Type type = assembly.GetType (typeName, true, ignoreCase);
120 object obj = CreateInstance (type, bindingAttr, binder, args, culture, activationAttributes);
121 return (obj != null) ? new ObjectHandle (obj) : null;
124 public static ObjectHandle CreateInstance (string assemblyName, string typeName)
126 if (assemblyName == null)
127 assemblyName = Assembly.GetCallingAssembly ().GetName ().Name;
129 return Activator.CreateInstance (assemblyName, typeName, null);
132 public static ObjectHandle CreateInstance (string assemblyName, string typeName, object [] activationAttributes)
134 if (assemblyName == null)
135 assemblyName = Assembly.GetCallingAssembly ().GetName ().Name;
137 return Activator.CreateInstance (assemblyName, typeName, false, _flags, null, null, null,
138 activationAttributes, null);
144 public static ObjectHandle CreateInstance (string assemblyName, string typeName, bool ignoreCase,
145 BindingFlags bindingAttr, Binder binder, object [] args,
146 CultureInfo culture, object [] activationAttributes, Evidence securityInfo)
148 Assembly assembly = null;
149 if(assemblyName == null)
150 assembly = Assembly.GetCallingAssembly ();
152 assembly = Assembly.Load (assemblyName, securityInfo);
153 Type type = assembly.GetType (typeName, true, ignoreCase);
154 object obj = CreateInstance (type, bindingAttr, binder, args, culture, activationAttributes);
155 return (obj != null) ? new ObjectHandle (obj) : null;
158 [MonoNotSupported ("no ClickOnce in mono")]
159 public static ObjectHandle CreateInstance (ActivationContext activationContext)
161 throw new NotImplementedException ();
164 [MonoNotSupported ("no ClickOnce in mono")]
165 public static ObjectHandle CreateInstance (ActivationContext activationContext, string [] activationCustomData)
167 throw new NotImplementedException ();
170 // Cross-domain instance creation
172 public static ObjectHandle CreateInstanceFrom (AppDomain domain, string assemblyFile, string typeName)
175 throw new ArgumentNullException ("domain");
176 return domain.CreateInstanceFrom (assemblyFile, typeName);
183 public static ObjectHandle CreateInstanceFrom (AppDomain domain, string assemblyFile, string typeName,
184 bool ignoreCase, BindingFlags bindingAttr, Binder binder,
185 object [] args, CultureInfo culture,
186 object [] activationAttributes,
187 Evidence securityAttributes)
190 throw new ArgumentNullException ("domain");
192 return domain.CreateInstanceFrom (assemblyFile, typeName, ignoreCase, bindingAttr, binder, args, culture, activationAttributes, securityAttributes);
195 public static ObjectHandle CreateInstance (AppDomain domain, string assemblyName, string typeName)
198 throw new ArgumentNullException ("domain");
199 return domain.CreateInstance (assemblyName, typeName);
205 public static ObjectHandle CreateInstance (AppDomain domain, string assemblyName, string typeName,
206 bool ignoreCase, BindingFlags bindingAttr, Binder binder,
207 object [] args, CultureInfo culture,
208 object [] activationAttributes,
209 Evidence securityAttributes)
212 throw new ArgumentNullException ("domain");
213 return domain.CreateInstance (assemblyName, typeName, ignoreCase, bindingAttr, binder, args, culture, activationAttributes, securityAttributes);
217 public static T CreateInstance <T> ()
219 return (T) CreateInstance (typeof (T));
222 public static object CreateInstance (Type type)
224 return CreateInstance (type, false);
227 public static object CreateInstance (Type type, params object [] args)
229 return CreateInstance (type, args, new object [0]);
232 public static object CreateInstance (Type type, object [] args, object [] activationAttributes)
234 return CreateInstance (type, BindingFlags.Default, Binder.DefaultBinder, args, null, activationAttributes);
237 public static object CreateInstance (Type type, BindingFlags bindingAttr, Binder binder, object [] args,
240 return CreateInstance (type, bindingAttr, binder, args, culture, new object [0]);
243 public static object CreateInstance (Type type, BindingFlags bindingAttr, Binder binder, object [] args,
244 CultureInfo culture, object [] activationAttributes)
248 if (type.ContainsGenericParameters)
249 throw new ArgumentException (type + " is an open generic type", "type");
251 // It seems to apply the same rules documented for InvokeMember: "If the type of lookup
252 // is omitted, BindingFlags.Public | BindingFlags.Instance will apply".
253 if ((bindingAttr & _accessFlags) == 0)
254 bindingAttr |= BindingFlags.Public | BindingFlags.Instance;
258 length = args.Length;
260 Type[] atypes = length == 0 ? Type.EmptyTypes : new Type [length];
261 for (int i = 0; i < length; ++i)
262 if (args [i] != null)
263 atypes [i] = args [i].GetType ();
266 binder = Binder.DefaultBinder;
268 ConstructorInfo ctor = (ConstructorInfo) binder.SelectMethod (bindingAttr, type.GetConstructors (bindingAttr), atypes, null);
271 // Not sure about this
272 if (type.IsValueType && atypes.Length == 0) {
273 return CreateInstanceInternal (type);
276 StringBuilder sb = new StringBuilder ();
277 foreach (Type t in atypes){
278 sb.Append (t != null ? t.ToString () : "(unknown)");
284 throw new MissingMethodException (String.Format (Locale.GetText ("No constructor found for {0}::.ctor({1})"),
288 CheckAbstractType (type);
290 if (activationAttributes != null && activationAttributes.Length > 0) {
291 if (!type.IsMarshalByRef) {
292 string msg = Locale.GetText ("Type '{0}' doesn't derive from MarshalByRefObject.", type.FullName);
293 throw new NotSupportedException (msg);
295 object newOb = ActivationServices.CreateProxyFromAttributes (type, activationAttributes);
298 ctor.Invoke (newOb, bindingAttr, binder, args, culture);
303 return ctor.Invoke (bindingAttr, binder, args, culture);
306 public static object CreateInstance (Type type, bool nonPublic)
310 if (type.ContainsGenericParameters)
311 throw new ArgumentException (type + " is an open generic type", "type");
313 CheckAbstractType (type);
315 ConstructorInfo ctor;
316 MonoType monoType = type as MonoType;
318 if (monoType != null) {
319 ctor = monoType.GetDefaultConstructor ();
320 if (!nonPublic && ctor != null && !ctor.IsPublic)
323 BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
325 flags |= BindingFlags.NonPublic;
326 ctor = type.GetConstructor (flags, null, CallingConventions.Any, Type.EmptyTypes, null);
330 if (type.IsValueType)
331 return CreateInstanceInternal (type);
333 throw new MissingMethodException (Locale.GetText ("Default constructor not found for type " +
334 type.FullName + "."));
337 return ctor.Invoke (null);
340 private static void CheckType (Type type)
343 throw new ArgumentNullException ("type");
345 if ((type == typeof (TypedReference)) || (type == typeof (ArgIterator)) || (type == typeof (void)) ||
346 (type == typeof (RuntimeArgumentHandle))) {
347 string msg = Locale.GetText ("CreateInstance cannot be used to create this type ({0}).", type.FullName);
348 throw new NotSupportedException (msg);
352 private static void CheckAbstractType (Type type)
354 if (type.IsAbstract) {
355 string msg = Locale.GetText ("Cannot create an abstract class '{0}'.", type.FullName);
356 throw new MissingMethodException (msg);
361 [SecurityPermission (SecurityAction.LinkDemand, RemotingConfiguration = true)]
362 public static object GetObject (Type type, string url)
365 throw new ArgumentNullException ("type");
367 return RemotingServices.Connect (type, url);
370 [SecurityPermission (SecurityAction.LinkDemand, RemotingConfiguration = true)]
371 public static object GetObject (Type type, string url, object state)
374 throw new ArgumentNullException ("type");
376 return RemotingServices.Connect (type, url, state);
379 [MethodImplAttribute (MethodImplOptions.InternalCall)]
380 internal static extern object CreateInstanceInternal (Type type);
382 void _Activator.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
384 throw new NotImplementedException ();
387 void _Activator.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
389 throw new NotImplementedException ();
392 void _Activator.GetTypeInfoCount (out uint pcTInfo)
394 throw new NotImplementedException ();
397 void _Activator.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
398 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
400 throw new NotImplementedException ();
404 public static ObjectHandle CreateInstance (string assemblyName, string typeName, bool ignoreCase,
405 BindingFlags bindingAttr, Binder binder, object [] args,
406 CultureInfo culture, object [] activationAttributes)
408 Assembly assembly = null;
409 if(assemblyName == null)
410 assembly = Assembly.GetCallingAssembly ();
412 assembly = Assembly.Load (assemblyName);
413 Type type = assembly.GetType (typeName, true, ignoreCase);
414 object obj = CreateInstance (type, bindingAttr, binder, args, culture, activationAttributes);
415 return (obj != null) ? new ObjectHandle (obj) : null;
418 public static ObjectHandle CreateInstance (AppDomain domain, string assemblyName, string typeName,
419 bool ignoreCase, BindingFlags bindingAttr, Binder binder,
420 object [] args, CultureInfo culture,
421 object [] activationAttributes)
424 throw new ArgumentNullException ("domain");
425 return domain.CreateInstance (assemblyName, typeName, ignoreCase, bindingAttr, binder, args, culture, activationAttributes);
428 public static ObjectHandle CreateInstanceFrom (string assemblyFile, string typeName, bool ignoreCase,
429 BindingFlags bindingAttr, Binder binder, object [] args,
430 CultureInfo culture, object [] activationAttributes)
432 Assembly assembly = Assembly.LoadFrom (assemblyFile);
433 if (assembly == null)
436 Type type = assembly.GetType (typeName, true, ignoreCase);
440 object obj = CreateInstance (type, bindingAttr, binder, args, culture, activationAttributes);
441 return (obj != null) ? new ObjectHandle (obj) : null;
444 public static ObjectHandle CreateInstanceFrom (AppDomain domain, string assemblyFile, string typeName,
445 bool ignoreCase, BindingFlags bindingAttr, Binder binder,
446 object [] args, CultureInfo culture,
447 object [] activationAttributes)
450 throw new ArgumentNullException ("domain");
452 return domain.CreateInstanceFrom (assemblyFile, typeName, ignoreCase, bindingAttr, binder, args, culture, activationAttributes);