C# Reference ยท Reflection, Attributes and Metadata
Activator.CreateInstance() method
Reference for Activator.CreateInstance in C#: create objects from a Type, pass constructor arguments, use the generic overload, and handle errors and cost.
Activator.CreateInstance creates an object when you only have a Type (or a type name) at run time. The compiler cannot write new T(...) for a type it does not know, so you hand the Type to the activator and it finds a matching constructor, runs it and returns the new object as object?.
Plugin systems, dependency injection containers and deserializers use it all the time. For code that creates the same type many times, a cached delegate or a compiled expression is much faster; for a single known type, plain new is always better.
object? o = Activator.CreateInstance(typeof(StringBuilder)); T t = Activator.CreateInstance<T>(); // needs a public parameterless constructor
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public static object? CreateInstance(Type type); public static object? CreateInstance(Type type, params object?[]? args); public static object? CreateInstance(Type type, bool nonPublic); public static T CreateInstance<T>();
| Name | Description |
|---|---|
type |
The type to create. Must be concrete (not abstract, an interface or an open generic type). |
args |
Constructor arguments. The binder picks the constructor whose parameters fit the runtime types of the arguments. |
nonPublic |
true also allows a non-public parameterless constructor. |
Returns: the new instance, boxed as object?. It can be null only for Nullable<T> (for example Activator.CreateInstance(typeof(int?)) returns null).
Errors:
MissingMethodException: no constructor matches (no parameterless constructor, or the arguments do not fit), or the type is abstract.MissingMethodExceptionderives fromMemberAccessException, so catching the base type covers both.TargetInvocationException: the constructor itself threw; the real exception is inInnerException.ArgumentExceptionorNotSupportedException: open generic types and some other special types.
Usage notes
- Prefer
newor a factory when the type is known at compile time.Activatorexists for types discovered at run time. - Overload resolution is dynamic. With several constructors, passing
nullorintversuslongarguments can pick the wrong one or fail; pass the exact types or useConstructorInfo.Invoke. - Exceptions are wrapped. A throwing constructor surfaces as
TargetInvocationException, not as the original exception. - Performance: each call walks metadata and boxes arguments. The parameterless case is optimised, but a loop creating millions of objects should use a cached
Func<object>built with an expression tree orConstructorInfo. - Value types:
CreateInstance(typeof(MyStruct))works even without a constructor and givesdefault(MyStruct). - Generic overload:
CreateInstance<T>()is whatnew T()compiles to in a generic method with thenew()constraint. - Trimming and Native AOT: a type only reached through
Activatormay be trimmed away. See Native AOT and trimming. - Type names:
Activator.CreateInstance(string assemblyName, string typeName)returns anObjectHandle; callUnwrap(). Most code usesType.GetTypefirst and then theTypeoverload.