Expression.Field Method (Expression, String)
Creates a MemberExpression that represents accessing a field given the name of the field.
Assembly: System.Core (in System.Core.dll)
Public Shared Function Field ( expression As Expression, fieldName As String ) As MemberExpression
- Type: System.Linq.Expressions.Expression
The name of a field to be accessed.
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 code example shows how to create an expression that represents accessing a field.
' 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
Available since 8
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Available since 8.1