Expression.Property 方法

定義

建立代表存取屬性的 MemberExpressionCreates 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)

建立用於存取屬性的 MemberExpressionCreates a MemberExpression accessing a property.

Property(Expression, PropertyInfo)

建立代表存取屬性的 MemberExpressionCreates 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)

建立代表存取屬性的 MemberExpressionCreates 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. 如果屬性為靜態/共用屬性,就必須為 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

建立的 IndexExpressionThe 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. 如果屬性為靜態/共用屬性,就必須為 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

建立的 IndexExpressionThe 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. 如果屬性為靜態/共用屬性,就必須為 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

建立的 IndexExpressionThe created IndexExpression.

適用於

Property(Expression, Type, String)

建立用於存取屬性的 MemberExpressionCreates 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.

type
Type

包含屬性的 TypeThe Type that contains the property.

propertyName
String

要存取的屬性。The property to be accessed.

傳回

MemberExpression

建立的 MemberExpressionThe created MemberExpression.

適用於

Property(Expression, PropertyInfo)

建立代表存取屬性的 MemberExpressionCreates 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 屬性設定為與之相等的 ExpressionAn Expression to set the Expression property equal to. 如果是靜態屬性,可以是 null。This can be null for static properties.

property
PropertyInfo

要將 PropertyInfo 屬性設定為與之相等的 MemberThe PropertyInfo to set the Member property equal to.

傳回

MemberExpression

MemberExpression,其 NodeType 屬性等於 MemberAccess,且 ExpressionMember 屬性設定為指定的值。A MemberExpression that has the NodeType property equal to MemberAccess and the Expression and Member properties set to the specified values.

例外狀況

propertynullproperty is null.

-或--or- property 所表示的屬性不是 static (在 Visual Basic 中為 Shared),且 expressionnullThe 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 屬性 MemberThe Type property of the resulting MemberExpression is equal to the PropertyType property of Member.

如果所表示的屬性 propertystatic Shared 在 Visual Basic) 中 (,則 expression 可以是 nullIf 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 屬性設定為與之相等的 ExpressionAn 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

MemberExpression,其 NodeType 屬性等於 MemberAccessExpression 屬性設定為 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.

例外狀況

propertyAccessornullpropertyAccessor is null.

-或--or- propertyAccessor 所表示的方法不是 static (在 Visual Basic 中為 Shared),且 expressionnullThe 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 屬性 MemberThe Type property of the resulting MemberExpression is equal to the PropertyType property of Member.

如果所表示的方法 propertyAccessorstatic Shared 在 Visual Basic) 中 (,則 expression 可以是 nullIf the method represented by propertyAccessor is static (Shared in Visual Basic), expression can be null.

適用於

Property(Expression, String)

建立代表存取屬性的 MemberExpressionCreates 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. 如果是靜態屬性,可以是 nullThis can be null for static properties.

propertyName
String

要存取的屬性名稱。The name of a property to be accessed.

傳回

MemberExpression

MemberExpression,其 NodeType 屬性等於 MemberAccessExpression 屬性設定為 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.

例外狀況

expressionpropertyNamenullexpression 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 的屬性 propertyNameThe Type property of the resulting MemberExpression is equal to the PropertyType property of the PropertyInfo that represents the property denoted by propertyName.

這個方法會搜尋 expression 。類型及其基底類型,適用于具有名稱的屬性 propertyNameThis 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 代表該屬性的傳遞給 PropertyIf a matching property is found, this method passes expression and the PropertyInfo that represents that property to Property.

適用於