This documentation is archived and is not being maintained.

Activator.CreateInstance Method (Type)

Creates an instance of the specified type using that type's default constructor.

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

public static Object CreateInstance (
	Type type
)
public static Object CreateInstance (
	Type type
)
public static function CreateInstance (
	type : Type
) : Object
Not applicable.

Parameters

type

The type of object to create.

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.

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.

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.

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

' Create an instance of the StringBuilder type using Activator.CreateInstance.
Dim o As Object = Activator.CreateInstance(GetType(StringBuilder))

' Append a string into the StringBuilder object and display the StringBuilder.
Dim sb As StringBuilder = CType(o, StringBuilder)
sb.Append("Hello, there.")
Console.WriteLine(sb)

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

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Show: