Expression.Field Méthode

Définition

Crée un MemberExpression qui représente l'accès à un champ.

Surcharges

Field(Expression, FieldInfo)

Crée un MemberExpression qui représente l'accès à un champ.

Field(Expression, String)

Crée un MemberExpression qui représente l'accès à un champ à partir du nom du champ.

Field(Expression, Type, String)

Crée un MemberExpression qui représente l'accès à un champ.

Field(Expression, FieldInfo)

Crée un MemberExpression qui représente l'accès à un champ.

public:
 static System::Linq::Expressions::MemberExpression ^ Field(System::Linq::Expressions::Expression ^ expression, System::Reflection::FieldInfo ^ field);
public static System.Linq.Expressions.MemberExpression Field (System.Linq.Expressions.Expression expression, System.Reflection.FieldInfo field);
public static System.Linq.Expressions.MemberExpression Field (System.Linq.Expressions.Expression? expression, System.Reflection.FieldInfo field);
static member Field : System.Linq.Expressions.Expression * System.Reflection.FieldInfo -> System.Linq.Expressions.MemberExpression
Public Shared Function Field (expression As Expression, field As FieldInfo) As MemberExpression

Paramètres

expression
Expression

Expression auquel la propriété Expression doit être égale. Pour static (Shared en Visual Basic), expression doit être null.

field
FieldInfo

FieldInfo auquel la propriété Member doit être égale.

Retours

MemberExpression

MemberExpression dont la propriété NodeType est égale à MemberAccess et dont les propriétés Expression et Member ont les valeurs spécifiées.

Exceptions

field a la valeur null.

  • ou - Le champ représenté par field n'est pas static (Shared en Visual Basic) et expression est null.

expression.Type ne peut pas être assigné au type de déclaration du champ représenté par field.

Remarques

La Type propriété du résultat MemberExpression est égale à la FieldType propriété de field.

S’applique à

Field(Expression, String)

Crée un MemberExpression qui représente l'accès à un champ à partir du nom du champ.

public:
 static System::Linq::Expressions::MemberExpression ^ Field(System::Linq::Expressions::Expression ^ expression, System::String ^ fieldName);
public static System.Linq.Expressions.MemberExpression Field (System.Linq.Expressions.Expression expression, string fieldName);
static member Field : System.Linq.Expressions.Expression * string -> System.Linq.Expressions.MemberExpression
Public Shared Function Field (expression As Expression, fieldName As String) As MemberExpression

Paramètres

expression
Expression

Expression dont Type contient un champ nommé fieldName. Cette valeur peut être null pour les champs statiques.

fieldName
String

Nom d'un champ auquel accéder.

Retours

MemberExpression

MemberExpression dont la propriété NodeType est égale à MemberAccess, dont la propriété Expression a pour valeur expression et dont la propriété Member a pour valeur le FieldInfo qui représente le champ désigné par fieldName.

Exceptions

expression ou fieldName est null.

Aucun champ nommé fieldName n'est défini dans expression.Type ou ses types de base.

Exemples

L’exemple de code suivant montre comment créer une expression qui représente l’accès à un champ.

// Add the following directive to your file:
// using System.Linq.Expressions;

class TestFieldClass
{
    int sample = 40;
}

static void TestField()
{
    TestFieldClass obj = new TestFieldClass();

    // This expression represents accessing a field.
    // For static fields, the first parameter must be null.
    Expression fieldExpr = Expression.Field(
        Expression.Constant(obj),
        "sample"
    );

    // The following statement first creates an expression tree,
    // then compiles it, and then runs it.
    Console.WriteLine(Expression.Lambda<Func<int>>(fieldExpr).Compile()());
}

// This code example produces the following output:
//
// 40
' Add the following directive to your file:
' Imports System.Linq.Expressions

Class TestFieldClass
    Dim sample As Integer = 40
End Class

Sub TestField()

    Dim obj As New TestFieldClass()

    ' This expression represents accessing a field.
    ' For static fields, the first parameter must be Nothing.
    Dim fieldExpr As Expression = Expression.Field(
          Expression.Constant(obj),
          "sample"
      )

    ' The following statement first creates an expression tree,
    ' then compiles it, and then runs it.
    Console.WriteLine(Expression.Lambda(Of Func(Of Integer))(fieldExpr).Compile()())
End Sub

' This code example produces the following output:
'
' 40

Remarques

La Type propriété du résultat MemberExpression est égale à la FieldType propriété du FieldInfo champ qui représente le champ indiqué par fieldName.

Cette méthode recherche expression. Tapez et ses types de base pour un champ qui a le nom fieldName. Les champs publics sont privilégiés par rapport aux champs non publics. Si un champ correspondant est trouvé, cette méthode passe expression et celle FieldInfo qui représente ce champ .Field

S’applique à

Field(Expression, Type, String)

Crée un MemberExpression qui représente l'accès à un champ.

public:
 static System::Linq::Expressions::MemberExpression ^ Field(System::Linq::Expressions::Expression ^ expression, Type ^ type, System::String ^ fieldName);
public static System.Linq.Expressions.MemberExpression Field (System.Linq.Expressions.Expression expression, Type type, string fieldName);
public static System.Linq.Expressions.MemberExpression Field (System.Linq.Expressions.Expression? expression, Type type, string fieldName);
static member Field : System.Linq.Expressions.Expression * Type * string -> System.Linq.Expressions.MemberExpression
Public Shared Function Field (expression As Expression, type As Type, fieldName As String) As MemberExpression

Paramètres

expression
Expression

Objet conteneur du champ. Cette valeur peut être null pour les champs statiques.

type
Type

Type qui contient le champ.

fieldName
String

Champ auquel accéder.

Retours

MemberExpression

Élément MemberExpression créé.

S’applique à