public sealed class Activator
|
You must have sufficient permission to search for and call a constructor, otherwise and exception is thrown. By default, only public constructors are considered during the search for a constructor. If no constructor or default constructor can be found, an exception is thrown.
A binder parameter specifies an object that searches an assembly for a suitable constructor. You can specify your own binder and search criteria, but if no binder is specified a default binder is used. For more information, see the Binder and BindingFlags classes.
An evidence parameter affects the security policy and permissions for the constructor. For more information, see the Evidence class.
An instance of a type can be created at a local or remote site. If the type is created remotely, an activation attribute parameter specifies the URI of the remote site. The call to create the instance might pass through intermediary sites before it reaches the remote site. Other activation attributes can modify the environment, or context, in which the call operates at the remote and intermediary sites.
If the instance is created locally, a reference to that object is returned. If the instance is created remotely, a reference to a proxy is returned. The remote object is manipulated through the proxy as if it were a local object.
The Activator.GetObject method creates a proxy to a currently running remote object, server-activated well-known object, or XML Web service. You can specify the connection medium, that is, the channel. For more information, see the ChannelServices class.
Assemblies contain type definitions. The Activator.CreateInstance method creates an instance of a type from a currently running assembly. The Activator.CreateInstanceFrom method creates an instance from a file that contains an assembly. The System.Activator.CreateComInstanceFrom (not supported on the shared source CLI) method creates an instance of a COM object from a file that contains an assembly.
For more information about server-activated and client-activated objects, see the the conceptual topic at MSDN: serveractivation topic.
CreateInstance | Overloaded:CreateInstance(Type type) Creates an instance of the specified type using the constructor that best matches the specified parameter. |
CreateInstance | Overloaded:CreateInstance(string assemblyName, string typeName) Creates an instance of the type whose name is specified, using the named assembly and the constructor that best matches the default constructor. |
CreateInstance | Overloaded:CreateInstance(Type type, bool nonPublic) Creates an instance of the specified type using the constructor that best matches the specified parameter. |
CreateInstance | Overloaded:CreateInstance(Type type, object[] args) Creates an instance of the specified type using the constructor that best matches the specified parameters. |
CreateInstance | Overloaded:CreateInstance(string assemblyName, string typeName, object[] activationAttributes) Creates an instance of the type whose name is specified, using the named assembly and the constructor that best matches the default constructor. |
CreateInstance | Overloaded:CreateInstance(Type type, object[] args, object[] activationAttributes) Creates an instance of the specified type using the constructor that best matches the specified parameters. |
CreateInstance | Overloaded:CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture) Creates an instance of the specified type using the constructor that best matches the specified parameters. |
CreateInstance | Overloaded:CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes) Creates an instance of the specified type using the constructor that best matches the specified parameters. |
CreateInstance | Overloaded:CreateInstance(string assemblyName, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes, Evidence securityInfo) Creates an instance of the type whose name is specified, using the named assembly and the constructor that best matches the specified parameters. |
CreateInstanceFrom | Overloaded:CreateInstanceFrom(string assemblyFile, string typeName) Creates an instance of the type whose name is specified, using the named assembly file and default constructor. |
CreateInstanceFrom | Overloaded:CreateInstanceFrom(string assemblyFile, string typeName, object[] activationAttributes) Creates an instance of the type whose name is specified, using the named assembly file and default constructor. |
CreateInstanceFrom | Overloaded:CreateInstanceFrom(string assemblyFile, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes, Evidence securityInfo) Creates an instance of the type whose name is specified, using the named assembly file and the constructor that best matches the specified parameters. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetObject | Overloaded:GetObject(Type type, string url) Creates a proxy for the well-known object indicated by the specified type and URL. |
GetObject | Overloaded:GetObject(Type type, string url, object state) Creates a proxy for the well-known object indicated by the specified type, URL, and channel data. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
type
Exception Type | Condition |
---|---|
ArgumentNullException | type is null. |
ArgumentException | type is not a RuntimeType. |
NotSupportedException | type cannot be a TypeBuilder. |
TargetInvocationException | The constructor being called throws an exception. |
MethodAccessException | The caller does not have permission to call this constructor. |
MissingMethodException | No matching constructor was found. |
public static ObjectHandle CreateInstance( |
assemblyName
typeName
Exception Type | Condition |
---|---|
ArgumentNullException | typeName is null. |
MissingMethodException | No matching public constructor was found. |
TypeLoadException | typename was not found in assemblyName. |
FileNotFoundException | assemblyName was not found. |
MethodAccessException | The caller does not have permission to call this constructor. |
TargetInvocationException | The constructor, which was invoked through reflection, threw an exception. |
Use ObjectHandle.Unwrap to unwrap the return value.
type
nonPublic
Exception Type | Condition |
---|---|
ArgumentNullException | type is null. |
ArgumentException | type is not a RuntimeType. |
NotSupportedException | type cannot be a TypeBuilder. |
TargetInvocationException | The constructor being called throws an exception. |
MethodAccessException | The caller does not have permission to call this constructor. |
MissingMethodException | No matching constructor was found. |
type
args
Exception Type | Condition |
---|---|
ArgumentNullException | type is null. |
ArgumentException | type is not a RuntimeType. |
NotSupportedException | type cannot be a TypeBuilder. |
TargetInvocationException | The constructor being called throws an exception. |
MethodAccessException | The caller does not have permission to call this constructor. |
MissingMethodException | No matching constructor was found. |
public static ObjectHandle CreateInstance( |
assemblyName
typeName
activationAttributes
Exception Type | Condition |
---|---|
ArgumentNullException | typeName is null. |
MissingMethodException | No matching public constructor was found. |
TypeLoadException | typename was not found in assemblyName. |
FileNotFoundException | assemblyName was not found. |
MethodAccessException | The caller does not have permission to call this constructor. |
Use ObjectHandle.Unwrap to unwrap the return value.
public static object CreateInstance( |
type
args
activationAttributes
Exception Type | Condition |
---|---|
ArgumentNullException | type is null. |
ArgumentException | type is not a RuntimeType. |
NotSupportedException | type cannot be a TypeBuilder. |
TargetInvocationException | The constructor being called throws an exception. |
MethodAccessException | The caller does not have permission to call this constructor. |
MissingMethodException | No matching constructor was found. |
public static object CreateInstance( |
type
bindingAttr
binder
args
culture
Exception Type | Condition |
---|---|
ArgumentNullException | type is null. |
ArgumentException | type is not a RuntimeType. |
NotSupportedException | type cannot be a TypeBuilder. |
TargetInvocationException | The constructor being called throws an exception. |
MethodAccessException | The caller does not have permission to call this constructor. |
MissingMethodException | No matching constructor was found. |
public static object CreateInstance( |
type
bindingAttr
binder
args
culture
activationAttributes
Exception Type | Condition |
---|---|
ArgumentNullException | type is null. |
ArgumentException | type is not a RuntimeType. |
NotSupportedException | type cannot be a TypeBuilder. |
TargetInvocationException | The constructor being called throws an exception. |
MethodAccessException | The caller does not have permission to call this constructor. |
MissingMethodException | No matching constructor was found. |
ObjectHandle hdlSample; IMyExtenderInterface myExtenderInterface; string argOne = "Value of argOne"; int argTwo = 7; object[] args = {argOne, argTwo}; // Uses the UrlAttribute to create a remote object. object[] activationAttributes = {new UrlAttribute("http://localhost:9000/MySampleService")}; // Activates an object for this client. hdlSample = Activator.CreateInstance("SampleAssembly", "samplenamespace.sampleclass", true, BindingFlags.Instance|BindingFlags.Public, null, args, null, activationAttributes, null); myExtenderInterface = (IMyExtenderInterface)hdlSample.Unwrap(); Console.WriteLine(myExtenderInterface.SampleMethod("Bill"));
public static ObjectHandle CreateInstance( |
assemblyName
typeName
ignoreCase
bindingAttr
binder
args
culture
activationAttributes
securityInfo
Exception Type | Condition |
---|---|
ArgumentNullException | typeName is null. |
MissingMethodException | No matching constructor was found. |
TypeLoadException | typename was not found in assemblyName. |
FileNotFoundException | assemblyName was not found. |
MethodAccessException | The caller does not have permission to call this constructor. |
TargetInvocationException | The constructor, which was invoked through reflection, threw an exception. |
Use ObjectHandle.Unwrap to unwrap the return value.
public static ObjectHandle CreateInstanceFrom( |
assemblyFile
typeName
-or-
null, if the requested type could not be created.
Exception Type | Condition |
---|---|
ArgumentNullException | typeName is null. |
MissingMethodException | No matching public constructor was found. |
TypeLoadException | typename was not found in assemblyFile. |
FileNotFoundException | assemblyFile was not found. |
MethodAccessException | The caller does not have permission to call this constructor. |
TargetInvocationException | The constructor, which was invoked through reflection, threw an exception. |
Use the ObjectHandle.Unwrap method to unwrap the return value.
For information about other exceptions that can be thrown by invoked methods, see the Exceptions section of the Assembly.LoadFrom and Activator.CreateInstance methods.
public static ObjectHandle CreateInstanceFrom( |
assemblyFile
typeName
activationAttributes
Exception Type | Condition |
---|---|
ArgumentNullException | typeName is null. |
MissingMethodException | No matching public constructor was found. |
TypeLoadException | typename was not found in assemblyFile. |
FileNotFoundException | assemblyFile was not found. |
MethodAccessException | The caller does not have permission to call this constructor. |
TargetInvocationException | The constructor, which was invoked through reflection, threw an exception. |
Use the ObjectHandle.Unwrap method to unwrap the return value.
For information about other exceptions that can be thrown by invoked methods, see the Exceptions section of the Assembly.LoadFrom and Activator.CreateInstance methods.
ObjectHandle hdlSample; IMyExtenderInterface myExtenderInterface; object[] activationAttributes = {new SynchronizationAttribute()}; // Assumes that SampleAssembly.dll exists in the same directory as this assembly. hdlSample = Activator.CreateInstanceFrom("SampleAssembly.dll", "SampleNamespace.SampleClass", activationAttributes); // Assumes that the SampleClass implements an interface provided by // this application. myExtenderInterface = (IMyExtenderInterface)hdlSample.Unwrap(); Console.WriteLine(myExtenderInterface.SampleMethod("Bill"));
public static ObjectHandle CreateInstanceFrom( |
assemblyFile
typeName
ignoreCase
bindingAttr
binder
args
culture
activationAttributes
securityInfo
Exception Type | Condition |
---|---|
ArgumentNullException | typeName is null. |
MissingMethodException | No matching constructor was found. |
TypeLoadException | typename was not found in assemblyFile. |
FileNotFoundException | assemblyFile was not found. |
MethodAccessException | The caller does not have permission to call this constructor. |
TargetInvocationException | The constructor, which was invoked through reflection, threw an exception. |
Use the ObjectHandle.Unwrap method to unwrap the return value.
For information about other exceptions that can be thrown by invoked methods, see the Exceptions section of the Assembly.LoadFrom and Activator.CreateInstance methods.
~Activator(); |
public virtual int GetHashCode(); |
type
url
Exception Type | Condition |
---|---|
ArgumentNullException | type or url is null. |
RemotingException | type is not marshaled by reference or an interface. |
type
url
state
Exception Type | Condition |
---|---|
ArgumentNullException | type or url is null. |
RemotingException | type is not marshaled by reference or an interface. |
The state parameter communicates information to the channel, and is passed to the IChannelSender.CreateMessageSink method.
public Type GetType(); |
protected object MemberwiseClone(); |
public virtual string ToString(); |