Expression.Field 方法
定义
创建一个表示访问字段的 MemberExpression。Creates a MemberExpression that represents accessing a field.
重载
| Field(Expression, FieldInfo) |
创建一个表示访问字段的 MemberExpression。Creates a MemberExpression that represents accessing a field. |
| Field(Expression, String) |
在给定字段名称的情况下,创建一个表示访问此字段的 MemberExpression。Creates a MemberExpression that represents accessing a field given the name of the field. |
| Field(Expression, Type, String) |
创建一个表示访问字段的 MemberExpression。Creates a MemberExpression that represents accessing a field. |
Field(Expression, FieldInfo)
创建一个表示访问字段的 MemberExpression。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
参数
- expression
- Expression
要将 Expression 属性设置为与其相等的 Expression。An Expression to set the Expression property equal to. 对于 static(在 Visual Basic 中为 Shared),expression 必须是 null。For static (Shared in Visual Basic), expression must be null.
返回
一个 MemberExpression,其 NodeType 属性等于 MemberAccess,并且其 Expression 和 Member 属性设置为指定值。A MemberExpression that has the NodeType property equal to MemberAccess and the Expression and Member properties set to the specified values.
例外
field 为 null。field is null.
- 或 --or-
field 表示的字段不为 static(在 Visual Basic 中不为 Shared),且 expression 为 null。The field represented by field is not static (Shared in Visual Basic) and expression is null.
expression.Type 不能赋给 field 所表示的字段的声明类型。expression.Type is not assignable to the declaring type of the field represented by field.
注解
Type生成的的属性 MemberExpression 等于的 FieldType 属性 field 。The Type property of the resulting MemberExpression is equal to the FieldType property of field.
适用于
Field(Expression, String)
在给定字段名称的情况下,创建一个表示访问此字段的 MemberExpression。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
参数
- expression
- Expression
一个 Expression,其 Type 中包含一个名为 fieldName 的字段。An Expression whose Type contains a field named fieldName. 对于静态字段,这可以为 null。This can be null for static fields.
- fieldName
- String
要访问的字段的名称。The name of a field to be accessed.
返回
MemberExpression 的 NodeType 属性等于 MemberAccess,Expression 属性设置为 expression,Member 属性设置为 FieldInfo(表示由 fieldName 表示的字段)。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.
例外
expression 或 fieldName 为 null。expression or fieldName is null.
没有在 fieldName.Type 或其基类型中定义名为 expression 的字段。No field named fieldName is defined in expression.Type or its base types.
示例
下面的代码示例演示如何创建一个表示访问字段的表达式。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
注解
生成的的 Type 属性 MemberExpression 等于 FieldType FieldInfo 表示由表示的字段的的属性 fieldName 。The Type property of the resulting MemberExpression is equal to the FieldType property of the FieldInfo that represents the field denoted by fieldName.
此方法搜索 expression 。类型及其基类型作为名称的字段 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. 如果找到了匹配字段,此方法会将 expression 和表示该字段的传递 FieldInfo 到 Field 。If a matching field is found, this method passes expression and the FieldInfo that represents that field to Field.
适用于
Field(Expression, Type, String)
创建一个表示访问字段的 MemberExpression。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
参数
- expression
- Expression
字段的包含对象。The containing object of the field. 对于静态字段,这可以为 null。This can be null for static fields.
- fieldName
- String
要访问的字段。The field to be accessed.
返回
创建的 MemberExpression。The created MemberExpression.