public void TestGetValue_DateTimeNow() { Expression <Func <Sample, bool> > expression = t => t.DateValue == DateTime.Now; var value = Lambdas.GetValue(expression); Assert.NotNull(Conv.ToDateOrNull(value)); }
private Lambda CompileLambdaBody(Source src, Parameter[] parameters, AstStatement body, DelegateType dt) { var result = new Lambda(src, parameters, dt, null); Lambdas.Push(result); if (body is AstExpression) { var compiledExpr = CompileExpression((AstExpression)body); var compiledBody = MakeLambdaStatementBody( dt.ReturnType.IsVoid ? compiledExpr : CompileImplicitCast(body.Source, dt.ReturnType, compiledExpr), dt.ReturnType.IsVoid); result.SetBody(compiledBody); } else { result.SetBody(CompileStatement(body)); } Lambdas.Pop(); return(result); }
public void TestGetValue() { Assert.Null(Lambdas.GetValue(null)); Expression <Func <Sample, bool> > expression = t => t.StringValue == "A"; Assert.Equal("A", Lambdas.GetValue(expression)); Expression <Func <Sample, bool> > expression2 = t => t.Test2.IntValue == 1; Assert.Equal(1, Lambdas.GetValue(expression2)); Expression <Func <Sample, bool> > expression3 = t => t.Test2.Test3.StringValue == "B"; Assert.Equal("B", Lambdas.GetValue(expression3)); var value = Guid.NewGuid(); Expression <Func <Sample, bool> > expression4 = t => t.GuidValue == value; Assert.Equal(value, Lambdas.GetValue(expression4)); Expression <Func <Sample, bool> > expression5 = t => 1 == t.Test2.IntValue; Assert.Equal(1, Lambdas.GetValue(expression5)); }
private object GetGuidValue(Guid id) { Expression <Func <Sample, bool> > expression = t => t.GuidValue == id; var value = Lambdas.GetValue(expression); return(value); }
public void TestGetValue_Instance() { var test = new Sample() { StringValue = "a", BoolValue = true, Test2 = new Sample2() { StringValue = "b", Test3 = new Sample3() { StringValue = "c" } } }; Expression <Func <string> > expression = () => test.StringValue; Assert.Equal("a", Lambdas.GetValue(expression)); Expression <Func <string> > expression2 = () => test.Test2.StringValue; Assert.Equal("b", Lambdas.GetValue(expression2)); Expression <Func <string> > expression3 = () => test.Test2.Test3.StringValue; Assert.Equal("c", Lambdas.GetValue(expression3)); Expression <Func <bool> > expression4 = () => test.BoolValue; Assert.True(Conv.ToBool(Lambdas.GetValue(expression4))); }
public void TestGetValue_NewGuid() { Expression <Func <Sample, bool> > expression = t => t.GuidValue == Guid.NewGuid(); var value = Lambdas.GetValue(expression); Assert.NotEqual(Guid.Empty, Conv.ToGuid(value)); }
public void Local_functions_parameters_are_called_by_reference() { var transformed = LocalFunctions.Transform(); var transformed2 = Lambdas.Transform(); Assert.AreEqual(transformed2, transformed); }
/// <summary> /// Or连接条件 /// </summary> /// <typeparam name="TEntity">实体类型</typeparam> /// <param name="conditions">查询条件</param> public void OrIfNotEmpty <TEntity>(params Expression <Func <TEntity, bool> >[] conditions) { if (conditions == null) { return; } foreach (var condition in conditions) { if (condition == null) { continue; } if (Lambdas.GetConditionCount(condition) > 1) { throw new InvalidOperationException(string.Format(LibraryResource.OnlyOnePredicate, condition)); } if (string.IsNullOrWhiteSpace(Lambdas.GetValue(condition).SafeString())) { continue; } var predicate = _expressionResolver.Resolve(condition); if (predicate == null) { continue; } Or(predicate); } }
public void TestGetValue_List() { var list = new List <string> { "a", "b" }; Expression <Func <Sample, bool> > expression = t => list.Contains(t.StringValue); Assert.Equal(list, Lambdas.GetValue(expression)); }
/// <summary> /// Add /// </summary> /// <param name="expression"></param> /// <param name="newValue"></param> /// <typeparam name="TObject"></typeparam> /// <typeparam name="TProperty"></typeparam> /// <typeparam name="TValue"></typeparam> public void Add <TObject, TProperty, TValue>(Expression <Func <TObject, TProperty> > expression, TValue newValue) where TObject : IDomainObject { var name = Lambdas.GetName(expression); var desc = Reflections.GetDescription(Lambdas.GetMember(expression)); var value = Lambdas.GetValue(expression); Add(name, desc, value.CastTo <TValue>(), newValue); }
/// <summary> /// 添加 /// </summary> /// <typeparam name="TObject">领域对象类型</typeparam> /// <typeparam name="TProperty">属性类型</typeparam> /// <typeparam name="TValue">值类型</typeparam> /// <param name="expression">属性表达式。范例:t => t.Name</param> /// <param name="obj">领域对象</param> /// <param name="newValue">新值。范例:newEntity.Name</param> public void Add <TObject, TProperty, TValue>(Expression <Func <TObject, TProperty> > expression, TObject obj, TValue newValue) where TObject : IDomainObject { var member = Lambdas.GetMemberExpression(expression); var name = Lambdas.GetMemberName(member); var desc = Reflection.Reflections.GetDisplayNameOrDescription(member.Member); var value = member.Member.GetPropertyValue(obj); Add(name, desc, Conv.To <TValue>(value), newValue); }
/// <summary> /// 设置连接条件 /// </summary> /// <typeparam name="TLeft">左表实体类型</typeparam> /// <typeparam name="TRight">右表实体类型</typeparam> /// <param name="expression">条件表达式</param> public void On <TLeft, TRight>(Expression <Func <TLeft, TRight, bool> > expression) where TLeft : class where TRight : class { if (expression == null) { throw new ArgumentNullException(nameof(expression)); } var expressions = Lambdas.GetGroupPredicates(expression); var items = expressions.Select(GetOnItems).ToList(); _params.LastOrDefault()?.On(items, _dialect); }
/// <summary> /// 添加描述 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <typeparam name="TProperty">属性类型</typeparam> /// <param name="expression">属性表达式,范例:t => t.Name</param> public void Add <T, TProperty>(Expression <Func <T, TProperty> > expression) { var member = Lambdas.GetMember(expression); var description = Reflection.Reflections.GetDisplayNameOrDescription(member); var value = member.GetPropertyValue(this); if (Reflection.Reflections.IsBool(member)) { value = Conv.ToBool(value).Description(); } Add(description, value); }
public void TestGetValue_Nullable() { Expression <Func <Sample, bool> > expression = t => t.NullableIntValue == 1; Assert.Equal(1, Lambdas.GetValue(expression)); expression = t => t.NullableDecimalValue == 1.5M; Assert.Equal(1.5M, Lambdas.GetValue(expression)); var sample = new Sample(); expression = t => t.BoolValue == sample.NullableBoolValue; Assert.Null(Lambdas.GetValue(expression)); }
/// <summary> /// 获取列 /// </summary> /// <param name="expression">表达式</param> /// <param name="right">是否取右侧操作数</param> private SqlItem GetColumn(Expression expression, bool right) { var type = _resolver.GetType(expression, right); var column = _resolver.GetColumn(expression, type, right); if (string.IsNullOrWhiteSpace(column)) { var name = _parameterManager.GenerateName(); _parameterManager.Add(name, Lambdas.GetValue(expression)); return(new SqlItem(name, raw: true)); } return(new SqlItem(GetColumn(type, column))); }
/// <summary> /// 设置查询条件 /// </summary> /// <typeparam name="TEntity">实体类型</typeparam> /// <param name="expression">查询条件表达式,如果参数值为空,则忽略该查询条件</param> public void WhereIfNotEmpty <TEntity>(Expression <Func <TEntity, bool> > expression) where TEntity : class { if (expression == null) { throw new ArgumentNullException(nameof(expression)); } if (Lambdas.GetConditionCount(expression) > 1) { throw new InvalidOperationException(string.Format(LibraryResource.OnlyOnePredicate, expression)); } if (string.IsNullOrWhiteSpace(Lambdas.GetValue(expression).SafeString())) { return; } Where(expression); }
public void TestGetValue_Operation() { Expression <Func <Sample, bool> > expression = t => t.Test2.IntValue != 1; Assert.Equal(1, Lambdas.GetValue(expression)); expression = t => t.Test2.IntValue > 1; Assert.Equal(1, Lambdas.GetValue(expression)); expression = t => t.Test2.IntValue < 1; Assert.Equal(1, Lambdas.GetValue(expression)); expression = t => t.Test2.IntValue >= 1; Assert.Equal(1, Lambdas.GetValue(expression)); expression = t => t.Test2.IntValue <= 1; Assert.Equal(1, Lambdas.GetValue(expression)); }
public void TestGetValue_Static() { Expression <Func <Sample, bool> > expression = t => t.StringValue == Sample.StaticString; Assert.Equal("TestStaticString", Lambdas.GetValue(expression)); expression = t => t.StringValue == Sample.StaticSample.StringValue; Assert.Equal("TestStaticSample", Lambdas.GetValue(expression)); expression = t => t.Test2.StringValue == Sample.StaticString; Assert.Equal("TestStaticString", Lambdas.GetValue(expression)); expression = t => t.Test2.StringValue == Sample.StaticSample.StringValue; Assert.Equal("TestStaticSample", Lambdas.GetValue(expression)); expression = t => t.Test2.StringValue.Contains(Sample.StaticSample.StringValue); Assert.Equal("TestStaticSample", Lambdas.GetValue(expression)); }
/// <summary> /// 获取查询条件表达式 /// </summary> /// <typeparam name="TEntity">实体类型</typeparam> /// <param name="predicate">查询条件,如果参数值为空,则忽略该查询条件,范例:t => t.Name == "",该查询条件被忽略。 /// 注意:一次仅能添加一个条件,范例:t => t.Name == "a" && t.Mobile == "123",不支持,将抛出异常</param> public static Expression <Func <TEntity, bool> > GetWhereIfNotEmptyExpression <TEntity>( Expression <Func <TEntity, bool> > predicate) where TEntity : class { if (predicate == null) { return(null); } if (Lambdas.GetConditionCount(predicate) > 1) { throw new InvalidOperationException(string.Format(LibraryResource.CanOnlyOneCondition, predicate)); } var value = predicate.Value(); if (string.IsNullOrWhiteSpace(value.SafeString())) { return(null); } return(predicate); }
/// <summary> /// Resets the collection by reevaluating the groupings. /// </summary> void Reset() { // find new and old items var nowItems = groups.Values.SelectMany(i => i); var newItems = Lambdas.Select(i => Lambdas[i]).Except(nowItems).ToList(); var oldItems = nowItems.Except(newItems).ToList(); // remove old items foreach (var item in oldItems) { RemoveItem(item); } // add new items foreach (var item in newItems) { AddItem(item); } }
public void TestGetValue_Method() { Expression <Func <Sample, bool> > expression = t => t.StringValue.Contains("A"); Assert.Equal("A", Lambdas.GetValue(expression)); expression = t => t.Test2.StringValue.Contains("B"); Assert.Equal("B", Lambdas.GetValue(expression)); expression = t => t.Test2.Test3.StringValue.StartsWith("C"); Assert.Equal("C", Lambdas.GetValue(expression)); var test = new Sample { Email = "a" }; expression = t => t.StringValue.Contains(test.Email); Assert.Equal("a", Lambdas.GetValue(expression)); }
/// <summary> /// 获取值 /// </summary> /// <param name="expression">表达式</param> public object GetValue(Expression expression) { if (expression == null) { return(null); } var result = Lambdas.GetValue(expression); if (result == null) { return(null); } var type = result.GetType(); if (type.IsEnum) { return(Enum.GetValue(type, result)); } return(result); }
protected override void OnLambdaCollectionReset() { // unsubscribe from all items foreach (var item in items) { UnsubscribeItem(item); } // reset all items items.Clear(); items.AddRange(Lambdas.Select(i => i.Value)); // subscribe to all items foreach (var item in items) { SubscribeItem(item); } OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); }
public void TestGetValue_Bool() { Expression <Func <Sample, bool> > expression = t => t.BoolValue; Assert.Equal("True", Lambdas.GetValue(expression).ToString()); expression = t => !t.BoolValue; Assert.Equal("False", Lambdas.GetValue(expression).ToString()); expression = t => t.Test2.BoolValue; Assert.Equal("True", Lambdas.GetValue(expression).ToString()); expression = t => !t.Test2.BoolValue; Assert.Equal("False", Lambdas.GetValue(expression).ToString()); expression = t => t.BoolValue == true; Assert.Equal("True", Lambdas.GetValue(expression).ToString()); expression = t => t.BoolValue == false; Assert.Equal("False", Lambdas.GetValue(expression).ToString()); }
private void MoveRock(CellType currentType, Point current, Point destination, CellType[,] newState) { newState.Set(current, CellType.Empty); Rocks.Remove(current); IsChanged = true; if (currentType.IsHoRock() && !Cell.At(destination.Down()).IsEmpty()) { newState.Set(destination, CellType.Lambda); Lambdas.Add(destination); HoRocks.Remove(current); } else { newState.Set(destination, currentType); Rocks.Add(destination); } if (Cell.At(destination.Down()).IsRobot()) { State = MapState.Killed; } }
static void Main(string[] args) { Console.WriteLine("Hello World!"); Tasker.FifthRun(); // Lambda Class Lambdas lambda = new Lambdas(); lambda.Run(); //Actions Class Actions.Listings(); // Tasker Class Tasker.FirstRun(); Task second = Tasker.SecondRun(); second.Wait(); Tasker.ThirdRun(); Console.WriteLine(Tasker.FourthRun().Result); }
public void TestGetValue_Enum() { var test1 = new Sample { NullableEnumValue = EnumSample.C }; Expression <Func <Sample, bool> > expression = test => test.EnumValue == EnumSample.D; Assert.Equal(EnumSample.D.Value(), Enum.GetValue <EnumSample>(Lambdas.GetValue(expression))); expression = test => test.EnumValue == test1.NullableEnumValue; Assert.Equal(EnumSample.C, Lambdas.GetValue(expression)); expression = test => test.NullableEnumValue == EnumSample.E; Assert.Equal(EnumSample.E, Lambdas.GetValue(expression)); expression = test => test.NullableEnumValue == test1.NullableEnumValue; Assert.Equal(EnumSample.C, Lambdas.GetValue(expression)); test1.NullableEnumValue = null; expression = test => test.NullableEnumValue == test1.NullableEnumValue; Assert.Null(Lambdas.GetValue(expression)); }
public void TestGetValue_Complex() { var test = new Sample() { StringValue = "a", Test2 = new Sample2() { StringValue = "b" } }; Expression <Func <Sample, bool> > expression = t => t.StringValue == test.StringValue; Assert.Equal("a", Lambdas.GetValue(expression)); Expression <Func <Sample, bool> > expression2 = t => t.StringValue == test.Test2.StringValue; Assert.Equal("b", Lambdas.GetValue(expression2)); Expression <Func <Sample, bool> > expression3 = t => t.StringValue.Contains(test.StringValue); Assert.Equal("a", Lambdas.GetValue(expression3)); Expression <Func <Sample, bool> > expression4 = t => t.StringValue.Contains(test.Test2.StringValue); Assert.Equal("b", Lambdas.GetValue(expression4)); }
public void TestGetValue_Object() { Expression <Func <Sample, object> > expression = t => t.StringValue == "A"; Assert.Equal("A", Lambdas.GetValue(expression)); }
private void MoveRobotTo(RobotCommand direction, Point destPos) { CellType destType = Cell.At(destPos); if (!destType.IsTraversible()) { throw new InvalidMoveException(RobotPosition, destPos); } if (!destType.IsEmpty()) { IsChanged = true; } if (destType.IsRock()) { if (direction == RobotCommand.Left && Cell.At(destPos.Left()).IsEmpty()) { Cell.Set(destPos.Left(), destType); Rocks.Remove(destPos); Rocks.Add(destPos.Left()); if (destType.IsHoRock()) { HoRocks.Remove(destPos); HoRocks.Add(destPos.Left()); } } else if (direction == RobotCommand.Right && Cell.At(destPos.Right()).IsEmpty()) { Cell.Set(destPos.Right(), destType); Rocks.Remove(destPos); Rocks.Add(destPos.Right()); if (destType.IsHoRock()) { HoRocks.Remove(destPos); HoRocks.Add(destPos.Right()); } } else { throw new InvalidMoveException(RobotPosition, destPos); } } if (destType.IsLambda()) { Lambdas.Remove(destPos); Score += 25; LambdasCollected++; } if (destType.IsOpenLift()) { State = MapState.Won; Score += LambdasCollected * 50 - 1; // minus one point for the final move } if (destType.IsTrampoline()) { Cell.Set(destPos, CellType.Empty); destPos = Trampolines[destPos]; // remove all trampolines that have the same target var remove = new List <Point>(); foreach (var trampoline in Trampolines) { if (trampoline.Value == destPos) { Cell.Set(trampoline.Key, CellType.Empty); remove.Add(trampoline.Key); } } foreach (Point point in remove) { Trampolines.Remove(point); } } if (destType.IsRazor()) { RazorCount++; Razors.Remove(destPos); } Cell.Set(RobotPosition, CellType.Empty); Cell.Set(destPos, CellType.Robot); RobotPosition = destPos; }