Znalostná bázaCentrum skúšok
SKSlovenčina
Vybrať jazyk13

CKTIS / C#

System.Activator: praktická referencia

System.Activator: praktická referencia: Activator.CreateInstance vytvorí pomocou dostupného konštruktora inštanciu typu zvoleného za behu.

Verzia: .NET API; verified on .NET 10.0.14

Syntax

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

Activator.CreateInstance vytvorí pomocou dostupného konštruktora inštanciu typu zvoleného za behu.

Použite v riadených továrňach typov a reflexných integráciách, keď bežné new alebo DI nestačí.

Výnimka konštruktora je zabalená a reflexia môže kolidovať s trimming; neprijímajte nedôveryhodný názov typu.

Chýbajúci zodpovedajúci konštruktor spôsobí MissingMethodException; výsledok negenerického preťaženia môže byť null.

Úplný program skutočne používa System.Activator; presne očakávaný stdout je StringBuilder.

Príklady

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);

Výsledok

StringBuilder

Úplný program skutočne používa `System.Activator`; presne očakávaný stdout je `StringBuilder`.

Zdroje