Пример #1
0
        public void AddFunctionAsCalculatorEventHandler()
        {
            Calculator calculator = new Calculator();
            Listener listener = new Listener();
            ObjectUtilities.AddHandler(calculator, "MyEvent", listener, null);
            calculator.Add(1, 2);

            Assert.AreEqual(1, listener.X);
            Assert.AreEqual(2, listener.Y);
        }
Пример #2
0
        public void CallNativeObjectMethodWithArguments()
        {
            Calculator calculator = new Calculator();
            BindingEnvironment environment = new BindingEnvironment();
            environment.SetValue("calculator", calculator);
            CallExpression expression = new CallExpression(new AttributeExpression(new NameExpression("calculator"), "Add"), new IExpression[] { new ConstantExpression(1), new ConstantExpression(2) });

            var result = expression.Evaluate(environment);

            Assert.IsNotNull(result);
            Assert.AreEqual(calculator.Add(1, 2), result);
        }
Пример #3
0
        public void AddEventHandlerToCalculator()
        {
            Listener listener = new Listener();
            Calculator calculator = new Calculator();
            FunctionWrapper<int, int, int, Action<int, int>> wrapper = new FunctionWrapper<int, int, int, Action<int, int>>(listener, null);
            typeof(Calculator).GetEvent("MyEvent").AddEventHandler(calculator, wrapper.CreateActionDelegate());
            calculator.Add(1, 2);

            Assert.AreEqual(1, listener.X);
            Assert.AreEqual(2, listener.Y);

            MethodInfo invoke = typeof(Calculator).GetEvent("MyEvent").EventHandlerType.GetMethod("Invoke");
            MethodInfo add = typeof(Calculator).GetEvent("MyEvent").GetAddMethod();
            Assert.IsNotNull(add);
            Assert.AreEqual(1, add.GetParameters().Count());
            Assert.IsNotNull(invoke);
            var parameters = invoke.GetParameters();
            var returnparameter = invoke.ReturnParameter;
            Assert.IsNotNull(returnparameter);
            Assert.AreEqual("System.Void", returnparameter.ParameterType.FullName);

            Assert.AreEqual(typeof(MyEvent), typeof(Person).GetEvent("NameEvent").EventHandlerType);
        }