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;
44 [ClassInterface (ClassInterfaceType.None)]
47 [ComDefaultInterface (typeof (_Activator))]
49 public sealed class Activator : _Activator
51 const BindingFlags _flags = BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.Instance;
52 const BindingFlags _accessFlags = BindingFlags.DeclaredOnly | BindingFlags.FlattenHierarchy | BindingFlags.IgnoreCase |
53 BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public |
61 public static ObjectHandle CreateComInstanceFrom (string assemblyName, string typeName)
63 if (assemblyName == null)
64 throw new ArgumentNullException ("assemblyName");
67 throw new ArgumentNullException ("typeName");
69 if (assemblyName.Length == 0)
70 throw new ArgumentException ("assemblyName");
72 throw new NotImplementedException();
77 public static ObjectHandle CreateComInstanceFrom (string assemblyName, string typeName,
78 byte []hashValue, AssemblyHashAlgorithm hashAlgorithm)
80 if (assemblyName == null)
81 throw new ArgumentNullException ("assemblyName");
84 throw new ArgumentNullException ("typeName");
86 if (assemblyName.Length == 0)
87 throw new ArgumentException ("assemblyName");
89 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);
104 public static ObjectHandle CreateInstanceFrom (string assemblyFile, string typeName, bool ignoreCase,
105 BindingFlags bindingAttr, Binder binder, object [] args,
106 CultureInfo culture, object [] activationAttributes,
107 Evidence securityInfo)
109 Assembly assembly = Assembly.LoadFrom (assemblyFile, securityInfo);
110 if (assembly == null)
113 Type type = assembly.GetType (typeName, true, ignoreCase);
117 object obj = CreateInstance (type, bindingAttr, binder, args, culture, activationAttributes);
118 return (obj != null) ? new ObjectHandle (obj) : null;
121 public static ObjectHandle CreateInstance (string assemblyName, string typeName)
123 if (assemblyName == null)
124 assemblyName = Assembly.GetCallingAssembly ().GetName ().Name;
126 return Activator.CreateInstance (assemblyName, typeName, null);
129 public static ObjectHandle CreateInstance (string assemblyName, string typeName, object [] activationAttributes)
131 if (assemblyName == null)
132 assemblyName = Assembly.GetCallingAssembly ().GetName ().Name;
134 return Activator.CreateInstance (assemblyName, typeName, false, _flags, null, null, null,
135 activationAttributes, null);
138 public static ObjectHandle CreateInstance (string assemblyName, string typeName, bool ignoreCase,
139 BindingFlags bindingAttr, Binder binder, object [] args,
140 CultureInfo culture, object [] activationAttributes, Evidence securityInfo)
142 Assembly assembly = null;
143 if(assemblyName == null)
144 assembly = Assembly.GetCallingAssembly ();
146 assembly = Assembly.Load (assemblyName, securityInfo);
147 Type type = assembly.GetType (typeName, true, ignoreCase);
148 object obj = CreateInstance (type, bindingAttr, binder, args, culture, activationAttributes);
149 return (obj != null) ? new ObjectHandle (obj) : null;
152 public static object CreateInstance (Type type)
154 return CreateInstance (type, false);
157 public static object CreateInstance (Type type, object [] args)
159 return CreateInstance (type, args, new object [0]);
162 public static object CreateInstance (Type type, object [] args, object [] activationAttributes)
165 throw new ArgumentNullException ("type");
169 length = args.Length;
171 Type [] atypes = new Type [length];
172 for (int i = 0; i < length; ++i)
173 if (args [i] != null)
174 atypes [i] = args [i].GetType ();
176 ConstructorInfo ctor = type.GetConstructor (atypes);
178 if (type.IsValueType && atypes.Length == 0)
179 return CreateInstanceInternal (type);
181 throw new MissingMethodException (Locale.GetText ("Constructor not found. Class") +
187 throw new MissingMethodException (Locale.GetText ("Cannot create an abstract class. Class name: ") +
190 throw new MemberAccessException (Locale.GetText ("Cannot create an abstract class. Class name: ") +
194 if (activationAttributes != null && activationAttributes.Length > 0 && type.IsMarshalByRef) {
195 object newOb = ActivationServices.CreateProxyFromAttributes (type, activationAttributes);
197 return ctor.Invoke (newOb, args);
200 return ctor.Invoke (args);
203 public static object CreateInstance (Type type, BindingFlags bindingAttr, Binder binder, object [] args,
206 return CreateInstance (type, bindingAttr, binder, args, culture, new object [0]);
209 public static object CreateInstance (Type type, BindingFlags bindingAttr, Binder binder, object [] args,
210 CultureInfo culture, object [] activationAttributes)
213 throw new ArgumentNullException ("type");
215 // It seems to apply the same rules documented for InvokeMember: "If the type of lookup
216 // is omitted, BindingFlags.Public | BindingFlags.Instance will apply".
217 if ((bindingAttr & _accessFlags) == 0)
218 bindingAttr |= BindingFlags.Public | BindingFlags.Instance;
222 length = args.Length;
224 Type[] atypes = new Type [length];
225 for (int i = 0; i < length; ++i)
226 if (args [i] != null)
227 atypes [i] = args [i].GetType ();
229 ConstructorInfo ctor = type.GetConstructor (bindingAttr, binder, atypes, null);
231 // Not sure about this
232 if (type.IsValueType && atypes.Length == 0) {
233 return CreateInstanceInternal (type);
236 throw new MissingMethodException (Locale.GetText ("Constructor not found. Class: ") +
242 throw new MissingMethodException (Locale.GetText ("Cannot create an abstract class. Class name: ") +
245 throw new MemberAccessException (Locale.GetText ("Cannot create an abstract class. Class name: ") +
249 if (activationAttributes != null && activationAttributes.Length > 0 && type.IsMarshalByRef) {
250 object newOb = ActivationServices.CreateProxyFromAttributes (type, activationAttributes);
252 return ctor.Invoke (newOb, bindingAttr, binder, args, culture);
255 return ctor.Invoke (bindingAttr, binder, args, culture);
258 public static object CreateInstance (Type type, bool nonPublic)
261 throw new ArgumentNullException ("type");
265 throw new MissingMethodException (Locale.GetText ("Cannot create an abstract class. Class name: ") +
268 throw new MemberAccessException (Locale.GetText ("Cannot create an abstract class. Class name: ") +
272 BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
274 flags |= BindingFlags.NonPublic;
276 ConstructorInfo ctor = type.GetConstructor (flags, null, CallingConventions.Any, Type.EmptyTypes, null);
279 if (type.IsValueType)
280 return CreateInstanceInternal (type);
282 throw new MissingMethodException (Locale.GetText ("Default constructor not found."),
283 ".ctor() of " + type.FullName);
286 return ctor.Invoke (null);
289 [SecurityPermission (SecurityAction.LinkDemand, RemotingConfiguration = true)]
290 public static object GetObject (Type type, string url)
293 throw new ArgumentNullException ("type");
295 return RemotingServices.Connect (type, url);
298 [SecurityPermission (SecurityAction.LinkDemand, RemotingConfiguration = true)]
299 public static object GetObject (Type type, string url, object state)
302 throw new ArgumentNullException ("type");
304 return RemotingServices.Connect (type, url, state);
307 [MethodImplAttribute (MethodImplOptions.InternalCall)]
308 internal static extern object CreateInstanceInternal (Type type);
311 void _Activator.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
313 throw new NotImplementedException ();
316 void _Activator.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
318 throw new NotImplementedException ();
321 void _Activator.GetTypeInfoCount (out uint pcTInfo)
323 throw new NotImplementedException ();
326 void _Activator.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
327 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
329 throw new NotImplementedException ();