Beispiel #1
0
        public void CanCreateADynamicAdder()
        {
            Func <object[], object> addBody = delegate(object[] args)
            {
                var a = (int)args[0];
                var b = (int)args[1];
                return(a + b);
            };

            // Map LinFu's DynamicObject to an ICanAdd interface
            var linfuDynamicObject = new DynamicObject(new object());
            var returnType         = typeof(int);
            var parameterTypes     = new[] { typeof(int), typeof(int) };

            linfuDynamicObject.AddMethod("Add", addBody, returnType, parameterTypes);

            // If it looks like a duck...
            Assert.IsTrue(linfuDynamicObject.LooksLike <ICanAdd>());

            // ...then it must be a duck, right?
            var somethingThatCanAdd = new SomethingThatAdds(linfuDynamicObject.CreateDuck <ICanAdd>());

            somethingThatCanAdd.FirstNumber  = 10;
            somethingThatCanAdd.SecondNumber = 20;
            Assert.AreEqual(somethingThatCanAdd.AddNumbers(), 30);
        }
        public void ShouldBeAbleToAddNewMethod()
        {
            IntegerOperation addBody = delegate(int a, int b) { return a + b; };
            DynamicObject dynamic = new DynamicObject(new object());
            dynamic.AddMethod("Add", addBody);

            int result = (int) dynamic.Methods["Add"](1, 1);
            Assert.AreEqual(2, result);
        }
Beispiel #3
0
        public void ShouldBeAbleToAddNewMethod()
        {
            IntegerOperation addBody = delegate(int a, int b) { return(a + b); };
            var dynamic = new DynamicObject(new object());

            dynamic.AddMethod("Add", addBody);

            var result = (int)dynamic.Methods["Add"](1, 1);

            Assert.AreEqual(2, result);
        }
Beispiel #4
0
        public void ShouldBeAbleToUseCLRDynamicObjectsAsExpandoObjects()
        {
            Func <object[], object> body = args => 42;

            var dynamicObject = new DynamicObject(new object());

            dynamicObject.AddMethod("GetFoo", body, typeof(int));

            dynamic expando = dynamicObject.AsExpandoObject();

            int result = expando.GetFoo();

            Assert.AreEqual(42, result);
        }
        public void ShouldBeAbleToCallCovariantPropertySetterOnExpandoObject()
        {
            var expectedValue = 12345;
            Func<object[], object> setterBody = args =>
            {
                var value = args[0];
                Assert.AreEqual(value, expectedValue);
                return null;
            };

            var target = new DynamicObject();
            target.AddMethod("set_TargetProperty", setterBody, typeof(void), typeof(object));

            dynamic expando = new ExpandoObject(target);
            expando.TargetProperty = expectedValue;
        }
        public void ShouldBeAbleToCallCovariantPropertySetterOnExpandoObject()
        {
            var expectedValue = 12345;
            Func <object[], object> setterBody = args =>
            {
                var value = args[0];
                Assert.AreEqual(value, expectedValue);
                return(null);
            };

            var target = new DynamicObject();

            target.AddMethod("set_TargetProperty", setterBody, typeof(void), typeof(object));

            dynamic expando = new ExpandoObject(target);

            expando.TargetProperty = expectedValue;
        }
Beispiel #7
0
        public void ShouldBeAbleToAddMethodsUsingRuntimeAnonymousDelegates()
        {
            Func <object[], object> addBody = delegate(object[] args)
            {
                var a = (int)args[0];
                var b = (int)args[1];
                return(a + b);
            };

            var dynamic    = new DynamicObject(new object());
            var returnType = typeof(int);

            Type[] parameterTypes = { typeof(int), typeof(int) };
            dynamic.AddMethod("Add", addBody, returnType, parameterTypes);

            var result = (int)dynamic.Methods["Add"](1, 1);

            Assert.AreEqual(2, result);
        }
        public void ShouldBeAbleToAddMethodsUsingRuntimeAnonymousDelegates()
        {
            CustomDelegate addBody = delegate(object[] args)
                                         {
                                             int a = (int)args[0];
                                             int b = (int) args[1];
                                             return a + b;
                                         };

            DynamicObject dynamic = new DynamicObject(new object());
            Type returnType = typeof (int);
            Type[] parameterTypes = new Type[] {typeof (int), typeof (int)};
            dynamic.AddMethod("Add", addBody, returnType, parameterTypes);

            int result = (int)dynamic.Methods["Add"](1, 1);
            Assert.AreEqual(2, result);
        }