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
;
257 binder
= Binder
.DefaultBinder
;
260 ConstructorInfo ctor
= (ConstructorInfo
) binder
.BindToMethod (bindingAttr
, type
.GetConstructors (bindingAttr
), ref args
, null, null, null, out state
);
263 // Not sure about this
264 if (type
.IsValueType
&& (args
== null || args
.Length
== 0)) {
265 return CreateInstanceInternal (type
);
268 var sb
= new StringBuilder ();
270 for (int i
= 0; i
< args
.Length
; i
++) {
274 var argument
= args
[i
];
275 var arg_type
= argument
!= null ? argument
.GetType () : null;
276 sb
.Append (arg_type
!= null ? arg_type
.ToString () : "(unknown)");
280 throw new MissingMethodException (String
.Format (Locale
.GetText ("No constructor found for {0}::.ctor({1})"),
284 CheckAbstractType (type
);
286 if (activationAttributes
!= null && activationAttributes
.Length
> 0) {
287 if (!type
.IsMarshalByRef
) {
288 string msg
= Locale
.GetText ("Type '{0}' doesn't derive from MarshalByRefObject.", type
.FullName
);
289 throw new NotSupportedException (msg
);
291 object newOb
= ActivationServices
.CreateProxyFromAttributes (type
, activationAttributes
);
294 ctor
.Invoke (newOb
, bindingAttr
, binder
, args
, culture
);
299 return ctor
.Invoke (bindingAttr
, binder
, args
, culture
);
302 public static object CreateInstance (Type type
, bool nonPublic
)
306 if (type
.ContainsGenericParameters
)
307 throw new ArgumentException (type
+ " is an open generic type", "type");
309 CheckAbstractType (type
);
311 ConstructorInfo ctor
;
312 MonoType monoType
= type
as MonoType
;
314 if (monoType
!= null) {
315 ctor
= monoType
.GetDefaultConstructor ();
316 if (!nonPublic
&& ctor
!= null && !ctor
.IsPublic
)
319 BindingFlags flags
= BindingFlags
.Public
| BindingFlags
.Instance
;
321 flags
|= BindingFlags
.NonPublic
;
322 ctor
= type
.GetConstructor (flags
, null, CallingConventions
.Any
, Type
.EmptyTypes
, null);
326 if (type
.IsValueType
)
327 return CreateInstanceInternal (type
);
329 throw new MissingMethodException (Locale
.GetText ("Default constructor not found for type " +
330 type
.FullName
+ "."));
333 return ctor
.Invoke (null);
336 private static void CheckType (Type type
)
339 throw new ArgumentNullException ("type");
341 if ((type
== typeof (TypedReference
)) || (type
== typeof (ArgIterator
)) || (type
== typeof (void)) ||
342 (type
== typeof (RuntimeArgumentHandle
))) {
343 string msg
= Locale
.GetText ("CreateInstance cannot be used to create this type ({0}).", type
.FullName
);
344 throw new NotSupportedException (msg
);
348 private static void CheckAbstractType (Type type
)
350 if (type
.IsAbstract
) {
351 string msg
= Locale
.GetText ("Cannot create an abstract class '{0}'.", type
.FullName
);
352 throw new MissingMethodException (msg
);
357 [SecurityPermission (SecurityAction
.LinkDemand
, RemotingConfiguration
= true)]
358 public static object GetObject (Type type
, string url
)
361 throw new ArgumentNullException ("type");
363 return RemotingServices
.Connect (type
, url
);
366 [SecurityPermission (SecurityAction
.LinkDemand
, RemotingConfiguration
= true)]
367 public static object GetObject (Type type
, string url
, object state
)
370 throw new ArgumentNullException ("type");
372 return RemotingServices
.Connect (type
, url
, state
);
375 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
376 internal static extern object CreateInstanceInternal (Type type
);
378 void _Activator
.GetIDsOfNames ([In
] ref Guid riid
, IntPtr rgszNames
, uint cNames
, uint lcid
, IntPtr rgDispId
)
380 throw new NotImplementedException ();
383 void _Activator
.GetTypeInfo (uint iTInfo
, uint lcid
, IntPtr ppTInfo
)
385 throw new NotImplementedException ();
388 void _Activator
.GetTypeInfoCount (out uint pcTInfo
)
390 throw new NotImplementedException ();
393 void _Activator
.Invoke (uint dispIdMember
, [In
] ref Guid riid
, uint lcid
, short wFlags
, IntPtr pDispParams
,
394 IntPtr pVarResult
, IntPtr pExcepInfo
, IntPtr puArgErr
)
396 throw new NotImplementedException ();
400 public static ObjectHandle
CreateInstance (string assemblyName
, string typeName
, bool ignoreCase
,
401 BindingFlags bindingAttr
, Binder binder
, object [] args
,
402 CultureInfo culture
, object [] activationAttributes
)
404 Assembly assembly
= null;
405 if(assemblyName
== null)
406 assembly
= Assembly
.GetCallingAssembly ();
408 assembly
= Assembly
.Load (assemblyName
);
409 Type type
= assembly
.GetType (typeName
, true, ignoreCase
);
410 object obj
= CreateInstance (type
, bindingAttr
, binder
, args
, culture
, activationAttributes
);
411 return (obj
!= null) ? new ObjectHandle (obj
) : null;
414 public static ObjectHandle
CreateInstance (AppDomain domain
, string assemblyName
, string typeName
,
415 bool ignoreCase
, BindingFlags bindingAttr
, Binder binder
,
416 object [] args
, CultureInfo culture
,
417 object [] activationAttributes
)
420 throw new ArgumentNullException ("domain");
421 return domain
.CreateInstance (assemblyName
, typeName
, ignoreCase
, bindingAttr
, binder
, args
, culture
, activationAttributes
);
424 public static ObjectHandle
CreateInstanceFrom (string assemblyFile
, string typeName
, bool ignoreCase
,
425 BindingFlags bindingAttr
, Binder binder
, object [] args
,
426 CultureInfo culture
, object [] activationAttributes
)
428 Assembly assembly
= Assembly
.LoadFrom (assemblyFile
);
429 if (assembly
== null)
432 Type type
= assembly
.GetType (typeName
, true, ignoreCase
);
436 object obj
= CreateInstance (type
, bindingAttr
, binder
, args
, culture
, activationAttributes
);
437 return (obj
!= null) ? new ObjectHandle (obj
) : null;
440 public static ObjectHandle
CreateInstanceFrom (AppDomain domain
, string assemblyFile
, string typeName
,
441 bool ignoreCase
, BindingFlags bindingAttr
, Binder binder
,
442 object [] args
, CultureInfo culture
,
443 object [] activationAttributes
)
446 throw new ArgumentNullException ("domain");
448 return domain
.CreateInstanceFrom (assemblyFile
, typeName
, ignoreCase
, bindingAttr
, binder
, args
, culture
, activationAttributes
);