Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Activator.CreateInstance Method

Creates an instance of the specified type using the constructor that best matches the specified parameters.

Overload List

Creates an instance of the specified type using the constructor that best matches the specified parameter.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function CreateInstance(Type) As Object
[C#] public static object CreateInstance(Type);
[C++] public: static Object* CreateInstance(Type*);
[JScript] public static function CreateInstance(Type) : Object;

Creates an instance of the type whose name is specified, using the named assembly and default constructor.

[Visual Basic] Overloads Public Shared Function CreateInstance(String, String) As ObjectHandle
[C#] public static ObjectHandle CreateInstance(string, string);
[C++] public: static ObjectHandle* CreateInstance(String*, String*);
[JScript] public static function CreateInstance(String, String) : ObjectHandle;

Creates an instance of the specified type using the constructor that best matches the specified parameter.

[Visual Basic] Overloads Public Shared Function CreateInstance(Type, Boolean) As Object
[C#] public static object CreateInstance(Type, bool);
[C++] public: static Object* CreateInstance(Type*, bool);
[JScript] public static function CreateInstance(Type, Boolean) : Object;

Creates an instance of the specified type using the constructor that best matches the specified parameters.

[Visual Basic] Overloads Public Shared Function CreateInstance(Type, Object()) As Object
[C#] public static object CreateInstance(Type, object[]);
[C++] public: static Object* CreateInstance(Type*, Object[]);
[JScript] public static function CreateInstance(Type, Object[]) : Object;

Creates an instance of the type whose name is specified, using the named assembly and default constructor.

[Visual Basic] Overloads Public Shared Function CreateInstance(String, String, Object()) As ObjectHandle
[C#] public static ObjectHandle CreateInstance(string, string, object[]);
[C++] public: static ObjectHandle* CreateInstance(String*, String*, Object[]);
[JScript] public static function CreateInstance(String, String, Object[]) : ObjectHandle;

Creates an instance of the specified type using the constructor that best matches the specified parameters.

[Visual Basic] Overloads Public Shared Function CreateInstance(Type, Object(), Object()) As Object
[C#] public static object CreateInstance(Type, object[], object[]);
[C++] public: static Object* CreateInstance(Type*, Object[], Object[]);
[JScript] public static function CreateInstance(Type, Object[], Object[]) : Object;

Creates an instance of the specified type using the constructor that best matches the specified parameters.

[Visual Basic] Overloads Public Shared Function CreateInstance(Type, BindingFlags, Binder, Object(), CultureInfo) As Object
[C#] public static object CreateInstance(Type, BindingFlags, Binder, object[], CultureInfo);
[C++] public: static Object* CreateInstance(Type*, BindingFlags, Binder*, Object[], CultureInfo*);
[JScript] public static function CreateInstance(Type, BindingFlags, Binder, Object[], CultureInfo) : Object;

Creates an instance of the specified type using the constructor that best matches the specified parameters.

[Visual Basic] Overloads Public Shared Function CreateInstance(Type, BindingFlags, Binder, Object(), CultureInfo, Object()) As Object
[C#] public static object CreateInstance(Type, BindingFlags, Binder, object[], CultureInfo, object[]);
[C++] public: static Object* CreateInstance(Type*, BindingFlags, Binder*, Object[], CultureInfo*, Object[]);
[JScript] public static function CreateInstance(Type, BindingFlags, Binder, Object[], CultureInfo, Object[]) : Object;

Creates an instance of the type whose name is specified, using the named assembly and the constructor that best matches the specified parameters.

[Visual Basic] Overloads Public Shared Function CreateInstance(String, String, Boolean, BindingFlags, Binder, Object(), CultureInfo, Object(), Evidence) As ObjectHandle
[C#] public static ObjectHandle CreateInstance(string, string, bool, BindingFlags, Binder, object[], CultureInfo, object[], Evidence);
[C++] public: static ObjectHandle* CreateInstance(String*, String*, bool, BindingFlags, Binder*, Object[], CultureInfo*, Object[], Evidence*);
[JScript] public static function CreateInstance(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[], Evidence) : ObjectHandle;

Example

[Visual Basic, C#, JScript] The following example demonstrates an overload of the CreateInstance method.

[Visual Basic, C#, JScript] For this code example to run, you must provide the fully qualified assembly name. For information about how to obtain the fully qualified assembly name, see Assembly Names.

[Visual Basic, C#, JScript] Note   This example shows how to use one of the overloaded versions of CreateInstance. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Dim hdlSample As ObjectHandle
Dim myExtenderInterface As IMyExtenderInterface
Dim argOne As String = "Value of argOne"
Dim argTwo As Integer = 7
Dim args As Object() =  {argOne, argTwo}
' Uses the UrlAttribute to create a remote object.
Dim activationAttributes() = {New UrlAttribute("http://localhost:9000/MySampleService")}
' Activates an object for this client.
' You must supply a valid fully qualified assembly name here.
hdlSample = Activator.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "samplenamespace.sampleclass", True, BindingFlags.Instance Or BindingFlags.Public, Nothing, args, Nothing, activationAttributes, Nothing)
myExtenderInterface = CType(hdlSample.Unwrap(), IMyExtenderInterface)
Console.WriteLine(myExtenderInterface.SampleMethod("Bill"))

[C#] 
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.
// You must supply a valid fully qualified assembly name here. 
hdlSample = Activator.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken",
                                     "samplenamespace.sampleclass", 
                                     true,
                                     BindingFlags.Instance|BindingFlags.Public,
                                     null,
                                     args,
                                     null,
                                     activationAttributes,
                                     null);
myExtenderInterface = (IMyExtenderInterface)hdlSample.Unwrap();
Console.WriteLine(myExtenderInterface.SampleMethod("Bill"));

[JScript] 
var hdlSample : ObjectHandle;
var myExtenderInterface : IMyExtenderInterface;
var argOne : String = "Value of argOne";
var argTwo : int = 7;
var args : Object[] = [argOne, argTwo];

// Uses the UrlAttribute to create a remote object.
var activationAttributes : Object[] = [new UrlAttribute("http://localhost:9000/MySampleService")];

// Activates an object for this client.
// You must supply a valid fully qualified assembly name here. 
hdlSample = Activator.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken",
                                     "samplenamespace.sampleclass", 
                                     true,
                                     BindingFlags.Instance|BindingFlags.Public,
                                     null,
                                     args,
                                     null,
                                     activationAttributes,
                                     null);
myExtenderInterface = IMyExtenderInterface(hdlSample.Unwrap());
Console.WriteLine(myExtenderInterface.SampleMethod("Bill"));

[C++] No example is available for C++. To view a Visual Basic, C#, or JScript example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Activator Class | Activator Members | System Namespace

Show:
© 2014 Microsoft