Example #1
0
        public void ShouldBeAbleToAddMethodsThatHaveAccessToTheMetaObjectInstanceItself()
        {
            var wasMethodCalled     = new ManualResetEvent(false);
            var metaObjectsAccessed = new List <MetaObject>();

            // Notice how we can access the metaObject instance as part of the method body
            Action <MetaObject> methodBody = self =>
            {
                metaObjectsAccessed.Add(self);
                wasMethodCalled.Set();
            };

            var foo = new MetaObject();

            foo.AddMethod("DoSomething", methodBody);

            // Match the type signature
            Assert.True(foo.LooksLike <ISampleDuckInterface>());

            var duck = foo.CreateDuck <ISampleDuckInterface>();

            duck.DoSomething();

            Assert.True(wasMethodCalled.WaitOne(TimeSpan.FromMilliseconds(100)));
            Assert.Single(metaObjectsAccessed, metaObject => ReferenceEquals(foo, metaObjectsAccessed.First()));
        }
Example #2
0
        public void ShouldBeAbleToDetermineIfAMetaObjectCanFulfillAnInterfaceContract()
        {
            var wasMethodCalled = new ManualResetEvent(false);

            Action methodBody = () => wasMethodCalled.Set();

            // Add the new method that fulfills the ISampleDuckInterface contract
            var foo = new MetaObject();

            foo.AddMethod("DoSomething", methodBody);

            Assert.True(foo.LooksLike <ISampleDuckInterface>());
        }
Example #3
0
        public void ShouldBeAbleToPassTheDynamicObjectInstanceToAddedMethodsAsAnOptionalParameter()
        {
            var wasMethodCalled = new ManualResetEvent(false);

            // Use the Action delegate as the method body
            Action <DynamicObject> methodBody = _ => wasMethodCalled.Set();

            var metaObject = new MetaObject();

            metaObject.AddMethod("DoSomething", methodBody);

            dynamic foo = metaObject;

            foo.DoSomething();

            Assert.True(wasMethodCalled.WaitOne(TimeSpan.FromMilliseconds(100)));
        }
Example #4
0
        public void ShouldBeAbleToCreateDuckTypesFromTheMetaObjectItself()
        {
            var wasMethodCalled = new ManualResetEvent(false);
            var metaObject      = new MetaObject();

            metaObject.AddMethod <Func <int> >("get_Value", () =>
            {
                wasMethodCalled.Set();
                return(42);
            });

            var duck = metaObject.CreateDuck <ISampleInterfaceWithProperties>();

            Assert.Equal(42, duck.Value);

            Assert.True(wasMethodCalled.WaitOne(TimeSpan.FromMilliseconds(100)));
        }
Example #5
0
        public void ShouldBeAbleToAddMethodWithoutUsingDynamicKeyword()
        {
            var wasMethodCalled = new ManualResetEvent(false);

            // Use the Action delegate as the method body
            Action methodBody = () => wasMethodCalled.Set();

            var metaObject = new MetaObject();

            metaObject.AddMethod("DoSomething", methodBody);

            dynamic foo = metaObject;

            foo.DoSomething();

            Assert.True(wasMethodCalled.WaitOne());
        }