This documentation is archived and is not being maintained.

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.

Protected methodExpressionBuilderInitializes a new instance of the ExpressionBuilder class.

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

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 a hash function for a particular type. (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.)

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.

              <add expressionPrefix="MyCustomExpression"

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.
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, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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.