コード例 #1
0
        public static void Main(string[] args)
        {
            var ps = new
            {
                PaintWheels  = ReturnVoid.Arguments <IWheels>(wheels => wheels.Foo()),
                PaintChassie = ReturnVoid.Arguments <IChassie>(chassie => chassie.Foo()),
                ChromeEngine = ReturnVoid.Arguments <IEngine>(engine => engine.Foo())
            };
            var paintShop = ps.ActLike <IPaintShop>();

            var fullCar = new
            {
                Foo = ReturnVoid.Arguments(() => Console.WriteLine("Hello World!"))
            };
            var car = fullCar.ActLike <IEngine>(typeof(IChassie), typeof(IWheels));

            //each of these 3 calls prints "Hello World!" to the console
            paintShop.PaintWheels((IWheels)car);     //need to tell the compiler to cast your car to type IWheels because var car is of type IEngine
            paintShop.PaintChassie(car as IChassie); //need to tell the compiler to cast your car to type IChassie because var car is of type IEngine
            paintShop.ChromeEngine(car);             //works sans cast because var car is of type IEngine

            //each of these 3 calls prints "Hello World!" to the console, too
            dynamic dynamicCar = car;

            paintShop.PaintWheels(dynamicCar);            //by using dynamic you disable the compile time
            paintShop.PaintChassie(dynamicCar);           //type checking and the compiler "trusts you" on the typing
            paintShop.ChromeEngine(dynamicCar);           //since Impromptu wrapped your object and implemented the interfaces for you, there is no runtime exception

            Console.ReadLine();
        }
コード例 #2
0
        public static void ImpromptuBuilder()
        {
            dynamic New = Builder.New();

            dynamic person = New.Person(
                FirstName: "Robert",
                LastName: "Paulson"
                )
                             .Age(42)
                             .Sing(ReturnVoid.Arguments(() => Console.WriteLine("TROOOOOOOOGDOOOOOOOOOOOR!")))
                             .Greet(Return <string> .ThisAndArguments(
                                        (@this, greeting) => string.Format("{0} {1} {2}", greeting, @this.FirstName, @this.LastName)));

            Console.WriteLine(person.FirstName);
            Console.WriteLine(person.LastName);
            Console.WriteLine(person.Age);
            Console.WriteLine(person.Greet("His name is"));
            person.Sing();

            // http://code.google.com/p/impromptu-interface/wiki/UsageBuilder

            #region Duck Typing

            //IPerson duckTypedPerson = Impromptu.ActLike<IPerson>(person);
            //duckTypedPerson.LastName = "Martin";

            //IGreet duckTypedGreeter = Impromptu.ActLike<IGreet>(person);
            //Console.WriteLine(duckTypedGreeter.Greet("Clean coder:"));

            #endregion
        }
コード例 #3
0
        public void TestAnonInterface()
        {
            var tInterface = Impromptu.Create <Get, ICollection>(new
            {
                CopyArray      = ReturnVoid.Arguments <Array, int>((ar, i) => Enumerable.Range(1, 10)),
                Count          = 10,
                IsSynchronized = false,
                SyncRoot       = this,
                GetEnumerator  = Return <IEnumerator> .Arguments(() => Enumerable.Range(1, 10).GetEnumerator())
            });

            Assert.AreEqual(10, tInterface.Count);
            Assert.AreEqual(false, tInterface.IsSynchronized);
            Assert.AreEqual(this, tInterface.SyncRoot);
            Assert.AreEqual(true, tInterface.GetEnumerator().MoveNext());
        }
コード例 #4
0
ファイル: GilesMSpecRunListener.cs プロジェクト: nosami/Giles
        public static object GetAnonymousListener(SessionResults sessionResults, List <TestResult> testResults, ResultFormatterFactory resultFormatterFactory)
        {
            return(new
            {
                OnAssemblyStart = ReturnVoid.Arguments <dynamic>(assembly => { }),

                OnAssemblyEnd = ReturnVoid.Arguments <dynamic>(assembly => { }),

                OnRunStart = ReturnVoid.Arguments(() => { }),

                OnRunEnd = ReturnVoid.Arguments(() => testResults.ForEach(x => sessionResults.TestResults.Add(x))),

                OnContextStart = ReturnVoid.Arguments <dynamic>(context =>
                {
                    string r = string.Format("\n{0}", context.FullName);
                    sessionResults.Messages.Add(r);
                }),

                OnContextEnd = ReturnVoid.Arguments <dynamic>(context => { }),

                OnSpecificationStart = ReturnVoid.Arguments <Object>(specification => { }),

                OnSpecificationEnd = ReturnVoid.Arguments <dynamic, dynamic>((specification, result) =>
                {
                    var formatter = ResultFormatterFactory.GetResultFormatterFor(result: result.Status.ToString());

                    string formatResult = formatter.FormatResult(specification, result);
                    sessionResults.Messages.Add(formatResult);

                    var testResult =
                        new TestResult {
                        Name = specification.Name, TestRunner = "MSPEC"
                    };

                    ProcessTestResult((object)result, testResult, testResults);
                }),

                OnFatalError = ReturnVoid.Arguments <dynamic>(exception => sessionResults.Messages.Add("Fatal error: " + exception)),

                sessionResults,

                testResults,

                resultFormatterFactory
            });
        }
コード例 #5
0
        private void Stuff()
        {
            var pfact = TypeProjector.Create()
                        .SelectFrom <IShip>(t => t.Property(s => s.Fuel, 0.0F),
                                            t => t.Method(s => s.FullStop(), Return <int> .ThisAndArguments(@this => 0)))
                        .Construct(builder => builder.Prototype(
                                       Nonsense: "Lugrgar",
                                       Transform: Return <string> .ThisAndArguments <string>((@this, m) => @this.Xform(m)),
                                       DoStuff: Return <int> .ThisAndArguments <string, Ship>((@this, name, s) => { return(0); }),
                                       DamageIt: ReturnVoid.ThisAndArguments <Ship, Laser, Captain>(
                                           (@this, aShip, laser, captain) =>
            {
                aShip.Captain = captain;
                aShip.HullIntegrity[Sides.Above] -= laser.PowerLevel;
                @this.SplatOnScreen(aShip, laser);
            }),
                                       XFactorIt: Return <bool> .ThisAndArguments(@this => false)))
                        .EmbedInstanceOf <IShip>(MemberNames.MyShip)
                        .EmbedAndExpose <Captain>(MemberNames.MyCaptain,
                                                  t => t.Properties(
                                                      c => c.AggressionLevel,
                                                      c => c.Braveness,
                                                      c => c.DeceptionLevel),
                                                  t => t.Methods(
                                                      c => c.AbandonShip()
                                                      ))
                        .Initializer(
                ReturnVoid.ThisAndArguments(
                    @this =>
            {
                @this.InitialData = Tuple.Create(0, false, MemberNames.MyCaptain, string.Empty,
                                                 new Laser());
            }))
                        .Declare()
                        .CreateFactoryDressedAs <IXFactor>();

            Catalog.Services.Register(_ => pfact());



            var  ixf  = Catalog.Factory.Resolve <IXFactor>();
            bool good = ixf.XFactorIt();
        }
コード例 #6
0
        public void DynamicAnnonymousWrapper()
        {
            var tData = new Dictionary <int, string> {
                { 1, "test" }
            };
            dynamic tDyn = new Get(new
            {
                Test1          = 1,
                Test2          = "2",
                IsGreaterThan5 = Return <bool> .Arguments <int>(it => it > 5),
                ClearData      = ReturnVoid.Arguments(() => tData.Clear())
            });

            Assert.AreEqual(1, tDyn.Test1);
            Assert.AreEqual("2", tDyn.Test2);
            Assert.AreEqual(true, tDyn.IsGreaterThan5(6));
            Assert.AreEqual(false, tDyn.IsGreaterThan5(4));

            Assert.AreEqual(1, tData.Count);
            tDyn.ClearData();
            Assert.AreEqual(0, tData.Count);
        }
コード例 #7
0
ファイル: DynamicObjects.cs プロジェクト: rioka/dynamitey
        public void TestAnonInterface()
        {
            dynamic tInterface = new DynamicObjects.Get(new
            {
                CopyArray =
                    ReturnVoid.Arguments <Array, int>(
                        (ar, i) => Enumerable.Range(1, 10)),
                Count          = 10,
                IsSynchronized = false,
                SyncRoot       = this,
                GetEnumerator  =
                    Return <IEnumerator> .Arguments(
                        () => Enumerable.Range(1, 10).GetEnumerator())
            });

            Dynamic.ApplyEquivalentType(tInterface, typeof(ICollection), typeof(IEnumerable));

            Assert.AreEqual(10, tInterface.Count);
            Assert.AreEqual(false, tInterface.IsSynchronized);
            Assert.AreEqual(this, tInterface.SyncRoot);
            Assert.That((object)tInterface.GetEnumerator(), Is.InstanceOf <IEnumerator>());
        }
コード例 #8
0
        public static void Main(string[] args)
        {
            var ps = new
            {
                PaintWheels  = ReturnVoid.Arguments <IWheels>(wheels => wheels.Foo()),
                PaintChassie = ReturnVoid.Arguments <IChassie>(chassie => chassie.Foo()),
                ChromeEngine = ReturnVoid.Arguments <IEngine>(engine => engine.Foo())
            };
            var paintShop = ps.ActLike <IPaintShop>();

            var fullCar = new
            {
                Foo = ReturnVoid.Arguments(() => Console.WriteLine("Hello World!"))
            };
            var car = fullCar.ActLike <IEngine>(typeof(IChassie), typeof(IWheels));


            paintShop.PaintWheels((IWheels)car);            //need to tell the compiler to cast your car to type IWheels because var car is of type IEngine
            paintShop.PaintChassie(car as IChassie);        //need to tell the compiler to cast your car to type IChassie because var car is of type IEngine
            paintShop.ChromeEngine(car);

            Console.ReadLine();
        }
コード例 #9
0
        public static void Main(string[] args)
        {
            var ps = new
            {
                PaintWheels  = ReturnVoid.Arguments <IWheels>(wheels => wheels.Foo()),
                PaintChassie = ReturnVoid.Arguments <IChassie>(chassie => chassie.Foo()),
                ChromeEngine = ReturnVoid.Arguments <IEngine>(engine => engine.Foo()),
                PaintCar     = ReturnVoid.Arguments <ICar>(car1 => ((IEngine)car1).Foo())
            };
            var paintShop = ps.ActLike <IPaintShop>();

            var fullCar = new
            {
                Foo = ReturnVoid.Arguments(() => Console.WriteLine("Hello World!"))
            };
            var car   = fullCar.ActLike <ICar>();
            var wheel = car.ActLike <IWheels>();

            paintShop.PaintWheels(wheel);       //Writes "Hello World!" to console
            paintShop.PaintChassie(car);        //Writes "Hello World!" to console
            paintShop.PaintCar(car);            //Writes "Hello World!" to console

            Console.ReadLine();
        }