Expression.Property 方法

定義

建立代表存取屬性的 MemberExpression

多載

Property(Expression, String, Expression[])

建立 IndexExpression,表示索引之屬性的存取結果。

Property(Expression, PropertyInfo, Expression[])

建立 IndexExpression,表示索引之屬性的存取結果。

Property(Expression, PropertyInfo, IEnumerable<Expression>)

建立 IndexExpression,表示索引之屬性的存取結果。

Property(Expression, Type, String)

建立用於存取屬性的 MemberExpression

Property(Expression, PropertyInfo)

建立代表存取屬性的 MemberExpression

Property(Expression, MethodInfo)

建立 MemberExpression,代表透過使用屬性存取子來存取屬性。

Property(Expression, String)

建立代表存取屬性的 MemberExpression

Property(Expression, String, Expression[])

建立 IndexExpression,表示索引之屬性的存取結果。

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

屬性所屬的物件。 如果屬性為靜態/共用屬性,就必須為 null。

propertyName
String

索引子的名稱。

arguments
Expression[]

Expression 物件的陣列,用於對屬性進行索引。

傳回

IndexExpression

建立的 IndexExpression

適用於

Property(Expression, PropertyInfo, Expression[])

建立 IndexExpression,表示索引之屬性的存取結果。

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

屬性所屬的物件。 如果屬性為靜態/共用屬性,就必須為 null。

indexer
PropertyInfo

PropertyInfo,代表要編製索引的屬性。

arguments
Expression[]

Expression 物件的陣列,用於對屬性進行索引。

傳回

IndexExpression

建立的 IndexExpression

適用於

Property(Expression, PropertyInfo, IEnumerable<Expression>)

建立 IndexExpression,表示索引之屬性的存取結果。

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

屬性所屬的物件。 如果屬性為靜態/共用屬性,就必須為 null。

indexer
PropertyInfo

PropertyInfo,代表要編製索引的屬性。

arguments
IEnumerable<Expression>

IEnumerable<T> 物件的 Expression,用於對屬性進行索引。

傳回

IndexExpression

建立的 IndexExpression

適用於

Property(Expression, Type, String)

建立用於存取屬性的 MemberExpression

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

屬性的包含物件。 如果是靜態屬性,可以是 null。

type
Type

包含屬性的 Type

propertyName
String

要存取的屬性。

傳回

MemberExpression

建立的 MemberExpression

適用於

Property(Expression, PropertyInfo)

建立代表存取屬性的 MemberExpression

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。 如果是靜態屬性,可以是 null。

property
PropertyInfo

要將 PropertyInfo 屬性設定為與之相等的 Member

傳回

MemberExpression

MemberExpression,其 NodeType 屬性等於 MemberAccess,且 ExpressionMember 屬性設定為指定的值。

例外狀況

propertynull

-或- property 所表示的屬性不是 static (在 Visual Basic 中為 Shared),且 expressionnull

expression.Type 無法指派給 property 所表示屬性的宣告類型。

備註

Type產生的 MemberExpression 屬性等於 PropertyTypeMember 屬性。

如果 所 property static 表示的屬性是在 Visual Basic) 中 (Sharedexpression 可以是 null

適用於

Property(Expression, MethodInfo)

建立 MemberExpression,代表透過使用屬性存取子來存取屬性。

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。 如果是靜態屬性,可以是 null。

propertyAccessor
MethodInfo

MethodInfo,代表屬性存取子方法。

傳回

MemberExpression

MemberExpression,其 NodeType 屬性等於 MemberAccessExpression 屬性設定為 expression,且 Member 屬性設定為 PropertyInfo,代表在 propertyAccessor 中存取的屬性。

例外狀況

propertyAccessornull

-或- propertyAccessor 所表示的方法不是 static (在 Visual Basic 中為 Shared),且 expressionnull

expression.Type 無法指派給 propertyAccessor 所代表之方法的宣告類型。

-或- propertyAccessor 所表示的方法不是屬性存取子方法。

備註

Type產生的 MemberExpression 屬性等於 PropertyTypeMember 屬性。

如果 所 propertyAccessor static 表示的方法在 Visual Basic) Shared 中 (, expression 可以是 null

適用於

Property(Expression, String)

建立代表存取屬性的 MemberExpression

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 的屬性。 如果是靜態屬性,可以是 null

propertyName
String

要存取的屬性名稱。

傳回

MemberExpression

MemberExpression,其 NodeType 屬性等於 MemberAccessExpression 屬性設定為 expression,且 Member 屬性設定為 PropertyInfo,代表 propertyName 所表示的屬性。

例外狀況

expressionpropertyNamenull

propertyName.Type 或其基底類型中沒有定義名為 expression 的屬性。

範例

下列範例示範如何建立表示存取屬性的運算式。

// 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 表示的屬性。

這個方法會搜尋 expression 。類型及其屬性的基底類型,其名稱 propertyName 為 。 公用屬性的喜好設定高於非公用屬性。 如果找到相符的屬性,這個方法會傳遞 ,並將 PropertyInfo 表示該屬性的 傳遞給 。 expression Property

適用於