Activator::CreateInstance Method (Type)
Creates an instance of the specified type using that type's default constructor.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- type
- Type: System::Type
The type of object to create.
| Exception | Condition |
|---|---|
| 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. -or- The assembly that contains type is a dynamic assembly that was created with AssemblyBuilderAccess::Save. |
| 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.
Note |
|---|
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 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.
using namespace System; ref class DynamicInstanceList { private: static String^ instanceSpec = "System.EventArgs;System.Random;" + "System.Exception;System.Object;System.Version"; public: static void Main() { array<String^>^ instances = instanceSpec->Split(';'); Array^ instlist = Array::CreateInstance(Object::typeid, instances->Length); Object^ item; for (int i = 0; i < instances->Length; i++) { // 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); } Console::WriteLine("\nObjects and their default values:\n"); for each (Object^ o in instlist) { Console::WriteLine("Type: {0}\nValue: {1}\nHashCode: {2}\n", o->GetType()->FullName, o->ToString(), o->GetHashCode()); } } }; int main() { DynamicInstanceList::Main(); } // 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
- SecurityPermission
for the ability to call unmanaged code when creating an instance of a delegate. Associated enumeration: SecurityPermissionFlag::UnmanagedCode
- ReflectionPermission
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
- ReflectionPermission
for accessing nonpublic types regardless of their grant sets. Associated enumeration: ReflectionPermissionFlag::MemberAccess
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note