Expression.Property 方法
定义
创建一个表示访问属性的 MemberExpression。Creates a MemberExpression that represents accessing a property.
重载
| Property(Expression, String, Expression[]) |
创建一个 IndexExpression,它表示对索引属性的访问。Creates an IndexExpression representing the access to an indexed property. |
| Property(Expression, PropertyInfo, Expression[]) |
创建一个 IndexExpression,它表示对索引属性的访问。Creates an IndexExpression representing the access to an indexed property. |
| Property(Expression, PropertyInfo, IEnumerable<Expression>) |
创建一个 IndexExpression,它表示对索引属性的访问。Creates an IndexExpression representing the access to an indexed property. |
| Property(Expression, Type, String) |
创建一个访问属性的 MemberExpression。Creates a MemberExpression accessing a property. |
| Property(Expression, PropertyInfo) |
创建一个表示访问属性的 MemberExpression。Creates a MemberExpression that represents accessing a property. |
| Property(Expression, MethodInfo) |
使用属性访问器方法创建一个表示访问属性的 MemberExpression。Creates a MemberExpression that represents accessing a property by using a property accessor method. |
| Property(Expression, String) |
创建一个表示访问属性的 MemberExpression。Creates a MemberExpression that represents accessing a property. |
Property(Expression, String, Expression[])
创建一个 IndexExpression,它表示对索引属性的访问。Creates an IndexExpression representing the access to an indexed property.
public:
static System::Linq::Expressions::IndexExpression ^ Property(System::Linq::Expressions::Expression ^ instance, System::String ^ propertyName, ... cli::array <System::Linq::Expressions::Expression ^> ^ arguments);
public static System.Linq.Expressions.IndexExpression Property (System.Linq.Expressions.Expression instance, string propertyName, params System.Linq.Expressions.Expression[] arguments);
public static System.Linq.Expressions.IndexExpression Property (System.Linq.Expressions.Expression instance, string propertyName, params System.Linq.Expressions.Expression[]? arguments);
static member Property : System.Linq.Expressions.Expression * string * System.Linq.Expressions.Expression[] -> System.Linq.Expressions.IndexExpression
Public Shared Function Property (instance As Expression, propertyName As String, ParamArray arguments As Expression()) As IndexExpression
参数
- instance
- Expression
属性所属的对象。The object to which the property belongs. 如果该属性为 static/shared,则此对象必须为 null。If the property is static/shared, it must be null.
- propertyName
- String
索引器的名称。The name of the indexer.
- arguments
- Expression[]
用于为属性编制索引的 Expression 对象的数组。An array of Expression objects that are used to index the property.
返回
创建的 IndexExpression。The created IndexExpression.
适用于
Property(Expression, PropertyInfo, Expression[])
创建一个 IndexExpression,它表示对索引属性的访问。Creates an IndexExpression representing the access to an indexed property.
public:
static System::Linq::Expressions::IndexExpression ^ Property(System::Linq::Expressions::Expression ^ instance, System::Reflection::PropertyInfo ^ indexer, ... cli::array <System::Linq::Expressions::Expression ^> ^ arguments);
public static System.Linq.Expressions.IndexExpression Property (System.Linq.Expressions.Expression instance, System.Reflection.PropertyInfo indexer, params System.Linq.Expressions.Expression[] arguments);
public static System.Linq.Expressions.IndexExpression Property (System.Linq.Expressions.Expression? instance, System.Reflection.PropertyInfo indexer, params System.Linq.Expressions.Expression[]? arguments);
static member Property : System.Linq.Expressions.Expression * System.Reflection.PropertyInfo * System.Linq.Expressions.Expression[] -> System.Linq.Expressions.IndexExpression
Public Shared Function Property (instance As Expression, indexer As PropertyInfo, ParamArray arguments As Expression()) As IndexExpression
参数
- instance
- Expression
属性所属的对象。The object to which the property belongs. 如果该属性为 static/shared,则此对象必须为 null。If the property is static/shared, it must be null.
- indexer
- PropertyInfo
表示要编制索引的属性的 PropertyInfo。The PropertyInfo that represents the property to index.
- arguments
- Expression[]
用于为属性编制索引的 Expression 对象的数组。An array of Expression objects that are used to index the property.
返回
创建的 IndexExpression。The created IndexExpression.
适用于
Property(Expression, PropertyInfo, IEnumerable<Expression>)
创建一个 IndexExpression,它表示对索引属性的访问。Creates an IndexExpression representing the access to an indexed property.
public:
static System::Linq::Expressions::IndexExpression ^ Property(System::Linq::Expressions::Expression ^ instance, System::Reflection::PropertyInfo ^ indexer, System::Collections::Generic::IEnumerable<System::Linq::Expressions::Expression ^> ^ arguments);
public static System.Linq.Expressions.IndexExpression Property (System.Linq.Expressions.Expression instance, System.Reflection.PropertyInfo indexer, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> arguments);
public static System.Linq.Expressions.IndexExpression Property (System.Linq.Expressions.Expression? instance, System.Reflection.PropertyInfo indexer, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression>? arguments);
static member Property : System.Linq.Expressions.Expression * System.Reflection.PropertyInfo * seq<System.Linq.Expressions.Expression> -> System.Linq.Expressions.IndexExpression
Public Shared Function Property (instance As Expression, indexer As PropertyInfo, arguments As IEnumerable(Of Expression)) As IndexExpression
参数
- instance
- Expression
属性所属的对象。The object to which the property belongs. 如果该属性为 static/shared,则此对象必须为 null。If the property is static/shared, it must be null.
- indexer
- PropertyInfo
表示要编制索引的属性的 PropertyInfo。The PropertyInfo that represents the property to index.
- arguments
- IEnumerable<Expression>
用于为属性编制索引的 IEnumerable<T> 对象的 Expression。An IEnumerable<T> of Expression objects that are used to index the property.
返回
创建的 IndexExpression。The created IndexExpression.
适用于
Property(Expression, Type, String)
创建一个访问属性的 MemberExpression。Creates a MemberExpression accessing a property.
public:
static System::Linq::Expressions::MemberExpression ^ Property(System::Linq::Expressions::Expression ^ expression, Type ^ type, System::String ^ propertyName);
public static System.Linq.Expressions.MemberExpression Property (System.Linq.Expressions.Expression expression, Type type, string propertyName);
public static System.Linq.Expressions.MemberExpression Property (System.Linq.Expressions.Expression? expression, Type type, string propertyName);
static member Property : System.Linq.Expressions.Expression * Type * string -> System.Linq.Expressions.MemberExpression
Public Shared Function Property (expression As Expression, type As Type, propertyName As String) As MemberExpression
参数
- expression
- Expression
属性的包含对象。The containing object of the property. 对于静态属性,这可以为 null。This can be null for static properties.
- propertyName
- String
要访问的属性。The property to be accessed.
返回
创建的 MemberExpression。The created MemberExpression.
适用于
Property(Expression, PropertyInfo)
创建一个表示访问属性的 MemberExpression。Creates a MemberExpression that represents accessing a property.
public:
static System::Linq::Expressions::MemberExpression ^ Property(System::Linq::Expressions::Expression ^ expression, System::Reflection::PropertyInfo ^ property);
public static System.Linq.Expressions.MemberExpression Property (System.Linq.Expressions.Expression expression, System.Reflection.PropertyInfo property);
public static System.Linq.Expressions.MemberExpression Property (System.Linq.Expressions.Expression? expression, System.Reflection.PropertyInfo property);
static member Property : System.Linq.Expressions.Expression * System.Reflection.PropertyInfo -> System.Linq.Expressions.MemberExpression
Public Shared Function Property (expression As Expression, property As PropertyInfo) As MemberExpression
参数
- expression
- Expression
要将 Expression 属性设置为与其相等的 Expression。An Expression to set the Expression property equal to. 对于静态属性,这可以为 null。This can be null for static properties.
- property
- PropertyInfo
要将 PropertyInfo 属性设置为与其相等的 Member。The PropertyInfo to set the Member property equal to.
返回
一个 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.
例外
property 为 null。property is null.
- 或 --or-
property 所表示的属性不为 static(在 Visual Basic 中为 Shared),并且 expression 为 null。The property that property represents is not static (Shared in Visual Basic) and expression is null.
expression.Type 不能赋给 property 所表示的属性的声明类型。expression.Type is not assignable to the declaring type of the property that property represents.
注解
Type生成的的属性 MemberExpression 等于的 PropertyType 属性 Member 。The Type property of the resulting MemberExpression is equal to the PropertyType property of Member.
如果由表示的属性 property static (Shared Visual Basic) 中,则 expression 可以为 null 。If the property represented by property is static (Shared in Visual Basic), expression can be null.
适用于
Property(Expression, MethodInfo)
使用属性访问器方法创建一个表示访问属性的 MemberExpression。Creates a MemberExpression that represents accessing a property by using a property accessor method.
public:
static System::Linq::Expressions::MemberExpression ^ Property(System::Linq::Expressions::Expression ^ expression, System::Reflection::MethodInfo ^ propertyAccessor);
public static System.Linq.Expressions.MemberExpression Property (System.Linq.Expressions.Expression expression, System.Reflection.MethodInfo propertyAccessor);
public static System.Linq.Expressions.MemberExpression Property (System.Linq.Expressions.Expression? expression, System.Reflection.MethodInfo propertyAccessor);
static member Property : System.Linq.Expressions.Expression * System.Reflection.MethodInfo -> System.Linq.Expressions.MemberExpression
Public Shared Function Property (expression As Expression, propertyAccessor As MethodInfo) As MemberExpression
参数
- expression
- Expression
要将 Expression 属性设置为与其相等的 Expression。An Expression to set the Expression property equal to. 对于静态属性,这可以为 null。This can be null for static properties.
- propertyAccessor
- MethodInfo
表示属性访问器方法的 MethodInfo。The MethodInfo that represents a property accessor method.
返回
一个 MemberExpression,其 NodeType 属性等于 MemberAccess,Expression 属性设置为 expression,并且 Member 属性设置为表示在 PropertyInfo 中访问的属性的 propertyAccessor。A MemberExpression that has the NodeType property equal to MemberAccess, the Expression property set to expression and the Member property set to the PropertyInfo that represents the property accessed in propertyAccessor.
例外
propertyAccessor 为 null。propertyAccessor is null.
- 或 --or-
propertyAccessor 所表示的方法不为 static(在 Visual Basic 中为 Shared),并且 expression 为 null。The method that propertyAccessor represents is not static (Shared in Visual Basic) and expression is null.
expression.Type 不可分配给由 propertyAccessor 表示的方法声明类型。expression.Type is not assignable to the declaring type of the method represented by propertyAccessor.
- 或 --or-
propertyAccessor 所表示的方法不为属性访问器方法。The method that propertyAccessor represents is not a property accessor method.
注解
Type生成的的属性 MemberExpression 等于的 PropertyType 属性 Member 。The Type property of the resulting MemberExpression is equal to the PropertyType property of Member.
如果由表示的方法 propertyAccessor static (Shared Visual Basic) 中,则 expression 可以为 null 。If the method represented by propertyAccessor is static (Shared in Visual Basic), expression can be null.
适用于
Property(Expression, String)
创建一个表示访问属性的 MemberExpression。Creates a MemberExpression that represents accessing a property.
public:
static System::Linq::Expressions::MemberExpression ^ Property(System::Linq::Expressions::Expression ^ expression, System::String ^ propertyName);
public static System.Linq.Expressions.MemberExpression Property (System.Linq.Expressions.Expression expression, string propertyName);
static member Property : System.Linq.Expressions.Expression * string -> System.Linq.Expressions.MemberExpression
Public Shared Function Property (expression As Expression, propertyName As String) As MemberExpression
参数
- expression
- Expression
一个 Expression,其 Type 包含一个名为 propertyName 的属性。An Expression whose Type contains a property named propertyName. 对于静态属性,这可以为 null。This can be null for static properties.
- propertyName
- String
要访问的属性的名称。The name of a property to be accessed.
返回
一个 MemberExpression,其 NodeType 属性等于 MemberAccess,Expression 属性设置为 expression,并且 Member 属性设置为由 PropertyInfo 表示的属性的 propertyName。A MemberExpression that has the NodeType property equal to MemberAccess, the Expression property set to expression, and the Member property set to the PropertyInfo that represents the property denoted by propertyName.
例外
expression 或 propertyName 为 null。expression or propertyName is null.
没有在 propertyName.Type 或其基类型中定义名为 expression 的属性。No property named propertyName is defined in expression.Type or its base types.
示例
下面的示例演示如何创建一个表示访问属性的表达式。The following example shows how to create an expression that represents accessing a property.
// Add the following directive to your file:
// using System.Linq.Expressions;
class TestPropertyClass
{
public int sample {get; set;}
}
static void TestProperty()
{
TestPropertyClass obj = new TestPropertyClass();
obj.sample = 40;
// This expression represents accessing a property.
// For static fields, the first parameter must be null.
Expression propertyExpr = Expression.Property(
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>>(propertyExpr).Compile()());
}
// This code example produces the following output:
//
// 40
' Add the following directive to your file:
' Imports System.Linq.Expressions
Class TestPropertyClass
Public Property Sample As Integer
End Class
Sub TestProperty()
Dim obj As New TestPropertyClass()
obj.Sample = 40
' This expression represents accessing a property.
' For static properties, the first parameter must be Nothing.
Dim propertyExpr As Expression = Expression.Property(
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))(propertyExpr).Compile()())
End Sub
' This code example produces the following output:
'
' 40
注解
生成的的 Type 属性 MemberExpression 等于表示由表示的 PropertyType 属性的属性 PropertyInfo propertyName 。The Type property of the resulting MemberExpression is equal to the PropertyType property of the PropertyInfo that represents the property denoted by propertyName.
此方法搜索 expression 。类型及其基类型作为名称的属性 propertyName 。This method searches expression.Type and its base types for a property that has the name propertyName. 公共属性优先于非公共属性。Public properties are given preference over non-public properties. 如果找到匹配的属性,此方法会 expression 将和 PropertyInfo 表示该属性的传递到 Property 。If a matching property is found, this method passes expression and the PropertyInfo that represents that property to Property.