Export (0) Print
Expand All

ExpressionBuilder Class

Evaluates expressions during page parsing.

Namespace:  System.Web.Compilation
Assembly:  System.Web (in System.Web.dll)

public abstract class ExpressionBuilder

The ExpressionBuilder type exposes the following members.

  NameDescription
Protected methodExpressionBuilderInitializes a new instance of the ExpressionBuilder class.
Top

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

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodEvaluateExpressionWhen overridden in a derived class, returns an object that represents an evaluated expression.
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetCodeExpressionWhen overridden in a derived class, returns code that is used during page execution to obtain the evaluated expression.
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodParseExpressionWhen overridden in a derived class, returns an object that represents the parsed expression.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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 Inheritors

When you inherit from the ExpressionBuilder class, you must override the GetCodeExpression method.

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; }
    }
}

.NET Framework

Supported in: 4.6, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft