Beispiel #1
0
        public void TestComplexExpression()
        {
            var newGenerator = new ConstructorExpressionGenerator(ConstructorSelectors.MostSpecific);
            int numFoos = 0;
            var fooGenerator = new LambdaExpressionGenerator(() => { numFoos++; return new ServiceFoo(); });
            var singleBar = new ServiceBar(new ServiceFoo());
            var barGenerator = new ConstantExpressionGenerator(singleBar);

            newGenerator.DependencyExpressionGenerating += (sender, args) =>
                {
                    IExpressionGenerator generator;
                    if (args.DependencyType == typeof(ServiceFoo))
                        generator = fooGenerator;
                    else if (args.DependencyType == typeof(ServiceBar))
                        generator = barGenerator;
                    else
                        generator = (IExpressionGenerator)sender;

                    args.Expression = generator.GenerateExpression(args.DependencyType);
                };

            var expression = newGenerator.GenerateExpression(typeof(Root));

            var func = Expression.Lambda<Func<Root>>(expression).Compile();

            var r = func();

            Assert.IsNotNull(r);
            Assert.IsNotNull(r.Bar);
            Assert.IsNotNull(r.Foo);
            Assert.Greater(numFoos, 0);
            Assert.AreSame(r.Bar, singleBar);
            Assert.AreSame(r.Bar.Foo, singleBar.Foo);
            Assert.AreNotSame(r.Foo, r.Bar.Foo);
            Assert.AreNotSame(r.Foo, singleBar.Foo);
        }
Beispiel #2
0
        public void ShouldCreateExpressionWithSimpleLambda()
        {
            var generator = new LambdaExpressionGenerator(() => new Foo());
            var expression = generator.GenerateExpression(typeof(Foo));

            Assert.NotNull(expression);
            var unary = expression as UnaryExpression;
            Assert.IsNotNull(unary);
            Assert.AreEqual(ExpressionType.Convert, unary.NodeType);
        }
Beispiel #3
0
        public void ShouldCreateCompilableExpression()
        {
            var generator = new LambdaExpressionGenerator(() => new Foo());
            var expression = generator.GenerateExpression(typeof(Foo));
            var func = Expression.Lambda<Func<Foo>>(expression).Compile();

            var o = func();
            Assert.IsNotNull(o);
        }