CreateInstance Method (Type, Object[])
This documentation is archived and is not being maintained.

Activator.CreateInstance Method (Type, Object[])

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

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

public static Object CreateInstance (
	Type type,
	params Object[] args
)
public static Object CreateInstance (
	Type type, 
	Object[] args
)
public static function CreateInstance (
	type : Type, 
	... args : Object[]
) : Object
Not applicable.

Parameters

type

The type of object to create.

args

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 a null reference (Nothing in Visual Basic), the constructor that takes no parameters (the default constructor) is invoked.

Return Value

A reference to the newly created object.

Exception typeCondition

ArgumentNullException

type is a 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 cannot be a TypeBuilder.

-or-

Creation of TypedReference, ArgIterator, 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.

InvalidComObjectException

The COM type was not obtained through GetTypeFromProgID or GetTypeFromCLSID.

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.

The constructor to be invoked must be accessible and must provide the most specific match with the specified argument list.

NoteNote:

Starting with the , this method can be used to access nonpublic types if the caller has been granted ReflectionPermission with the ReflectionPermissionFlag.ReflectionEmit flag and if the grant set of the assembly that contains the nonpublic types is restricted to the caller’s grant set, or to a subset thereof. (See Security Considerations for Reflection.)

To use this functionality, your application should target the . For more information, see The .NET Framework 3.5 Architecture.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Show:
© 2016 Microsoft