Export (0) Print
Expand All

Expression.Field Method (Expression, String)

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

Namespace:  System.Linq.Expressions
Assembly:  System.Core (in System.Core.dll)

public static MemberExpression Field(
	Expression expression,
	string fieldName
)

Parameters

expression
Type: System.Linq.Expressions.Expression

An Expression whose Type contains a field named fieldName.

fieldName
Type: System.String

The name of a field.

Return Value

Type: System.Linq.Expressions.MemberExpression
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.

ExceptionCondition
ArgumentNullException

expression or fieldName is null.

ArgumentException

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

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.

The following example demonstrates how to use the Field(Expression, String) method to create a MemberExpression that represents getting the value of a field member.

class Animal
{
    public string species;
}

public static void CreateFieldExpression()
{
    Animal horse = new Animal();

    // Create a MemberExpression that represents getting 
    // the value of the 'species' field of class 'Animal'.
    System.Linq.Expressions.MemberExpression memberExpression =
        System.Linq.Expressions.Expression.Field(
            System.Linq.Expressions.Expression.Constant(horse),
            "species");

    Console.WriteLine(memberExpression.ToString());

    // This code produces the following output: 
    // 
    // value(CodeSnippets.FieldExample+Animal).species
}

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5

Community Additions

ADD
Show:
© 2014 Microsoft