ExpressionBuilder 类


在分析页的过程中计算表达式。Evaluates expressions during page parsing.

public ref class ExpressionBuilder abstract
public abstract class ExpressionBuilder
type ExpressionBuilder = class
Public MustInherit Class ExpressionBuilder


下面的代码示例演示如何通过实现ExpressionBuilder抽象类来生成自定义表达式生成器。The following code examples demonstrate how to build a custom expression builder by implementing the ExpressionBuilder abstract class. ExpressionBuilder此实现返回传递给表达式的计算语句。This implementation of ExpressionBuilder returns an evaluated statement that is passed to the expression. 若要运行此示例, 必须先在 web.config 文件中注册自定义表达式生成器。To run this example, you must first register the custom expression builder in the Web.config file. 第一个代码示例演示如何在 web.config 文件中注册自定义表达式生成器。The first code example demonstrates how to register the custom expression builder in the Web.config file.

              <add expressionPrefix="MyCustomExpression"  

第二个代码示例演示如何引用 .aspx 文件中的表达式。The second code example demonstrates how to reference the expression in an .aspx file.

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

第三个代码示例演示如何通过从ExpressionBuilder派生来开发自定义表达式生成器。The third code example demonstrates how to develop a customized expression builder by deriving from ExpressionBuilder. 若要运行此代码示例, 必须将类放置在 App_Code 文件夹中。To run this code example, you must place the class in the App_Code folder.

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.
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 = _
        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
            Return True
        End Get
    End Property
End Class


类是表达式生成器 (如AppSettingsExpressionBuilder类) 的基类, 用于在页的分析过程中创建代码表达式。 ExpressionBuilderThe ExpressionBuilder class is the base class for expression builders, such as the AppSettingsExpressionBuilder class, that create code expressions during page parsing.

表达式生成器分析声明性表达式并创建代码以检索绑定到控件属性的值。Expression builders parse declarative expressions and create code to retrieve values bound to a control property. 在无编译情况下, 支持无编译功能的表达式生成器将在运行时计算表达式。In no-compile scenarios, an expression builder that supports a no-compile feature evaluates the expression during run time.

当页分析器遇到用字符串<%$ %>分隔的表达式时, 它将基于字符串中的前缀为该表达式创建一个表达式生成器。When the page parser encounters an expression that is delimited with the string <%$ %>, it creates an expression builder for the expression based on the prefix in the string. 前缀是位于冒号 (:) 的左侧的字符串部分。The prefix is the portion of the string that is to the left of the colon (:). 例如, 当分析器遇到字符串<%$ ConnectionStrings:MessageDB %>时, 它将创建一个ConnectionStringsExpressionBuilder对象。For example, when the parser encounters the string <%$ ConnectionStrings:MessageDB %>, it creates a ConnectionStringsExpressionBuilder object. 前缀与ExpressionBuilders节中 web.config 文件中的表达式生成器关联。Prefixes are associated with expression builders in the Web.config file in the ExpressionBuilders section.

声明性表达式的右侧传递到表达式生成器进行计算。The right side of the declarative expression is passed to the expression builder for evaluation. 重写GetCodeExpression方法以生成将与页一起编译的代码。Override the GetCodeExpression method to generate code that will be compiled with the page.

如果希望自定义表达式生成器在未编译的页上处于活动状态, 则还必须重写EvaluateExpression方法以返回表示该表达式结果的对象。If you want the custom expression builder to be active on pages that are not compiled, you must also override the EvaluateExpression method to return an object that represents the results of the expression. 还必须重写SupportsEvaluate属性, 以指示自定义表达式生成器不支持编译页。You also must override the SupportsEvaluate property to indicate that the custom expression builder does support no-compile pages.

通过实现表达式编辑器, 您可以在设计时定义与控件属性关联的一组属性和方法。You can define a set of properties and methods for selecting and evaluating an expression that is associated with a control property at design time by implementing an expression editor. 编辑器通过类级元数据在表达式生成器上进行标记。The editor is marked on the expression builder through class-level metadata. 有关详细信息,请参阅 ExpressionEditorFor more information, see ExpressionEditor.


ExpressionBuilder类继承时, 必须GetCodeExpression(BoundPropertyEntry, Object, ExpressionBuilderContext)重写方法。When you inherit from the ExpressionBuilder class, you must override the GetCodeExpression(BoundPropertyEntry, Object, ExpressionBuilderContext) method.



初始化 ExpressionBuilder 类的新实例。Initializes a new instance of the ExpressionBuilder class.



在派生类中重写时,会返回一个值,该值指示当前 ExpressionBuilder 对象是否支持非编译页。When overridden in a derived class, returns a value indicating whether the current ExpressionBuilder object supports no-compile pages.



确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
EvaluateExpression(Object, BoundPropertyEntry, Object, ExpressionBuilderContext)

在派生类中重写时,会返回一个对象,该对象表示计算出的表达式。When overridden in a derived class, returns an object that represents an evaluated expression.

GetCodeExpression(BoundPropertyEntry, Object, ExpressionBuilderContext)

在派生类中重写时,返回在页执行过程中用来获取计算出的表达式的代码。When overridden in a derived class, returns code that is used during page execution to obtain the evaluated expression.


作为默认哈希函数。Serves as the default hash function.

(继承自 Object)

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ParseExpression(String, Type, ExpressionBuilderContext)

在派生类中重写时,会返回一个对象,该对象表示通过分析得到的表达式。When overridden in a derived class, returns an object that represents the parsed expression.


返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)