Редагувати

Поділитися через


ExpressionBuilder Class

Definition

Evaluates expressions during page parsing.

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

Examples

The following code examples demonstrate how to build a custom expression builder by implementing the ExpressionBuilder abstract class. This implementation of ExpressionBuilder returns an evaluated statement that is passed to the expression. To run this example, you must first register the custom expression builder in the Web.config file. The first code example demonstrates how to register the custom expression builder in the Web.config file.

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

The second code example demonstrates how to reference the expression in an .aspx file.

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

The third code example demonstrates how to develop a customized expression builder by deriving from ExpressionBuilder. 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.
[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

Remarks

The 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 (:). For example, when the parser encounters the string <%$ ConnectionStrings:MessageDB %>, it creates a ConnectionStringsExpressionBuilder object. 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. Override the GetCodeExpression method to generate code that will be compiled with the page.

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. 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. For more information, see ExpressionEditor.

Notes to Implementers

When you inherit from the ExpressionBuilder class, you must override the GetCodeExpression(BoundPropertyEntry, Object, ExpressionBuilderContext) method.

Constructors

ExpressionBuilder()

Initializes a new instance of the ExpressionBuilder class.

Properties

SupportsEvaluate

When overridden in a derived class, returns a value indicating whether the current ExpressionBuilder object supports no-compile pages.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from 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.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ParseExpression(String, Type, ExpressionBuilderContext)

When overridden in a derived class, returns an object that represents the parsed expression.

ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also