Share via


ExpressionBuilder 類別

定義

在頁面剖析期間評估運算式。

public ref class ExpressionBuilder abstract
public abstract class ExpressionBuilder
type ExpressionBuilder = class
Public MustInherit Class ExpressionBuilder
繼承
ExpressionBuilder
衍生

範例

下列程式碼範例示範如何藉由實作 ExpressionBuilder 抽象類別來建置自訂表格達式產生器。 這個 實作 ExpressionBuilder 會傳回傳遞至運算式的評估語句。 若要執行此範例,您必須先在 Web.config 檔案中註冊自訂表格達式產生器。 第一個程式碼範例示範如何在 Web.config 檔案中註冊自訂表格達式產生器。

<configuration>  
    <system.web>  
       <compilation>  
          <expressionBuilders>  
              <add expressionPrefix="MyCustomExpression"  
               type="MyCustomExpressionBuilder"/>  
          </expressionBuilders>  
       </compilation>  
    </system.web>  
</configuration>  

第二個程式碼範例示範如何參考 .aspx 檔案中的運算式。

<asp:Label ID="Label1" runat="server"   
Text="<%$ MyCustomExpression:Hello, world! %>" />  

第三個程式碼範例示範如何藉由衍生自 ExpressionBuilder 來開發自訂表格達式產生器。 若要執行此程式碼範例,您必須將 類別放在 App_Code 資料夾中。

using System;
using System.CodeDom;
using System.Web.UI;
using System.ComponentModel;
using System.Web.Compilation;
using System.Web.UI.Design;

// Apply ExpressionEditorAttributes to allow the 
// expression to appear in the designer.
[ExpressionPrefix("MyCustomExpression")]
[ExpressionEditor("MyCustomExpressionEditor")]
public class MyExpressionBuilder : ExpressionBuilder
{
    // Create a method that will return the result 
    // set for the expression argument.
    public static object GetEvalData(string expression, Type target, string entry)
    {
        return expression;
    }

    public override object EvaluateExpression(object target, BoundPropertyEntry entry, 
    object parsedData, ExpressionBuilderContext context)
    {
        return GetEvalData(entry.Expression, target.GetType(), entry.Name);
    }

    public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, 
    object parsedData, ExpressionBuilderContext context)
    {
        Type type1 = entry.DeclaringType;
        PropertyDescriptor descriptor1 = TypeDescriptor.GetProperties(type1)[entry.PropertyInfo.Name];
        CodeExpression[] expressionArray1 = new CodeExpression[3];
        expressionArray1[0] = new CodePrimitiveExpression(entry.Expression.Trim());
        expressionArray1[1] = new CodeTypeOfExpression(type1);
        expressionArray1[2] = new CodePrimitiveExpression(entry.Name);
        return new CodeCastExpression(descriptor1.PropertyType, new CodeMethodInvokeExpression(new 
       CodeTypeReferenceExpression(base.GetType()), "GetEvalData", expressionArray1));
    }

    public override bool SupportsEvaluate
    {
        get { return true; }
    }
}
Imports System.CodeDom
Imports System.Web.UI
Imports System.ComponentModel
Imports System.Web.Compilation
Imports System.Web.UI.Design

' Apply ExpressionEditorAttributes to allow the 
' expression to appear in the designer.
<ExpressionPrefix("MyCustomExpression")> _
<ExpressionEditor("MyCustomExpressionEditor")> _
Public Class MyExpressionBuilder
    Inherits ExpressionBuilder
    ' Create a method that will return the result 
    ' set for the expression argument.
    Public Shared Function GetEvalData(ByVal expression As String, _
       ByVal target As Type, ByVal entry As String) As Object
        Return expression
    End Function

    Public Overrides Function EvaluateExpression(ByVal target As Object, _
       ByVal entry As BoundPropertyEntry, ByVal parsedData As Object, _
       ByVal context As ExpressionBuilderContext) As Object
        Return GetEvalData(entry.Expression, target.GetType(), entry.Name)
    End Function

    Public Overrides Function GetCodeExpression(ByVal entry _
       As BoundPropertyEntry, ByVal parsedData As Object, ByVal context _
       As ExpressionBuilderContext) As CodeExpression
        Dim type1 As Type = entry.DeclaringType
        Dim descriptor1 As PropertyDescriptor = _
           TypeDescriptor.GetProperties(type1)(entry.PropertyInfo.Name)
        Dim expressionArray1(2) As CodeExpression
        expressionArray1(0) = New CodePrimitiveExpression(entry.Expression.Trim())
        expressionArray1(1) = New CodeTypeOfExpression(type1)
        expressionArray1(2) = New CodePrimitiveExpression(entry.Name)
        Return New CodeCastExpression(descriptor1.PropertyType, _
           New CodeMethodInvokeExpression(New CodeTypeReferenceExpression _
           (MyBase.GetType()), "GetEvalData", expressionArray1))
    End Function

    Public Overrides ReadOnly Property SupportsEvaluate() As Boolean
        Get
            Return True
        End Get
    End Property
End Class

備註

類別 ExpressionBuilder 是運算式產生器的基類,例如 AppSettingsExpressionBuilder 類別,會在頁面剖析期間建立程式碼運算式。

運算式產生器會剖析宣告式運算式,並建立程式碼來擷取系結至控制項屬性的值。 在無編譯案例中,支援無編譯功能的運算式產生器會在執行時間評估運算式。

當頁面剖析器遇到以字串 <%$ %> 分隔的運算式時,它會根據字串中的前置詞,為運算式建立運算式的運算式產生器。 前置詞是冒號左邊的字串部分, (:) 。 例如,當剖析器遇到字串 <%$ ConnectionStrings:MessageDB %> 時,它會建立 ConnectionStringsExpressionBuilder 物件。 前置詞會與 區段中Web.config檔案中的 ExpressionBuilders 運算式產生器相關聯。

宣告式運算式的右側會傳遞至運算式產生器以進行評估。 GetCodeExpression覆寫 方法,以產生將使用頁面編譯的程式碼。

如果您想要在未編譯的頁面上使用自訂表格達式產生器,您也必須覆寫 EvaluateExpression 方法,以傳回代表運算式結果的物件。 您也必須覆寫 SupportsEvaluate 屬性,以指出自訂表格達式產生器不支援未編譯的頁面。

您可以藉由實作運算式編輯器,定義一組屬性和方法,以在設計階段選取及評估與控制項屬性相關聯的運算式。 編輯器會透過類別層級中繼資料在運算式產生器上標示。 如需詳細資訊,請參閱ExpressionEditor

給實施者的注意事項

當您繼承自 類別時 ExpressionBuilder ,必須覆寫 GetCodeExpression(BoundPropertyEntry, Object, ExpressionBuilderContext) 方法。

建構函式

ExpressionBuilder()

初始化 ExpressionBuilder 類別的新執行個體。

屬性

SupportsEvaluate

在衍生類別中覆寫時傳回值,指出目前的 ExpressionBuilder 物件是否支援無編譯頁面。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
EvaluateExpression(Object, BoundPropertyEntry, Object, ExpressionBuilderContext)

在衍生類別中覆寫時,傳回表示評估的運算式之物件。

GetCodeExpression(BoundPropertyEntry, Object, ExpressionBuilderContext)

在衍生類別中覆寫時,傳回用來在頁面執行期間取得評估的運算式之程式碼。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ParseExpression(String, Type, ExpressionBuilderContext)

在衍生類別中覆寫時,傳回表示剖析的運算式之物件。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱