FieldInfo.GetValue(Object) Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
En cas de substitution dans une classe dérivée, retourne la valeur d’un champ pris en charge par un objet donné.
public:
abstract System::Object ^ GetValue(System::Object ^ obj);
public abstract object GetValue(object obj);
public abstract object? GetValue(object? obj);
abstract member GetValue : obj -> obj
Public MustOverride Function GetValue (obj As Object) As Object
Paramètres
- obj
- Object
Objet dont la valeur de champ est retournée.
Retours
Objet contenant la valeur du champ reflété par cette instance.
Implémente
Exceptions
Le champ n’est pas statique et obj est null.
Un champ est marqué littéral, mais le champ n’a pas l’un des types littéral acceptés.
L’appelant n’a pas l’autorisation d’accéder à ce champ.
La méthode n’est ni déclarée ni héritée par la classe de obj.
Exemples
L’exemple suivant utilise la GetValue méthode pour récupérer la valeur d’un champ statique. Notez que la valeur de l’argument obj est null.
using System;
using System.Reflection;
class Example
{
public static String val = "test";
public static void Main()
{
FieldInfo fld = typeof(Example).GetField("val");
Console.WriteLine(fld.GetValue(null));
val = "hi";
Console.WriteLine(fld.GetValue(null));
}
}
// The example displays the following output:
// test
// hi
Imports System.Reflection
Class Example
Public Shared val As String = "test"
Public Shared Sub Main()
Dim fld As FieldInfo = GetType(Example).GetField("val")
Console.WriteLine(fld.GetValue(Nothing))
val = "hi"
Console.WriteLine(fld.GetValue(Nothing))
End Sub
End Class
' The example displays the following output:
' test
' hi
L’exemple suivant récupère un tableau d’objets FieldInfo qui représente les champs du FieldsClass type, puis appelle la GetValue valeur de chaque champ pour l’objet fieldsInst .
using System;
using System.Reflection;
public class FieldsClass
{
public string fieldA;
public string fieldB;
public FieldsClass()
{
fieldA = "A public field";
fieldB = "Another public field";
}
}
public class Example
{
public static void Main()
{
FieldsClass fieldsInst = new FieldsClass();
// Get the type of FieldsClass.
Type fieldsType = typeof(FieldsClass);
// Get an array of FieldInfo objects.
FieldInfo[] fields = fieldsType.GetFields(BindingFlags.Public
| BindingFlags.Instance);
// Display the values of the fields.
Console.WriteLine("Displaying the values of the fields of {0}:",
fieldsType);
for(int i = 0; i < fields.Length; i++)
{
Console.WriteLine(" {0}:\t'{1}'",
fields[i].Name, fields[i].GetValue(fieldsInst));
}
}
}
// The example displays the following output:
// Displaying the values of the fields of FieldsClass:
// fieldA: 'A public field'
// fieldB: 'Another public field'
Imports System.Reflection
Public Class FieldsClass
Public fieldA As String
Public fieldB As String
Public Sub New()
fieldA = "A public field"
fieldB = "Another public field"
End Sub
End Class
Public Module Example
Public Sub Main()
Dim fieldsInst As New FieldsClass()
' Get the type of FieldsClass.
Dim fieldsType As Type = GetType(FieldsClass)
' Get an array of FieldInfo objects.
Dim fields As FieldInfo() = fieldsType.GetFields(BindingFlags.Public Or BindingFlags.Instance)
' Display the values of the fields.
Console.WriteLine("Displaying the values of the fields of {0}:", fieldsType)
For i As Integer = 0 To fields.Length - 1
Console.WriteLine(" {0}:{2}'{1}'",
fields(i).Name, fields(i).GetValue(fieldsInst), vbTab)
Next
End Sub
End Module
' The example displays the following output:
' Displaying the values of the fields of FieldsClass:
' fieldA: 'A public field'
' fieldB: 'Another public field'
Remarques
Si le champ est statique, obj il est ignoré. Pour les champs non statiques, obj doit être une instance d’une classe qui hérite ou déclare le champ. Notez que le type de retour est GetValueObject. Par exemple, si le champ contient une valeur primitive booléenne, une instance dont Object la valeur booléenne appropriée est retournée. Avant de retourner la valeur, GetValue vérifie si l’utilisateur dispose d’une autorisation d’accès.
Note
Les restrictions d’accès sont ignorées pour le code entièrement approuvé. Autrement dit, les constructeurs privés, les méthodes, les champs et les propriétés sont accessibles et appelés par réflexion chaque fois que le code est entièrement approuvé.
Note
Cette méthode peut être utilisée pour accéder aux membres non publics si l’appelant a reçu ReflectionPermission l’indicateur ReflectionPermissionFlag.RestrictedMemberAccess et si l’ensemble d’octroi des membres non publics est limité au jeu d’octrois de l’appelant ou à un sous-ensemble de celui-ci. (Consultez considérations relatives à la sécurité pour la réflexion.)
Pour utiliser cette fonctionnalité, votre application doit cibler .NET Framework 3.5 ou version ultérieure.