How to build an Expression builder for ASP.Net 2.0

原创 2004年10月06日 14:08:00
 

When you have read the posts on my blog, you have probably seen the new expression like the <% $ resource … %>, <% $ ConnectionStrings: Name %> etc.

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

If you have not seen them before, then take a look at this post on my blog.

 

You can build your own expression by inherits the ExpressionBuilder class. The ExpressionBuilder class has one method that must be implemented, GetCodeExpression.

 

The GetCodeExpression is used to return the code that will be evaluated during the parsing of the page to return the result of the expression.

 

You can also override the ExpressionBuilder’s EvaluteExpression method if you want your expression builder to be active on no-compile pages. This method most returns the results of the expression. If you implement the EvaluteExpression method, you must also override the SupportsEvaluate property, this property will return a Boolean value, and most return true if the EvaluteExpression method should be used.

 

In the example I’m going to show you, The "My" expression prefix is used:

 

<%$ My:MyExpression %>

 

MyExpression is the expression that is going to be sent to the custom expression builder as the value of the expression.

 

The following code is a very simple custom expression builder that will only return the expression that is specified after the "My" prefix:

 

using System;

using System.CodeDom;

using System.Web.UI;

using System.ComponentModel;

using System.Web.Compilation;

 

public class MyExpressionBuilder : ExpressionBuilder

{

    public MyExpressionBuilder()

    {

    }

 

 

    public static object GetData(string expression, Type targetType, string entryName)

    {

        return expression;

    }

 

 

    public override object EvaluateExpression(object target, BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)

    {

        return MyExpressionBuilder.GetData(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()), "GetData", expressionArray1));

    }

 

 

    public override bool SupportsEvaluate

    {

        get

        {

            return true;

        }

    }

}

 

To use the custom expression builder, you have to register it to the web.config in the <expressionBuilders> section:

 

<configuration>

    <system.web>

       <compilation>

          <expressionBuilders>

              <add expressionPrefix="My" type="MyExpressionBuilder"/>

          </expressionBuilders>

       </compilation>

    </system.web>

</configuration>

 

The ExpressionPrefix attribute of the add element, is the name of the expression prefix:

 

<%$ expressionPrefixName: value %>

 

With the expressionPrefix set to "My", the expression would look like this:

 

<%$ My: value %>

 

The type attribute of the add element, is the type of the custom expression builder you have created. If you have added the custom expression builder to the /Code folder, you only need to write the name of the class. If you have created a separated class library, you have to enter the full name of the class and the assembly where the class exists.

 

Now let’s take a look at the code for MyExpressionBuilder.

 

First you need to add a method that will return the results for your expression, in this example the GetData method will be used to return the results of the expression:

 

public static object GetData(string expression, Type targetType, string entryName)

{

     return expression;

}

 

The code above is used to return the results of the expression argument. In this case it will return the value of the expression. The arguments of the GetData method, will contain the value of the expression, the targetType is the type of where the expression is bounded to, for example a TextBox control etc. The last argument, entryName, is the name of the property where the expression is bounded to. You can for example us the targetType to check if your expression is allowed for the property where the expression is specified.

 

As I mentioned earlier in my post, you can override the ExpressionBuilder’s EvaluteExpression method if you want your expression builder to be active on no-compile pages. The following code overrides the EvaluateExpression method and will call the GetData methoded described above:

 

public override object EvaluateExpression(object target, BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)

    {

        return MyExpressionBuilder.GetData(entry.Expression, target.GetType(), entry.Name);

    }

 

    public override bool SupportsEvaluate

    {

        get

        {

            return true;

        }

    }

 

 

The EvaluteExpression method has four arguments, target, entry, paresedData and context. I will only explain the one used in this example:

 

Target

 

The target arguments have the type of the type where the expression is bounded to.

 

Entry

 

The entry argument holds information about where the expression is bounded, such as the name of the property where the expression is bounded to, and the value of the expression etc.

 

As you can see in the code above, the method will do a call to the GetData method where the expression (value), the type of target and the name of the property where the expression is bound to are passed as arguments. By default the SupportEvalute property will return false, so to use the EvaluteExpression method, the SupportEvaluate property must be override and return true.

 

The next method to override is the GetCodeExpression. This method returns a CodeExpression class. It’s a class with the information about the method that will return the results for the expression, in this case the GetData method:

   

    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()), "GetData", expressionArray1));

    }

 

The method above will use the CodeCastExpression to specify a CodeExpression (The CodeCastExpression inherits the CodeExpression class). As you can see in the code above, I use the CodeCastExpression to specify the type of the property where the expression is bounded to, and the CodeTypeReferenceExpression to specify which method (GetData) that should be executed to get the results from for the expression. The argument to the GetData method is passed as a CodeExpression array where the items in the array are the value of the arguments that will be passed to the GetData method. I don’t think I need to write more about that, you will probably understand the code, if not, please don’t hesitate to contact me.

 

Here is an example how you can use the custom expression provider:

 

<asp:textbox Id="test" runat="server" Text='<%$ My: Welcome%>'></asp:textbox>

 

When you run this code, the TextBox’s Text property will have the value set to "Welcome".

自己编译JavaFX Scene Builder

虽然没有捣鼓JAVAFX,但是 近日发现Oracle官网竟然不提供JAVAFX Scene Builder的下载的。原先发布的2.0版本提示过期,心中一怒。几番寻找发现Scene Builder被迁至...
  • xby1993
  • xby1993
  • 2015年03月27日 08:01
  • 3066

opencl之code builder学习教程3

一下文章中缺少图片,请在百度网盘下载word观看 https://pan.baidu.com/s/1sloNYyx Intel® Code Builder for OpenCL™API for Mi...
  • qccz123456
  • qccz123456
  • 2017年12月19日 19:59
  • 140

使用ExpressionBuilder 类 在分析页的过程中计算表达式

ExpressionBuilder 类是在分析页的过程中创建代码表达式的表达式生成器的基类。 表达式生成器分析声明性表达式,并创建代码来检索绑定到控件属性的值。在非编译方案中,支持非编译功能的表达式生...
  • a497785609
  • a497785609
  • 2010年06月28日 11:50
  • 1251

让SQL Server 2008默认使用Report Builder 2.0做报表设计器

由于SQL Server 2008比Report Builder 2.0先发布,所以默认情况下,在SSRS 2008中的报表管理器中单击“报表生成器”,打开的是Report Builder 1.0版本...
  • studyzy
  • studyzy
  • 2009年11月05日 22:34
  • 6191

How to Build an Economic Model in Your Spare Time (如何利用业余时间建立经济模型)

如何利用业余时间建立经济模型 How to Build an Economic Model in Your Spare Time by Hal R. Varian UC Berkeley Decemb...
  • metababy
  • metababy
  • 2008年10月20日 21:25
  • 3149

【C++】Visual Studio 2012 MFC——Expression: _pFirstBlock==pHead解决办法

遇到调试错误:Expression: _pFirstBlock==pHead。查阅了好久不知道是什么原因。最后发现是工程设置问题。现已经解决了。 ----------》debug版下:属性--配置属性...
  • u013346007
  • u013346007
  • 2016年06月11日 21:52
  • 2102

Java 实现建造者(Builder)模式

建造者(Builder)模式是创建型模式,创建了一个对外隐藏创建过程的产品,使用组合的方式,由指挥者(Director)来决定建造的流程 public interface Builder { vo...
  • jjwwmlp456
  • jjwwmlp456
  • 2014年10月08日 10:58
  • 3310

How to Print in ASP.NET 2.0

One of the most common functionality in any ASP.NET application is to print forms and controls. Ther...
  • civilman
  • civilman
  • 2010年12月02日 16:16
  • 3174

数据结构与算法习题 Expression Expression tree Expression evaluation(栈与树结构综合)

数据结构与算法习题 Expression Expression tree Expression evaluation(栈与树结构综合)
  • PKU_ZZY
  • PKU_ZZY
  • 2016年10月14日 09:40
  • 594

asp.net core 2.0 入门攻略 第一章 环境部署

一、首先大家先部署环境安装 因为大家当前是研究学习用途,所以建议大家使用VS社区版...
  • loongsking
  • loongsking
  • 2017年10月07日 09:40
  • 1379
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:How to build an Expression builder for ASP.Net 2.0
举报原因:
原因补充:

(最多只允许输入30个字)