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 (Type)

Creates an instance of the specified type by 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 null.

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.

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.

The type and the constructor to be invoked must be accessible. 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 CreateInstance method throws a MemberAccessException exception instead of a MissingMethodException exception.

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.


Type sbType = typeof(StringBuilder);

// Create an instance of the StringBuilder type using Activator.CreateInstance
// and the parameterless constructor.
object o = Activator.CreateInstance(sbType);

// 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[] {"Hello, there."});
outputBlock.Text += o.ToString() + "\n";


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

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

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft