Breyta

Deila með


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.

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

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

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.

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

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.

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

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

Field(Expression, Type, String)

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

Creates a MemberExpression that represents accessing a field.

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

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