CKTIS / C#
System.Reflection.FieldInfo: riferimento pratico
System.Reflection.FieldInfo: riferimento pratico: FieldInfo descrive un campo e consente di leggerne o impostarne il valore tramite reflection.
Versione: .NET API; verified against .NET 10 reference surface
Sintassi
System.Reflection.FieldInfo /* select a documented member or constructor */FieldInfo descrive un campo e consente di leggerne o impostarne il valore tramite reflection.
Usalo negli strumenti di metadati quando il tipo del campo non è noto staticamente.
SetValue aggira alcuni controlli del compilatore ed è più lento dell’accesso diretto.
Per un campo statico GetValue e SetValue ignorano il parametro target/obj; passare null è convenzionale, non obbligatorio. Il trimming può rimuovere campi sconosciuti all’analisi.
Il programma completo usa realmente System.Reflection.FieldInfo; lo stdout esatto atteso è 8.
Esempi
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 field = typeof(Sample).GetField(nameof(Sample.Value))!;
Console.WriteLine(field.GetValue(new Sample()));
sealed class Sample { public int Value = 8; }
Risultato
8
Il programma completo usa realmente `System.Reflection.FieldInfo`; lo stdout esatto atteso è `8`.