CKTIS / C#
System.Activator: referencia práctica
System.Activator: referencia práctica: Activator.CreateInstance crea mediante un constructor disponible una instancia de un tipo elegido en tiempo de ejecución.
Versión: .NET API; verified on .NET 10.0.14
Sintaxis
System.Activator /* select a documented member or constructor */Activator.CreateInstance crea mediante un constructor disponible una instancia de un tipo elegido en tiempo de ejecución.
Úsalo en fábricas de tipos controladas e integraciones mediante reflexión cuando new ordinario o DI no sean suficientes.
La excepción del constructor se encapsula y la reflexión puede entrar en conflicto con trimming; no aceptes un nombre de tipo no confiable.
Si no hay un constructor compatible se produce MissingMethodException; el resultado de la sobrecarga no genérica puede ser null.
El programa completo usa realmente System.Activator; su stdout esperado exacto es StringBuilder.
Ejemplos
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);
Resultado
StringBuilder
El programa completo usa realmente `System.Activator`; su stdout esperado exacto es `StringBuilder`.