private static CacheSet <Type, Delegate> NewOpFunc(BinaryDelegate @delegate, Type retType) { Func <Delegate> cacheMethodBuilder(Type operandType) { return(() => { var const_delegate = Expression.Constant(@delegate); var method = GetOpMethod(operandType, operandType, retType); var exp = Expression.Call(method, const_delegate); // Expression.Lambda<Func<Func<operandType, operandType, retType>>>(exp).Compile()() var lambdaGenericType = typeof(Func <>).MakeGenericType(typeof(Func <, ,>).MakeGenericType(operandType, operandType, retType)); var lambdaMethod = typeof(Expression) .GetMethodViaQualifiedName("System.Linq.Expressions.Expression`1[TDelegate] Lambda[TDelegate](System.Linq.Expressions.Expression, System.Linq.Expressions.ParameterExpression[])") .MakeGenericMethod(lambdaGenericType); return (lambdaMethod.Invoke(null, new object[] { exp, new ParameterExpression[0] }) as LambdaExpression).Compile().DynamicInvoke() as Delegate; }); } var container = new CacheSet <Type, Delegate> { CacheMethodBuilder = cacheMethodBuilder }; return(container); }
private static Func <TOp1, TOp2, TRet> Op <TOp1, TOp2, TRet>(BinaryDelegate @delegate) { var leftExp = Expression.Parameter(typeof(TOp1), "left"); var rightExp = Expression.Parameter(typeof(TOp2), "right"); var lambda = Expression.Lambda <Func <TOp1, TOp2, TRet> >(@delegate(leftExp, rightExp), leftExp, rightExp); return(lambda.Compile()); }
/// <summary> /// Performs a binary operation on the specified values and returns the result. /// </summary> /// <typeparam name="TValue">The value type.</typeparam> /// <typeparam name="TUnit">The unit type.</typeparam> /// <param name="lhs">The left-hand operator.</param> /// <param name="rhs">The right-hand operator.</param> /// <param name="func">The binary function.</param> /// <returns>The resulting value of the binary operation, if successful; otherwise, <c>default</c>.</returns> public static UnitValue <TValue, TUnit> Binary <TValue, TUnit> ( this UnitValue <TValue, TUnit> lhs, UnitValue <TValue, TUnit> rhs, BinaryDelegate <TValue, TUnit> func ) where TValue : struct, IEquatable <TValue> where TUnit : struct => Unit.New( func( lhs, rhs ) .Value, lhs.Unit ) ?? default;
public Binary(int precendence, RightLeft associativity, BinaryDelegate evaluate) : base(precendence) { this.Associativity = associativity; this.Evaluate = evaluate; }