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);
        }
Example #2
0
        public void CreateActionDelegate()
        {
            Context         environment = new Context();
            Runner          function    = new Runner();
            FunctionWrapper wrapper     = new FunctionWrapper(function, environment);
            var             @delegate   = wrapper.CreateActionDelegate();

            @delegate.DynamicInvoke();
            Assert.IsTrue(function.WasInvoked);
        }