Ejemplo n.º 1
0
        public void TestFourArguments()
        {
            FuncAdapter adapater = new FuncAdapter();

            List <ParameterInfo> parameters = new List <ParameterInfo>()
            {
                new ParameterInfo()
                {
                    Name = "test1", DataType = DataType.Integer
                },
                new ParameterInfo()
                {
                    Name = "test2", DataType = DataType.Integer
                },
                new ParameterInfo()
                {
                    Name = "test3", DataType = DataType.Integer
                },
                new ParameterInfo()
                {
                    Name = "test4", DataType = DataType.Integer
                }
            };

            Func <int, int, int, int, double> wrappedFunction = (Func <int, int, int, int, double>)adapater.Wrap(parameters, dictionary => dictionary["test4"]);

            Assert.AreEqual(8.0, wrappedFunction(2, 4, 6, 8));
        }
Ejemplo n.º 2
0
        public void TestFuncAdapterWrapAndGC()
        {
            FuncAdapter adapter = new FuncAdapter();

            List <ParameterInfo> parameters = new List <ParameterInfo>()
            {
                new ParameterInfo()
                {
                    Name = "test1", DataType = DataType.Integer
                },
                new ParameterInfo()
                {
                    Name = "test2", DataType = DataType.FloatingPoint
                }
            };

            Func <Dictionary <string, double>, double> function = (dictionary) => dictionary["test1"] + dictionary["test2"];

            Func <int, double, double> wrappedFunction = (Func <int, double, double>)adapter.Wrap(parameters, function);

            adapter = null;
            GC.Collect();

            Assert.AreEqual(3.0, wrappedFunction(1, 2.0));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Build the formula defined. This will create a func delegate matching with the parameters
        /// and the return type specified.
        /// </summary>
        /// <returns>The func delegate for the defined formula.</returns>
        public Delegate Build()
        {
            if (!resultDataType.HasValue)
            {
                throw new Exception("Please define a result data type for the formula.");
            }

            Func <IDictionary <string, double>, double> formula = engine.Build(formulaText, constants);

            FuncAdapter adapter = new FuncAdapter();

            return(adapter.Wrap(parameters, variables =>
            {
                if (!caseSensitive)
                {
                    variables = EngineUtil.ConvertVariableNamesToLowerCase(variables);
                }

                engine.VerifyVariableNames(variables);

                // Add the reserved variables to the dictionary
                foreach (ConstantInfo constant in engine.ConstantRegistry)
                {
                    variables.Add(constant.ConstantName, constant.Value);
                }

                return formula(variables);
            }));
        }
Ejemplo n.º 4
0
        public Delegate Build()
        {
            if (!resultDataType.HasValue)
            {
                throw new Exception("Please define a result data type for the formula.");
            }

            Func <Dictionary <string, double>, double> formula = engine.Build(formulaText);

            FuncAdapter adapter = new FuncAdapter();

            return(adapter.Wrap(parameters, variables => formula(variables)));
        }