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)
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System
.Globalization
;
36 using System
.Reflection
;
37 using System
.Runtime
.Remoting
;
38 using System
.Runtime
.Remoting
.Activation
;
39 using System
.Runtime
.CompilerServices
;
40 using System
.Security
.Policy
;
41 using System
.Configuration
.Assemblies
;
45 public sealed class Activator
47 const BindingFlags _flags
= BindingFlags
.CreateInstance
| BindingFlags
.Public
| BindingFlags
.Instance
;
48 const BindingFlags _accessFlags
= BindingFlags
.DeclaredOnly
| BindingFlags
.FlattenHierarchy
| BindingFlags
.IgnoreCase
|
49 BindingFlags
.Instance
| BindingFlags
.NonPublic
| BindingFlags
.Public
|
57 public static ObjectHandle
CreateComInstanceFrom (string assemblyName
, string typeName
)
59 if (assemblyName
== null)
60 throw new ArgumentNullException ("assemblyName");
63 throw new ArgumentNullException ("typeName");
65 throw new NotImplementedException();
70 public static ObjectHandle
CreateComInstanceFrom (string assemblyName
, string typeName
,
71 byte []hashValue
, AssemblyHashAlgorithm hashAlgorithm
)
73 if (assemblyName
== null)
74 throw new ArgumentNullException ("assemblyName");
77 throw new ArgumentNullException ("typeName");
79 throw new NotImplementedException();
83 public static ObjectHandle
CreateInstanceFrom (string assemblyFile
, string typeName
)
85 return CreateInstanceFrom (assemblyFile
, typeName
, null);
88 public static ObjectHandle
CreateInstanceFrom (string assemblyFile
, string typeName
, object [] activationAttributes
)
90 return Activator
.CreateInstanceFrom (assemblyFile
, typeName
, false, _flags
, null, null, null,
91 activationAttributes
, null);
94 [MonoTODO ("security")]
95 public static ObjectHandle
CreateInstanceFrom (string assemblyFile
, string typeName
, bool ignoreCase
,
96 BindingFlags bindingAttr
, Binder binder
, object [] args
,
97 CultureInfo culture
, object [] activationAttributes
,
98 Evidence securityInfo
)
100 //TODO: when Assembly implements security, use it.
101 //Assembly assembly = Assembly.LoadFrom (assemblyFile, securityInfo);
102 Assembly assembly
= Assembly
.LoadFrom (assemblyFile
);
103 if (assembly
== null)
106 Type type
= assembly
.GetType (typeName
, true, ignoreCase
);
110 object obj
= CreateInstance (type
, bindingAttr
, binder
, args
, culture
, activationAttributes
);
111 return (obj
!= null) ? new ObjectHandle (obj
) : null;
114 public static ObjectHandle
CreateInstance (string assemblyName
, string typeName
)
116 if (assemblyName
== null)
117 assemblyName
= Assembly
.GetCallingAssembly ().GetName ().Name
;
119 return Activator
.CreateInstance (assemblyName
, typeName
, null);
122 public static ObjectHandle
CreateInstance (string assemblyName
, string typeName
, object [] activationAttributes
)
124 if (assemblyName
== null)
125 assemblyName
= Assembly
.GetCallingAssembly ().GetName ().Name
;
127 return Activator
.CreateInstance (assemblyName
, typeName
, false, _flags
, null, null, null,
128 activationAttributes
, null);
131 [MonoTODO ("security")]
132 public static ObjectHandle
CreateInstance (string assemblyName
, string typeName
, bool ignoreCase
,
133 BindingFlags bindingAttr
, Binder binder
, object [] args
,
134 CultureInfo culture
, object [] activationAttributes
, Evidence securityInfo
)
136 //TODO: when Assembly implements security, use it.
137 //Assembly assembly = Assembly.Load (assemblyFile, securityInfo);
138 Assembly assembly
= null;
139 if(assemblyName
== null)
140 assembly
= Assembly
.GetCallingAssembly ();
142 assembly
= Assembly
.Load (assemblyName
);
143 Type type
= assembly
.GetType (typeName
, true, ignoreCase
);
144 object obj
= CreateInstance (type
, bindingAttr
, binder
, args
, culture
, activationAttributes
);
145 return (obj
!= null) ? new ObjectHandle (obj
) : null;
148 public static object CreateInstance (Type type
)
150 return CreateInstance (type
, false);
153 public static object CreateInstance (Type type
, object [] args
)
155 return CreateInstance (type
, args
, new object [0]);
158 public static object CreateInstance (Type type
, object [] args
, object [] activationAttributes
)
161 throw new ArgumentNullException ("type");
164 throw new MemberAccessException (Locale
.GetText ("Cannot create an abstract class."));
168 length
= args
.Length
;
170 Type
[] atypes
= new Type
[length
];
171 for (int i
= 0; i
< length
; ++i
)
172 if (args
[i
] != null)
173 atypes
[i
] = args
[i
].GetType ();
175 ConstructorInfo ctor
= type
.GetConstructor (atypes
);
177 if (type
.IsValueType
&& atypes
.Length
== 0)
178 return CreateInstanceInternal (type
);
180 throw new MissingMethodException (Locale
.GetText ("Constructor not found."));
183 if (activationAttributes
!= null && activationAttributes
.Length
> 0 && type
.IsMarshalByRef
) {
184 object newOb
= ActivationServices
.CreateProxyFromAttributes (type
, activationAttributes
);
186 return ctor
.Invoke (newOb
, args
);
189 return ctor
.Invoke (args
);
192 public static object CreateInstance (Type type
, BindingFlags bindingAttr
, Binder binder
, object [] args
,
195 return CreateInstance (type
, bindingAttr
, binder
, args
, culture
, new object [0]);
198 public static object CreateInstance (Type type
, BindingFlags bindingAttr
, Binder binder
, object [] args
,
199 CultureInfo culture
, object [] activationAttributes
)
202 throw new ArgumentNullException ("type");
205 throw new MemberAccessException (Locale
.GetText ("Cannot create an abstract class."));
207 // It seems to apply the same rules documented for InvokeMember: "If the type of lookup
208 // is omitted, BindingFlags.Public | BindingFlags.Instance will apply".
209 if ((bindingAttr
& _accessFlags
) == 0)
210 bindingAttr
|= BindingFlags
.Public
| BindingFlags
.Instance
;
214 length
= args
.Length
;
216 Type
[] atypes
= new Type
[length
];
217 for (int i
= 0; i
< length
; ++i
)
218 if (args
[i
] != null)
219 atypes
[i
] = args
[i
].GetType ();
221 ConstructorInfo ctor
= type
.GetConstructor (bindingAttr
, binder
, atypes
, null);
223 // Not sure about this
224 if (type
.IsValueType
&& atypes
.Length
== 0) {
225 return CreateInstanceInternal (type
);
228 throw new MissingMethodException (Locale
.GetText ("Constructor not found."));
231 if (activationAttributes
!= null && activationAttributes
.Length
> 0 && type
.IsMarshalByRef
) {
232 object newOb
= ActivationServices
.CreateProxyFromAttributes (type
, activationAttributes
);
234 return ctor
.Invoke (newOb
, bindingAttr
, binder
, args
, culture
);
237 return ctor
.Invoke (bindingAttr
, binder
, args
, culture
);
240 public static object CreateInstance (Type type
, bool nonPublic
)
243 throw new ArgumentNullException ("type");
246 throw new MemberAccessException (Locale
.GetText ("Cannot create an abstract class."));
248 BindingFlags flags
= BindingFlags
.Public
| BindingFlags
.Instance
;
250 flags
|= BindingFlags
.NonPublic
;
252 ConstructorInfo ctor
= type
.GetConstructor (flags
, null, CallingConventions
.Any
, Type
.EmptyTypes
, null);
255 if (type
.IsValueType
)
256 return CreateInstanceInternal (type
);
258 throw new MissingMethodException (Locale
.GetText ("Default constructor not found."));
261 return ctor
.Invoke (null);
264 public static object GetObject (Type type
, string url
)
266 return RemotingServices
.Connect (type
, url
);
269 public static object GetObject (Type type
, string url
, object state
)
271 return RemotingServices
.Connect (type
, url
, state
);
274 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
275 private static extern object CreateInstanceInternal (Type type
);