Spring.net 表达式解析ExpressionEvaluator

1.类定义
public class Company
    {
        private string name;
        private Employee managingDirector;
        public string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }

        public Employee ManagingDirector
        {
            get { return this.managingDirector; }
            set { this.managingDirector = value; }
        }
    }
    public class Employee
    {
        private string name;
        private float salary;

        public string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }
        public float Salary
        {
            get { return salary; }
            set { this.salary = value; }
        }
    }
    public class Inventor
    {
        public Inventor(string name, DateTime t, string city)
        {
            Name = name;
            date = t;
            PlaceOfBirth = new PlaceOfBirth() {City=city};

        }
        public DateTime date;
        public string Name { get; set; }
        public PlaceOfBirth PlaceOfBirth { get; set; }

    }
2.代码实现
            Inventor tesla = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian");
            tesla.PlaceOfBirth.City = "Smiljan";
            string evaluatedName = (string)ExpressionEvaluator.GetValue(tesla, "Name");
            string evaluatedCity = (string)ExpressionEvaluator.GetValue(tesla, "PlaceOfBirth.City");
            ExpressionEvaluator.SetValue(tesla, "PlaceOfBirth.City", "Novi Sad");
   IExpression exp = Expression.Parse("Name");
            string evaluatedName1 = (string)exp.GetValue(tesla, null);
2.1 Literal expressions
    string helloWorld = (string)ExpressionEvaluator.GetValue(null, "‘Hello World‘"); // evals to "Hello World"
// string tonyPizza = (string)ExpressionEvaluator.GetValue(null, "‘Tony\\‘s Pizza‘"); // evals to "Tony‘s

double avogadrosNumber = (double)ExpressionEvaluator.GetValue(null, "6.0221415E+23");
int maxValue = (int)ExpressionEvaluator.GetValue(null, "0x7FFFFFFF"); // evals to 2147483647
DateTime birthday = (DateTime)ExpressionEvaluator.GetValue(null, "date(‘1974/08/24‘)");
DateTime exactBirthday =
(DateTime)ExpressionEvaluator.GetValue(null, " date(‘19740824T131030‘, ‘yyyyMMddTHHmmss‘)");
bool trueValue = (bool)ExpressionEvaluator.GetValue(null, "true"); object nullValue = ExpressionEvaluator.GetValue(null, "null");
2.2 Properties, Arrays, Lists, Dictionaries, Indexers
int year = (int) ExpressionEvaluator.GetValue(tesla, "DOB.Year")); // 1856 string city = (string) ExpressionEvaluator.GetValue(pupin, "PlaCeOfBirTh.CiTy"); // "Idvor"
// Inventions Array
string invention = (string) ExpressionEvaluator.GetValue(tesla, "Inventions[3]"); // "Induction motor" // Members List string name = (string) ExpressionEvaluator.GetValue(ieee, "Members[0].Name"); // "Nikola Tesla" // List and Array navigation string invention = (string) ExpressionEvaluator.GetValue(ieee, "Members[0].Inventions[6]") // "Wireless
// Officer‘s Dictionary
Inventor pupin = (Inventor) ExpressionEvaluator.GetValue(ieee, "Officers[‘president‘]"; string city = (string) ExpressionEvaluator.GetValue(ieee, "Officers[‘president‘].PlaceOfBirth.City"); // "Idvor" ExpressionEvaluator.SetValue(ieee, "Officers[‘advisors‘][0].PlaceOfBirth.Country", "Croatia");
//index 索引器
public class Bar
{
 private int[] numbers = new int[] {1, 2, 3};
 public int this[int index]
 {
  get
  {
    return numbers[index];
  }
  set
  {
   numbers[index] = value;
 }
  }
 Bar b = new Bar(); int val = (int) ExpressionEvaluator.GetValue(bar, "[1]") // evaluated to 2 ExpressionEvaluator.SetValue(bar, "[1]", 3); // set value to 3
2.3 Methods

//string literal char[] chars = (char[]) ExpressionEvaluator.GetValue(null, "‘test‘.ToCharArray(1, 2)")) // ‘t‘,‘e‘

//date literal int year = (int) ExpressionEvaluator.GetValue(null, "date(‘1974/08/24‘).AddYears(31).Year") // 2005// object usage, calculate age of tesla navigating from the IEEE society.   ExpressionEvaluator.GetValue(ieee, "Members[0].GetAge(date(‘2005-01-01‘)") // 149 (eww..a big anniversary is2.4 操作ExpressionEvaluator.GetValue(null, "2 == 2") // true ExpressionEvaluator.GetValue(null, "date(‘1974-08-24‘) != DateTime.Today") // trueExpressionEvaluator.GetValue(null, "2 < -5.0") // false ExpressionEvaluator.GetValue(null, "DateTime.Today <= date(‘1974-08-24‘)") // falseExpressionEvaluator.GetValue(null, "‘Test‘ >= ‘test‘") // trueFooColor fColor = new FooColor(); ExpressionEvaluator.SetValue(fColor, "Color", KnownColor.Blue); bool trueValue = (bool) ExpressionEvaluator.GetValue(fColor, "Color == KnownColor.Blue"); //true2.5表达式ExpressionEvaluator.GetValue(null, "3 in {1, 2, 3, 4, 5}") // trueExpressionEvaluator.GetValue(null, "‘Abc‘ like ‘[A-Z]b*‘") // trueExpressionEvaluator.GetValue(null, "‘Abc‘ like ‘?‘") // falseExpressionEvaluator.GetValue(null, "1 between {1, 5}") // trueExpressionEvaluator.GetValue(null, "‘efg‘ between {‘abc‘, ‘xyz‘}") // trueExpressionEvaluator.GetValue(null, "‘xyz‘ is int") // falseExpressionEvaluator.GetValue(null, "{1, 2, 3, 4, 5} is IList") // trueExpressionEvaluator.GetValue(null, "‘5.0067‘ matches ‘^-?\\d+(\\.\\d{2})?$‘")) // falseExpressionEvaluator.GetValue(null, @"‘5.00‘ matches ‘^-?\d+(\.\d{2})?$‘") // true2.6 Logical operatorsbool falseValue = (bool) ExpressionEvaluator.GetValue(null, "true and false"); //false string expression = @"IsMember(‘Nikola Tesla‘) and IsMember(‘Mihajlo Pupin‘)"; bool trueValue = (bool) ExpressionEvaluator.GetValue(ieee, expression); //true bool trueValue = (bool) ExpressionEvaluator.GetValue(null, "true or false"); //true string expression = @"IsMember(‘Nikola Tesla‘) or IsMember(‘Albert Einstien‘)"; bool trueValue = (bool) ExpressionEvaluator.GetValue(ieee, expression); // true // NOT bool falseValue = (bool) ExpressionEvaluator.GetValue(null, "!true"); // AND and NOT string expression = @"IsMember(‘Nikola Tesla‘) and !IsMember(‘Mihajlo Pupin‘)"; bool falseValue = (bool) ExpressionEvaluator.GetValue(ieee, expression);2.7 位运算// AND int result = (int) ExpressionEvaluator.GetValue(null, "1 and 3"); // 1 & 3 // OR int result = (int) ExpressionEvaluator.GetValue(null, "1 or 3"); // 1 | 3 // XOR int result = (int) ExpressionEvaluator.GetValue(null, "1 xor 3"); // 1 ^ 3 // NOT int result = (int) ExpressionEvaluator.GetValue(null, "!1"); // ~12.8 数学运算// Addition int two = (int)ExpressionEvaluator.GetValue(null, "1 + 1"); // 2 String testString = (String)ExpressionEvaluator.GetValue(null, "‘test‘ + ‘ ‘ + ‘string‘"); //‘test string‘DateTime dt = (DateTime)ExpressionEvaluator.GetValue(null, "date(‘1974-08-24‘) + 5"); // 8/29/1974 // Subtraction int four = (int) ExpressionEvaluator.GetValue(null, "1 - -3"); //4 Decimal dec = (Decimal) ExpressionEvaluator.GetValue(null, "1000.00m - 1e4"); // 9000.00 TimeSpan ts = (TimeSpan) ExpressionEvaluator.GetValue(null, "date(‘2004-08-14‘) - date(‘1974-08-24‘)"); //10948.00:00:00 //int six = (int) ExpressionEvaluator.GetValue(null, "-2 * -3"); // 6 int twentyFour = (int) ExpressionEvaluator.GetValue(null, "2.0 * 3e0 * 4"); // 24 // Division int minusTwo = (int) ExpressionEvaluator.GetValue(null, "6 / -3"); // -2 int one = (int) ExpressionEvaluator.GetValue(null, "8.0 / 4e0 / 2"); // 1 // Modulus int three = (int) ExpressionEvaluator.GetValue(null, "7 % 4"); // 3 int one = (int) ExpressionEvaluator.GetValue(null, "8.0 % 5e0 % 2"); // 1int sixteen = (int) ExpressionEvaluator.GetValue(null, "-2 ^ 4"); // 16 // Operator precedence int minusFortyFive = (int) ExpressionEvaluator.GetValue(null, "1+2-3*8^2/2/2"); // -452.9 AssignmentInventor inventor = new Inventor(); String aleks = (String) ExpressionEvaluator.GetValue(inventor, "Name = ‘Aleksandar Seovic‘");DateTime dt = (DateTime) ExpressionEvaluator.GetValue(inventor, "DOB = date(‘1974-08-24‘)"); //Set the vice president of the societyInventor tesla = (Inventor) ExpressionEvaluator.GetValue(ieee, "Officers[‘vp‘] = Members[0]");3.0 表达式列表//Perform property assignments and then return Name property. String pupin = (String) ExpressionEvaluator.GetValue(ieee.Members, "( [1].PlaceOfBirth.City = ‘Beograd‘; [1].PlaceOfBirth.Country = ‘Serbia‘; [1].Name )")); // pupin = "Mihajlo Pupin"3.1 类型ExpressionEvaluator.GetValue(null, "1 is int") ExpressionEvaluator.GetValue(null, "DateTime.Today")ExpressionEvaluator.GetValue(null, "new string[] {‘abc‘, ‘efg‘}")Type dateType = (Type) ExpressionEvaluator.GetValue(null, "T(System.DateTime)") Type evalType = (Type) ExpressionEvaluator.GetValue(null, "T(Spring.Expressions.ExpressionEvaluator, Spring.Core)") bool trueValue = (bool) ExpressionEvaluator.GetValue(tesla, "T(System.DateTime) == DOB.GetType()")3.2 类型注册TypeRegistry.RegisterType("Society", typeof(Society)); Inventor pupin = (Inventor) ExpressionEvaluator.GetValue(ieee, "Officers[Society.President]");3.3 VariablesIDictionary vars = new Hashtable(); vars["newName"] = "Mike Tesla"; ExpressionEvaluator.GetValue(tesla, "Name = #newName", vars));ExpressionEvaluator.GetValue(tesla, "{ #oldName = Name; Name = ‘Nikola Tesla‘ }", vars); String oldName = (String)vars["oldName"]; // Mike Teslavars["prez"] = "president"; Inventor pupin = (Inventor) ExpressionEvaluator.GetValue(ieee, "Officers[#prez]", vars);

  

时间: 2024-10-06 12:07:02

Spring.net 表达式解析ExpressionEvaluator的相关文章

基于Java的简易表达式解析工具(二)

之前简单的介绍了这个基于Java表达式解析工具,现在把代码分享给大家,希望帮助到有需要的人们,这个分享代码中依赖了一些其他的类,这些类大家可以根据自己的情况进行导入,无非就是写字符串处理工具类,日期处理的工具类什么的. 这个Java的表达式解析的工具只用了5个类,而且写得也很简单明了,相信有一些编程经验的可以看懂这些处理的逻辑代码. 1.第一个类:ExpressionNodeType(表达式各个字符节点的类型枚举类) public enum ExpressionNodeType { Unknow

Spring源码解析 - BeanFactory接口体系解读

不知道为什么看着Spring的源码,感触最深的是Spring对概念的抽象,所以我就先学接口了. BeanFactory是Spring IOC实现的基础,这边定义了一系列的接口,我们通过这些接口的学习,可以大致了解BeanFactory体系各接口如何分工合作. 为学习具体实现打下基础.毕竟这边逻辑复杂,涉及的概念很多. BeanFactory 是Spring bean容器的根接口.提供获取bean,是否包含bean,是否单例与原型,获取bean类型,bean 别名的api. -- Autowire

Spring讲解-----------表达式语言

5.1  概述 5.1.1  概述 Spring表达式语言全称为"Spring Expression Language",缩写为"SpEL",类似于Struts2x中使用的OGNL表达式语言,能在运行时构建复杂表达式.存取对象图属性.对象方法调用等等,并且能与Spring功能完美整合,如能用来配置Bean定义. 表达式语言给静态Java语言增加了动态功能. SpEL是单独模块,只依赖于core模块,不依赖于其他模块,可以单独使用. 5.1.2  能干什么 表达式语言

Spring 源码解析之DispatcherServlet源码解析(五)

Spring 源码解析之DispatcherServlet源码解析(五) 前言 本文需要有前四篇文章的基础,才能够清晰易懂,有兴趣可以先看看详细的流程,这篇文章可以说是第一篇文章,也可以说是前四篇文章的的汇总,Spring的整个请求流程都是围绕着DispatcherServlet进行的 类结构图 根据类的结构来说DispatcherServlet本身也是继承了HttpServlet的,所有的请求都是根据这一个Servlet来进行转发的,同时解释了为什么需要在web.xml进行如下配置,因为Spr

Java 表达式解析(非原创)

因项目需要,在网上找来一套表达式解析方法,由于原来的方法太过于零散,不利于移植,现在整理在同一文件内: 文件中包含5个内部类,源码如下: 1 import java.util.ArrayList; 2 import java.util.Date; 3 import java.util.List; 4 import java.util.Stack; 5 6 /** 7 * @项目名称: sunson_pams 8 * @类名称: FormulaUtils 9 * @类描述: 非原创(慎用) 10

Spring源码解析 – AnnotationConfigApplicationContext容器创建过程

Spring在BeanFactory基础上提供了一些列具体容器的实现,其中AnnotationConfigApplicationContext是一个用来管理注解bean的容器,从AnnotationConfigApplicationContext的实现结构图中可以看出: AnnotationConfigApplicationContext继承GenericApplicationContext这个通用应用上下文,GenericApplicationContext内部定义了一个DefaultList

SPEL 表达式解析

Spring Expression Language 解析器 SPEL解析过程 使用 ExpressionParser 基于 ParserContext 将字符串解析为 Expression, Expression 再根据 EvaluationContext 计算表达式的值. 将字符串解析为 Expression StandardBeanExpressionResolver# /** 默认表达式前缀 */ public static final String DEFAULT_EXPRESSION

Spring 缓存注解解析过程

Spring 缓存注解解析过程 通过 SpringCacheAnnotationParser 的 parseCacheAnnotations 方法解析指定方法或类上的缓存注解, @Cacheable 注解将被解析为 CacheableOperation 操作, @CachePut 注解将被解析为 CachePutOperation 操作, @CacheEvict 注解将被解析为 CacheEvictOperation 操作. 缓存注解 /** * 启用Spring以注解驱动的缓存管理功能 */

把功能强大的Spring EL表达式应用在.net平台

Spring EL 表达式是什么? Spring3中引入了Spring表达式语言—SpringEL,SpEL是一种强大,简洁的装配Bean的方式,他可以通过运行期间执行的表达式将值装配到我们的属性或构造函数当中,更可以调用C#中提供的静态常量,获取外部json xml文件中的的配置值 为什么要使用SpringEL? 可以方便的注入 外部配置文件到 类的构造方法,属性或者 字段,支持注入容器里面的对象的某个属性值,还可以调用对象的方法,功能非常的强大,请看官方文档的例子或者下面我的单元测试例子 S