Activator.CreateInstance Method (Type)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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

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

Public Shared Function CreateInstance (
	type As Type
) As Object


Type: System.Type

The type of object to create.

Return Value

Type: System.Object

A reference to the newly created object.

Exception Condition

type is null.


type is not a RuntimeType.


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


type cannot be a TypeBuilder.


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


The assembly that contains type is a dynamic assembly that was created with AssemblyBuilderAccess.Save.


The constructor being called throws an exception.


In the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, MemberAccessException, instead.

The caller does not have permission to call this constructor.


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


The COM type was not obtained through GetTypeFromProgID or GetTypeFromCLSID.


In the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, MissingMemberException, instead.

No matching public constructor was found.


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


type is not a valid type.

The constructor to be invoked must be accessible.


Starting with the .NET Framework 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 3.5 or later.

The following code example demonstrates how to call the CreateInstance(Type) method. Instances of several different types are created and their default values are displayed.

Imports System

Class DynamicInstanceList
    Private Shared instanceSpec As String = "System.EventArgs;System.Random;" + _

    Public Shared Sub Main()
        Dim instances() As String = instanceSpec.Split(";")
        Dim instlist As Array = Array.CreateInstance(GetType(Object), instances.Length)
        Dim item As Object

        For i As Integer = 0 To instances.Length -1
            ' create the object from the specification string
            Console.WriteLine("Creating instance of: {0}", instances(i))
            item = Activator.CreateInstance(Type.GetType(instances(i)))
            instlist.SetValue(item, i)
        Next i
        Console.WriteLine(vbNewLine + "Objects and their default values:" + vbNewLine)
        For Each o As Object In instlist
            Console.WriteLine("Type:     {0}" + vbNewLine + "Value:    {1}" + _
                vbNewLine + "HashCode: {2}" + vbNewLine, _
                o.GetType().FullName, o.ToString(), o.GetHashCode())
        Next o
    End Sub
End Class

' This program will display output similar to the following:
' Creating instance of: System.EventArgs
' Creating instance of: System.Random
' Creating instance of: System.Exception
' Creating instance of: System.Object
' Creating instance of: System.Version
' Objects and their default values:
' Type:     System.EventArgs
' Value:    System.EventArgs
' HashCode: 46104728
' Type:     System.Random
' Value:    System.Random
' HashCode: 12289376
' Type:     System.Exception
' Value:    System.Exception: Exception of type 'System.Exception' was thrown.
' HashCode: 55530882
' Type:     System.Object
' Value:    System.Object
' HashCode: 30015890
' Type:     System.Version
' Value:    0.0
' HashCode: 1048575


for the ability to call unmanaged code when creating an instance of a delegate. Associated enumeration: SecurityPermissionFlag.UnmanagedCode


for accessing a nonpublic type when the grant set of the nonpublic type is restricted to the caller's grant set or to a subset thereof. Associated enumeration: ReflectionPermissionFlag.RestrictedMemberAccess


for accessing nonpublic types regardless of their grant sets. Associated enumeration: ReflectionPermissionFlag.MemberAccess

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top