Share via


Activator.CreateInstance Method (Type, array<Object[])

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Shared Function CreateInstance ( _
    type As Type, _
    ParamArray args As Object() _
) As Object
public static Object CreateInstance(
    Type type,
    params Object[] args
)

Parameters

  • args
    Type: array<System.Object[]
    An array of arguments that match in number, order, and type the parameters of the constructor to invoke. If args is an empty array or nulla null reference (Nothing in Visual Basic), the constructor that takes no parameters (the default constructor) is invoked.

Return Value

Type: System.Object
A reference to the newly created object.

Exceptions

Exception Condition
ArgumentNullException

type is nulla null reference (Nothing in Visual Basic).

ArgumentException

type is not a RuntimeType.

-or-

type is an open generic type (that is, the ContainsGenericParameters property returns true).

NotSupportedException

type is a TypeBuilder.

-or-

Creation of TypedReference, Void, and RuntimeArgumentHandle types, or arrays of those types, is not supported.

-or-

The constructor that best matches args has varargs arguments.

TargetInvocationException

The constructor being called throws an exception.

MethodAccessException

The caller does not have permission to call this constructor.

MemberAccessException

Cannot create an instance of an abstract class, or this member was invoked with a late-binding mechanism.

MissingMethodException

No matching public constructor was found.

COMException

type is a COM object but the class identifier used to obtain the type is invalid, or the identified class is not registered.

TypeLoadException

type is not a valid type.

Remarks

The type to be created must be accessible. The constructor to be invoked must be accessible and must provide the most specific match with the specified argument list. For example, if you are using AssemblyBuilder to create a dynamic assembly, you can invoke an internal constructor (Friend constructor in Visual Basic) for a type defined within the dynamic assembly. However, if you are using DynamicMethod to create a dynamic method, you cannot invoke internal constructors, because the dynamic method is hosted in an anonymous module in a system-provided assembly.

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 For types that do not have constructors defined, the Activator.CreateInstance(Type, array<Object[]) method throws a MemberAccessException exception instead of a MissingMethodException exception.

If you pass a null object to Activator.CreateInstance(Type, array<Object[]) on Silverlight for Windows Phone, the method throws NullReferenceException instead of NotSupportedException.

Examples

The following code example demonstrates how to call the CreateInstance(Type, array<Object[]) method. This code example is part of a larger example provided for the Activator class.

' Create an instance of StringBuilder using the constructor that takes a 
' string.
o = Activator.CreateInstance(sbType, New Object() { "Hello, there." })

' Append a string to the StringBuilder object and display the StringBuilder,
' late bound.
sbType.InvokeMember("Append", _
   BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.InvokeMethod, _
   Type.DefaultBinder, _
   o, New Object() { " And hello again!" })
outputBlock.Text &= o.ToString() & vbCrLf
// Create an instance of StringBuilder using the constructor that takes a 
// string.
o = Activator.CreateInstance(sbType, new object[]{"Hello, there."});

// Append a string to the StringBuilder object and display the StringBuilder,
// late bound.
sbType.InvokeMember("Append", 
   BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, 
   Type.DefaultBinder, 
   o, new object[] {" And hello again!"});
outputBlock.Text += o.ToString() + "\n";

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.