Skip to content
Tutorials Center
My learning

Topics

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.

C#
object? o = Activator.CreateInstance(typeof(StringBuilder));
T t = Activator.CreateInstance<T>();          // needs a public parameterless constructor

Try it

C# Creating objects from a Type

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.

C# What goes wrong

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

C#
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. MissingMethodException derives from MemberAccessException, so catching the base type covers both.
  • TargetInvocationException: the constructor itself threw; the real exception is in InnerException.
  • ArgumentException or NotSupportedException: open generic types and some other special types.

Usage notes

  • Prefer new or a factory when the type is known at compile time. Activator exists for types discovered at run time.
  • Overload resolution is dynamic. With several constructors, passing null or int versus long arguments can pick the wrong one or fail; pass the exact types or use ConstructorInfo.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 or ConstructorInfo.
  • Value types: CreateInstance(typeof(MyStruct)) works even without a constructor and gives default(MyStruct).
  • Generic overload: CreateInstance<T>() is what new T() compiles to in a generic method with the new() constraint.
  • Trimming and Native AOT: a type only reached through Activator may be trimmed away. See Native AOT and trimming.
  • Type names: Activator.CreateInstance(string assemblyName, string typeName) returns an ObjectHandle; call Unwrap(). Most code uses Type.GetType first and then the Type overload.