Expression.Field Method

Definition

Creates a MemberExpression that represents accessing a field.

Overloads

Field(Expression, FieldInfo)

Creates a MemberExpression that represents accessing a field.

Field(Expression, String)

Creates a MemberExpression that represents accessing a field given the name of the field.

Field(Expression, Type, String)

Creates a MemberExpression that represents accessing a field.

Field(Expression, FieldInfo)

Source:
MemberExpression.cs
Source:
MemberExpression.cs
Source:
MemberExpression.cs

Creates a MemberExpression that represents accessing a field.

C#
public static System.Linq.Expressions.MemberExpression Field(System.Linq.Expressions.Expression expression, System.Reflection.FieldInfo field);
C#
public static System.Linq.Expressions.MemberExpression Field(System.Linq.Expressions.Expression? expression, System.Reflection.FieldInfo field);

Parameters

expression
Expression

An Expression to set the Expression property equal to. For static (Shared in Visual Basic), expression must be null.

field
FieldInfo

The FieldInfo to set the Member property equal to.

Returns

A MemberExpression that has the NodeType property equal to MemberAccess and the Expression and Member properties set to the specified values.

Exceptions

field is null.

-or-

The field represented by field is not static (Shared in Visual Basic) and expression is null.

expression.Type is not assignable to the declaring type of the field represented by field.

Remarks

The Type property of the resulting MemberExpression is equal to the FieldType property of field.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Field(Expression, String)

Source:
MemberExpression.cs
Source:
MemberExpression.cs
Source:
MemberExpression.cs

Creates a MemberExpression that represents accessing a field given the name of the field.

C#
public static System.Linq.Expressions.MemberExpression Field(System.Linq.Expressions.Expression expression, string fieldName);

Parameters

expression
Expression

An Expression whose Type contains a field named fieldName. This can be null for static fields.

fieldName
String

The name of a field to be accessed.

Returns

A MemberExpression that has the NodeType property equal to MemberAccess, the Expression property set to expression, and the Member property set to the FieldInfo that represents the field denoted by fieldName.

Exceptions

expression or fieldName is null.

No field named fieldName is defined in expression.Type or its base types.

Examples

The following code example shows how to create an expression that represents accessing a field.

C#
// 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

Remarks

The Type property of the resulting MemberExpression is equal to the FieldType property of the FieldInfo that represents the field denoted by fieldName.

This method searches expression.Type and its base types for a field that has the name fieldName. Public fields are given preference over non-public fields. If a matching field is found, this method passes expression and the FieldInfo that represents that field to Field.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Field(Expression, Type, String)

Source:
MemberExpression.cs
Source:
MemberExpression.cs
Source:
MemberExpression.cs

Creates a MemberExpression that represents accessing a field.

C#
public static System.Linq.Expressions.MemberExpression Field(System.Linq.Expressions.Expression expression, Type type, string fieldName);
C#
public static System.Linq.Expressions.MemberExpression Field(System.Linq.Expressions.Expression? expression, Type type, string fieldName);

Parameters

expression
Expression

The containing object of the field. This can be null for static fields.

type
Type

The Type that contains the field.

fieldName
String

The field to be accessed.

Returns

The created MemberExpression.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0