自定义ExpressionBuilder
ExpressionBuilder的常见说明见https://msdn.microsoft.com/zh-cn/library/System.Web.Compilation.ExpressionBuilder(v=vs.80).aspx
下面贴代码:
编写自定义ExpressionBuilder用于翻译,翻译文件缓存依赖。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web.Compilation;
using System.Web.UI;
using System.CodeDom;
using System.Web.Hosting;
using System.Web.Caching;
using System.Xml; namespace ExpressionBuilderTest.Library
{
/// <summary>
/// 自定义ExpressionBuilder
/// </summary>
public class SPExpressionBuilder:ExpressionBuilder
{
// Create a method that will return the result
// set for the expression argument.
public static object GetEvalData(string expression, Type target, string entry)
{
XmlDocument doc = (XmlDocument)HostingEnvironment.Cache["transDoc"];
if (doc == null)
{
doc = new XmlDocument();
string filePath = HostingEnvironment.MapPath("~/Translate.config");
doc.Load(filePath);
CacheDependency fileDep = new CacheDependency(filePath);
HostingEnvironment.Cache.Insert("transDoc", doc,fileDep);
}
XmlNode xn = doc.SelectSingleNode(string.Format("/configuration/p[@key='{0}']",expression));
if (xn != null)
{
return ((XmlElement)xn).GetAttribute("value");
}
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[];
expressionArray1[] = new CodePrimitiveExpression(entry.Expression.Trim());
expressionArray1[] = new CodeTypeOfExpression(type1);
expressionArray1[] = new CodePrimitiveExpression(entry.Name);
return new CodeCastExpression(descriptor1.PropertyType, new CodeMethodInvokeExpression(new
CodeTypeReferenceExpression(base.GetType()), "GetEvalData", expressionArray1));
} public override bool SupportsEvaluate
{
get { return true; }
}
}
}
翻译文件为Translate.config.格式如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<p key="_welCom_" value="You Are Welcome!"/>
</configuration>
好了,现在在web.config里配置就可以使用了。web.config配置如下:
<?xml version="1.0" encoding="utf-8"?> <configuration>
<system.web>
<compilation debug="true" targetFramework="4.0">
<expressionBuilders>
<add expressionPrefix="SPExpression"
type="ExpressionBuilderTest.Library.SPExpressionBuilder"/>
</expressionBuilders>
</compilation>
</system.web> </configuration>
在页面上拖一个label
<asp:Label ID="Label1" runat="server" Text="<%$SPExpression:_welCom_ %>"></asp:Label>
即可实现翻译。
自定义ExpressionBuilder的更多相关文章
- Asp.Net之自定义表达式构造器(ExpressionBuilder)
在Asp.Net的Page页面中我们经常会使用到资源文件.读取资源文件的方式为:<%$Resources:Resource1,Test1%>,这样就可以显示Resource1这个资源文件中 ...
- 关于Unity3D自定义编辑器的学习
被人物编辑器折腾了一个月,最终还是交了点成品上去(还要很多优化都还么做). 刚接手这项工作时觉得没概念,没想法,不知道.后来就去看<<Unity5.X从入门到精通>>中有关于 ...
- 一起学微软Power BI系列-使用技巧(5)自定义PowerBI时间日期表
1.日期函数表作用 经常使用Excel或者PowerBI,Power Pivot做报表,时间日期是一个重要的纬度,加上做一些钻取,时间日期函数表不可避免.所以今天就给大家分享一个自定义的做日期表的方法 ...
- JavaScript自定义浏览器滚动条兼容IE、 火狐和chrome
今天为大家分享一下我自己制作的浏览器滚动条,我们知道用css来自定义滚动条也是挺好的方式,css虽然能够改变chrome浏览器的滚动条样式可以自定义,css也能够改变IE浏览器滚动条的颜色.但是css ...
- ASP.NET Aries 入门开发教程8:树型列表及自定义右键菜单
前言: 前面几篇重点都在讲普通列表的相关操作. 本篇主要讲树型列表的操作. 框架在设计时,已经把树型列表和普通列表全面统一了操作,用法几乎是一致的. 下面介绍一些差距化的内容: 1:树型列表绑定: v ...
- ASP.NET Aries 入门开发教程5:自定义列表页工具栏区
前言: 抓紧时间,继续写教程,因为发现用户期待的内容,都在业务处理那一块. 不得不继续勤劳了. 这节主要介绍工具栏区的玩法. 工具栏的默认介绍: 工具栏默认包括5个按钮,根据不同的权限决定显示: 添加 ...
- UWP中实现自定义标题栏
UWP中实现自定义标题栏 0x00 起因 在UWP开发中,有时候我们希望实现自定义标题栏,例如在标题栏中加入搜索框.按钮之类的控件.搜了下资料居然在一个日文网站找到了一篇介绍这个主题的文章: http ...
- JavaScript 自定义对象
在Js中,除了Array.Date.Number等内置对象外,开发者可以通过Js代码创建自己的对象. 目录 1. 对象特性:描述对象的特性 2. 创建对象方式:对象直接量.new 构造函数.Objec ...
- 【WCF】自定义错误处理(IErrorHandler接口的用法)
当被调用的服务操作发生异常时,可以直接把异常的原始内容传回给客户端.在WCF中,服务器传回客户端的异常,通常会使用 FaultException,该异常由这么几个东东组成: 1.Action:在服务调 ...
随机推荐
- Mysql任务调度
Mysql任务调度 Event调度配置 Mysql任务调度Event不执行 Mysql任务作业Event不执行 我采用的方法就是: 方法一:找到当前使用的 .cnf 文件,在 [mysqld] 的下面 ...
- .Net连接数据库(AOD.Net)
用C#实现用户名登录---连接数据库,查询并判断是否存在用户名 一.链接类 1.添加DBConnection类 2构造 public class DBConnection { private stat ...
- sql 集合运算
UNION 并运算 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每条 SE ...
- 用sql的select语句从数据库中获取数据
基本的select语句 select语句中的算数表达式和NULL值 列的别名 使用连接符操作,literal character strings,alternative quote operator, ...
- 屏幕 Dynpro
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- SQL——连接查询
以mysql为例: 新建两张表table1和table2 CREATE TABLE `table1` ( `id` ) NOT NULL auto_increment, `name` ) defaul ...
- x名称空间
XAML代码的WPF程序都需要通过语句:xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml",x就是用来映射xmlns:x= ...
- 再学C++之C++中的全部关键字
/*______C++全部关键字___________*/ asm do if return try auto double inline short typedef bool dynamic_cas ...
- ubuntu下安装gradle
1.下载gradle 下载地址:当前版本gradle-2.10-all.zip http://gradle.org/gradle-download/ 2.解压安装gradle unzip gradle ...
- Send to Kindle :一键推送网页内容到多看
http://site.douban.com/129629/widget/notes/7074800/note/207072907/ 注意:增加配置信息,一键发送,方便及时分享网页.