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
)

Parameters

type
Type: System::Type

The type of object to create.

Return Value

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

ExceptionCondition
ArgumentNullException

type is nullptr.

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 .NET Framework version 2.0 Service Pack 1, this method can be used to access nonpublic types if the caller has been granted ReflectionPermission with the ReflectionPermissionFlag::RestrictedMemberAccess 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 .NET Framework version 3.5. For more information, see .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.
Object^ o = Activator::CreateInstance(StringBuilder::typeid);

// Append a string into the StringBuilder object and display the  
// StringBuilder.
StringBuilder^ sb = (StringBuilder^) o;
sb->Append("Hello, there.");
Console::WriteLine(sb);

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft