标签:
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 is
2.4 操作
ExpressionEvaluator.GetValue(null, "2 == 2") // true
ExpressionEvaluator.GetValue(null, "date(‘1974-08-24‘) != DateTime.Today") // true
ExpressionEvaluator.GetValue(null, "2 < -5.0") // false
ExpressionEvaluator.GetValue(null, "DateTime.Today <= date(‘1974-08-24‘)") // false
ExpressionEvaluator.GetValue(null, "‘Test‘ >= ‘test‘") // true
FooColor fColor = new FooColor(); ExpressionEvaluator.SetValue(fColor, "Color", KnownColor.Blue); bool trueValue = (bool) ExpressionEvaluator.GetValue(fColor, "Color == KnownColor.Blue"); //true
2.5表达式
ExpressionEvaluator.GetValue(null, "3 in {1, 2, 3, 4, 5}") // true
ExpressionEvaluator.GetValue(null, "‘Abc‘ like ‘[A-Z]b*‘") // true
ExpressionEvaluator.GetValue(null, "‘Abc‘ like ‘?‘") // false
ExpressionEvaluator.GetValue(null, "1 between {1, 5}") // true
ExpressionEvaluator.GetValue(null, "‘efg‘ between {‘abc‘, ‘xyz‘}") // true
ExpressionEvaluator.GetValue(null, "‘xyz‘ is int") // false
ExpressionEvaluator.GetValue(null, "{1, 2, 3, 4, 5} is IList") // true
ExpressionEvaluator.GetValue(null, "‘5.0067‘ matches ‘^-?\\d+(\\.\\d{2})?$‘")) // false
ExpressionEvaluator.GetValue(null, @"‘5.00‘ matches ‘^-?\d+(\.\d{2})?$‘") // true
2.6 Logical operators
bool 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"); // ~1
2.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"); // 1
int sixteen = (int) ExpressionEvaluator.GetValue(null, "-2 ^ 4"); // 16
// Operator precedence
int minusFortyFive = (int) ExpressionEvaluator.GetValue(null, "1+2-3*8^2/2/2"); // -45
2.9 Assignment
Inventor 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 society
Inventor 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 Variables
IDictionary 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 Tesla
vars["prez"] = "president";
Inventor pupin = (Inventor) ExpressionEvaluator.GetValue(ieee, "Officers[#prez]", vars);
Spring.net 表达式解析ExpressionEvaluator
标签:
原文地址:http://www.cnblogs.com/kexb/p/5922163.html