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.Remoting;
35 using System.Runtime.Remoting.Activation;
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;
45 [ClassInterface (ClassInterfaceType.None)]
48 [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 |
61 [MonoTODO ("No COM support")]
62 public static ObjectHandle CreateComInstanceFrom (string assemblyName, string typeName)
64 if (assemblyName == null)
65 throw new ArgumentNullException ("assemblyName");
68 throw new ArgumentNullException ("typeName");
70 if (assemblyName.Length == 0)
71 throw new ArgumentException ("assemblyName");
73 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();
94 public static ObjectHandle CreateInstanceFrom (string assemblyFile, string typeName)
96 return CreateInstanceFrom (assemblyFile, typeName, null);
99 public static ObjectHandle CreateInstanceFrom (string assemblyFile, string typeName, object [] activationAttributes)
101 return Activator.CreateInstanceFrom (assemblyFile, typeName, false, _flags, null, null, null,
102 activationAttributes, null);
105 public static ObjectHandle CreateInstanceFrom (string assemblyFile, string typeName, bool ignoreCase,
106 BindingFlags bindingAttr, Binder binder, object [] args,
107 CultureInfo culture, object [] activationAttributes,
108 Evidence securityInfo)
110 Assembly assembly = Assembly.LoadFrom (assemblyFile, securityInfo);
111 if (assembly == null)
114 Type type = assembly.GetType (typeName, true, ignoreCase);
118 object obj = CreateInstance (type, bindingAttr, binder, args, culture, activationAttributes);
119 return (obj != null) ? new ObjectHandle (obj) : null;
122 public static ObjectHandle CreateInstance (string assemblyName, string typeName)
124 if (assemblyName == null)
125 assemblyName = Assembly.GetCallingAssembly ().GetName ().Name;
127 return Activator.CreateInstance (assemblyName, typeName, null);
130 public static ObjectHandle CreateInstance (string assemblyName, string typeName, object [] activationAttributes)
132 if (assemblyName == null)
133 assemblyName = Assembly.GetCallingAssembly ().GetName ().Name;
135 return Activator.CreateInstance (assemblyName, typeName, false, _flags, null, null, null,
136 activationAttributes, null);
139 public static ObjectHandle CreateInstance (string assemblyName, string typeName, bool ignoreCase,
140 BindingFlags bindingAttr, Binder binder, object [] args,
141 CultureInfo culture, object [] activationAttributes, Evidence securityInfo)
143 Assembly assembly = null;
144 if(assemblyName == null)
145 assembly = Assembly.GetCallingAssembly ();
147 assembly = Assembly.Load (assemblyName, securityInfo);
148 Type type = assembly.GetType (typeName, true, ignoreCase);
149 object obj = CreateInstance (type, bindingAttr, binder, args, culture, activationAttributes);
150 return (obj != null) ? new ObjectHandle (obj) : null;
154 [MonoNotSupported ("no ClickOnce in mono")]
155 public static ObjectHandle CreateInstance (ActivationContext activationContext)
157 throw new NotImplementedException ();
160 [MonoNotSupported ("no ClickOnce in mono")]
161 public static ObjectHandle CreateInstance (ActivationContext activationContext, string [] activationCustomData)
163 throw new NotImplementedException ();
166 // Cross-domain instance creation
168 public static ObjectHandle CreateInstanceFrom (AppDomain domain, string assemblyFile, string typeName)
171 throw new ArgumentNullException ("domain");
172 return domain.CreateInstanceFrom (assemblyFile, typeName);
175 public static ObjectHandle CreateInstanceFrom (AppDomain domain, string assemblyFile, string typeName,
176 bool ignoreCase, BindingFlags bindingAttr, Binder binder,
177 object [] args, CultureInfo culture,
178 object [] activationAttributes,
179 Evidence securityAttributes)
182 throw new ArgumentNullException ("domain");
184 return domain.CreateInstanceFrom (assemblyFile, typeName, ignoreCase, bindingAttr, binder, args, culture, activationAttributes, securityAttributes);
187 public static ObjectHandle CreateInstance (AppDomain domain, string assemblyName, string typeName)
190 throw new ArgumentNullException ("domain");
191 return domain.CreateInstance (assemblyName, typeName);
194 public static ObjectHandle CreateInstance (AppDomain domain, string assemblyName, string typeName,
195 bool ignoreCase, BindingFlags bindingAttr, Binder binder,
196 object [] args, CultureInfo culture,
197 object [] activationAttributes,
198 Evidence securityAttributes)
201 throw new ArgumentNullException ("domain");
202 return domain.CreateInstance (assemblyName, typeName, ignoreCase, bindingAttr, binder, args, culture, activationAttributes, securityAttributes);
205 public static T CreateInstance <T> ()
207 return (T) CreateInstance (typeof (T));
211 public static object CreateInstance (Type type)
213 return CreateInstance (type, false);
217 public static object CreateInstance (Type type, params object [] args)
219 public static object CreateInstance (Type type, object [] args)
222 return CreateInstance (type, args, new object [0]);
225 public static object CreateInstance (Type type, object [] args, object [] activationAttributes)
227 return CreateInstance (type, BindingFlags.Default, Binder.DefaultBinder, args, null, activationAttributes);
230 public static object CreateInstance (Type type, BindingFlags bindingAttr, Binder binder, object [] args,
233 return CreateInstance (type, bindingAttr, binder, args, culture, new object [0]);
236 public static object CreateInstance (Type type, BindingFlags bindingAttr, Binder binder, object [] args,
237 CultureInfo culture, object [] activationAttributes)
242 if (type.ContainsGenericParameters)
243 throw new ArgumentException (type + " is an open generic type", "type");
245 // It seems to apply the same rules documented for InvokeMember: "If the type of lookup
246 // is omitted, BindingFlags.Public | BindingFlags.Instance will apply".
247 if ((bindingAttr & _accessFlags) == 0)
248 bindingAttr |= BindingFlags.Public | BindingFlags.Instance;
252 length = args.Length;
254 Type[] atypes = length == 0 ? Type.EmptyTypes : new Type [length];
255 for (int i = 0; i < length; ++i)
256 if (args [i] != null)
257 atypes [i] = args [i].GetType ();
260 binder = Binder.DefaultBinder;
262 ConstructorInfo ctor = (ConstructorInfo) binder.SelectMethod (bindingAttr, type.GetConstructors (bindingAttr), atypes, null);
265 // Not sure about this
266 if (type.IsValueType && atypes.Length == 0) {
267 return CreateInstanceInternal (type);
270 StringBuilder sb = new StringBuilder ();
271 foreach (Type t in atypes){
272 sb.Append (t != null ? t.ToString () : "(unknown)");
278 throw new MissingMethodException (String.Format (Locale.GetText ("No constructor found for {0}::.ctor({1})"),
282 CheckAbstractType (type);
284 if (activationAttributes != null && activationAttributes.Length > 0) {
285 if (!type.IsMarshalByRef) {
286 string msg = Locale.GetText ("Type '{0}' doesn't derive from MarshalByRefObject.", type.FullName);
287 throw new NotSupportedException (msg);
289 object newOb = ActivationServices.CreateProxyFromAttributes (type, activationAttributes);
292 ctor.Invoke (newOb, bindingAttr, binder, args, culture);
297 return ctor.Invoke (bindingAttr, binder, args, culture);
300 public static object CreateInstance (Type type, bool nonPublic)
304 if (type.ContainsGenericParameters)
305 throw new ArgumentException (type + " is an open generic type", "type");
307 CheckAbstractType (type);
309 BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
311 flags |= BindingFlags.NonPublic;
313 ConstructorInfo ctor = type.GetConstructor (flags, null, CallingConventions.Any, Type.EmptyTypes, null);
316 if (type.IsValueType)
317 return CreateInstanceInternal (type);
319 throw new MissingMethodException (Locale.GetText ("Default constructor not found."),
320 ".ctor() of " + type.FullName);
323 return ctor.Invoke (null);
326 private static void CheckType (Type type)
329 throw new ArgumentNullException ("type");
331 if ((type == typeof (TypedReference)) || (type == typeof (ArgIterator)) || (type == typeof (void)) ||
332 (type == typeof (RuntimeArgumentHandle))) {
333 string msg = Locale.GetText ("CreateInstance cannot be used to create this type ({0}).", type.FullName);
334 throw new NotSupportedException (msg);
338 private static void CheckAbstractType (Type type)
340 if (type.IsAbstract) {
341 string msg = Locale.GetText ("Cannot create an abstract class '{0}'.", type.FullName);
343 throw new MissingMethodException (msg);
345 throw new MemberAccessException (msg);
350 [SecurityPermission (SecurityAction.LinkDemand, RemotingConfiguration = true)]
351 public static object GetObject (Type type, string url)
354 throw new ArgumentNullException ("type");
356 return RemotingServices.Connect (type, url);
359 [SecurityPermission (SecurityAction.LinkDemand, RemotingConfiguration = true)]
360 public static object GetObject (Type type, string url, object state)
363 throw new ArgumentNullException ("type");
365 return RemotingServices.Connect (type, url, state);
368 [MethodImplAttribute (MethodImplOptions.InternalCall)]
369 internal static extern object CreateInstanceInternal (Type type);
372 void _Activator.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
374 throw new NotImplementedException ();
377 void _Activator.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
379 throw new NotImplementedException ();
382 void _Activator.GetTypeInfoCount (out uint pcTInfo)
384 throw new NotImplementedException ();
387 void _Activator.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
388 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
390 throw new NotImplementedException ();