Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

System.Activator: practical reference

System.Activator: practical reference: Activator.CreateInstance creates an instance of a type selected at run time through an available constructor.

Version: .NET API; verified on .NET 10.0.14

Syntax

System.Activator /* select a documented member or constructor */

Activator.CreateInstance creates an instance of a type selected at run time through an available constructor.

Use it in controlled type factories and reflection-based integrations when ordinary new or DI is insufficient.

A constructor exception is wrapped, and reflection can conflict with trimming; never accept an untrusted type name.

No matching constructor causes MissingMethodException; the result of the non-generic overload can be null.

The complete program actually uses System.Activator; its exact expected stdout is StringBuilder.

Examples

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

var value = Activator.CreateInstance<StringBuilder>();
Console.WriteLine(value.GetType().Name);

Result

StringBuilder

The complete program actually uses `System.Activator`; its exact expected stdout is `StringBuilder`.

Sources