Beispiel #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()));
        }
Beispiel #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>());
        }