Exemple #1
0
        static void Main(string[] args)
        {
            var ns = new FluentCodeCompileUnit().Namespace("Sample5");

            ns.Class(MemberAttributes.Public | MemberAttributes.Static, "Program")
            .Method(MemberAttributes.Public | MemberAttributes.Static, "ForArrayTest")
            .CallStatic(typeof(Console), "WriteLine", Expr.Primitive("ForArrayTest:"))
            .Declare(typeof(string[]), "sArr", Expr.NewArray(typeof(string), Expr.Primitive(2)))
            .Set(Expr.ArrayIndex(Expr.Var("sArr"), Expr.Primitive(0)), Expr.Primitive("Hallo1"))
            .Set(Expr.ArrayIndex(Expr.Var("sArr"), Expr.Primitive(1)), Expr.Primitive("Hallo2"))

            .ForArray("i", Expr.Var("sArr"))
            .CallStatic(typeof(Console), "WriteLine", Expr.ArrayIndex(Expr.Var("sArr"), Expr.Var("i")))
            .EndFor

            .EndMethod

            .Method(MemberAttributes.Public | MemberAttributes.Static, "ForeachTest")
            .CallStatic(typeof(Console), "WriteLine", Expr.Primitive("ForeachTest:"))
            .Declare(typeof(string[]), "sArr", Expr.NewArray(typeof(string), Expr.Primitive(2)))
            .Set(Expr.ArrayIndex(Expr.Var("sArr"), Expr.Primitive(0)), Expr.Primitive("Hallo1"))
            .Set(Expr.ArrayIndex(Expr.Var("sArr"), Expr.Primitive(1)), Expr.Primitive("Hallo2"))

            .ForeachEmu(typeof(string), "s", Expr.Var("sArr"))
            .CallStatic(typeof(Console), "WriteLine", Expr.Var("s"))
            .EndForeach
            .EndMethod

            .Method(MemberAttributes.Public | MemberAttributes.Static, "WhileTest")
            .CallStatic(typeof(Console), "WriteLine", Expr.Primitive("WhileTest:"))
            .Declare(typeof(int), "a", Expr.Primitive(1))
            .WhileEmu(Expr.Op(Expr.Var("a"), CodeBinaryOperatorType.LessThanOrEqual, Expr.Primitive(10)))
            .CallStatic(typeof(Console), "WriteLine", Expr.CallStatic(typeof(string), "Format", Expr.Primitive("Value: {0}"), Expr.Var("a")))
            .Set(Expr.Var("a"), Expr.Op(Expr.Var("a"), CodeBinaryOperatorType.Add, Expr.Primitive(1)))
            .EndWhile
            .EndMethod
            .EndFluent();

            CodeCompileUnit compileUnit = ns.EndNamespace.EndFluent();

            Console.WriteLine(Helper.CodeDomHelper.GenerateCodeAsString(compileUnit, new CSharpCodeProvider()));

            Assembly assembly = Helper.CodeDomHelper.CompileInMemory(compileUnit);

            assembly.GetType("Sample5.Program").GetMethod("ForArrayTest").Invoke(null, new object[] { });
            assembly.GetType("Sample5.Program").GetMethod("ForeachTest").Invoke(null, new object[] { });
            assembly.GetType("Sample5.Program").GetMethod("WhileTest").Invoke(null, new object[] { });
        }
Exemple #2
0
        public static void LoopTest()
        {
            var ns = new FluentCodeCompileUnit().Namespace("TestNamespace");

            ns.Class("AClass")
            .Method(MemberAttributes.Public, "ForArrayTest")
            .CallStatic(typeof(Console), "WriteLine", Expr.Primitive("ForArrayTest:"))
            .Declare(typeof(string[]), "sArr", Expr.NewArray(typeof(string), Expr.Primitive(2)))
            .Set(Expr.ArrayIndex(Expr.Var("sArr"), Expr.Primitive(0)), Expr.Primitive("Hallo1"))
            .Set(Expr.ArrayIndex(Expr.Var("sArr"), Expr.Primitive(1)), Expr.Primitive("Hallo2"))

            .ForArray("i", Expr.Var("sArr"))
            .CallStatic(typeof(Console), "WriteLine", Expr.ArrayIndex(Expr.Var("sArr"), Expr.Var("i")))
            .EndFor
            .EndMethod

            .Method(MemberAttributes.Public, "ForeachTest")
            .CallStatic(typeof(Console), "WriteLine", Expr.Primitive("ForeachTest:"))
            .Declare(typeof(string[]), "sArr", Expr.NewArray(typeof(string), Expr.Primitive(2)))
            .Set(Expr.ArrayIndex(Expr.Var("sArr"), Expr.Primitive(0)), Expr.Primitive("Hallo1"))
            .Set(Expr.ArrayIndex(Expr.Var("sArr"), Expr.Primitive(1)), Expr.Primitive("Hallo2"))

            .ForeachEmu(typeof(string), "s", Expr.Var("sArr"))
            .CallStatic(typeof(Console), "WriteLine", Expr.Var("s"))
            .EndForeach
            .EndMethod

            .Method(MemberAttributes.Public, "WhileTest")
            .CallStatic(typeof(Console), "WriteLine", Expr.Primitive("WhileTest:"))
            .Declare(typeof(int), "a", Expr.Primitive(1))
            .WhileEmu(Expr.Op(Expr.Var("a"), CodeBinaryOperatorType.LessThanOrEqual, Expr.Primitive(10)))
            .CallStatic(typeof(Console), "WriteLine", Expr.CallStatic(typeof(string), "Format", Expr.Primitive("Value: {0}"), Expr.Var("a")))
            .Set(Expr.Var("a"), Expr.Op(Expr.Var("a"), CodeBinaryOperatorType.Add, Expr.Primitive(1)))
            .EndWhile
            .EndMethod
            .EndFluent();

            CodeCompileUnit compileUnit = ns.EndNamespace.EndFluent();
            Assembly        assembly    = TestGenerated(compileUnit);

            object instance = GetClassInstance("TestNamespace.AClass", assembly);

            instance.GetType().GetMethod("ForArrayTest").Invoke(instance, new object[] { });
            instance.GetType().GetMethod("ForeachTest").Invoke(instance, new object[] { });
            instance.GetType().GetMethod("WhileTest").Invoke(instance, new object[] { });
        }
Exemple #3
0
        public static void DefintionTest()
        {
            var             fluent      = new FluentCodeCompileUnit();
            CodeCompileUnit compileUnit = fluent
                                          .Namespace("Test.FluentCodeDom.Test")
                                          .Import("System")
                                          .Import("System.Text")

                                          .Class(MemberAttributes.Public, "StringModifier")
                                          .Constructor()
                                          .EndConstructor

                                          .Field(typeof(int), "_fld").EndField
                                          .Field(MemberAttributes.Private, typeof(int), "_field1").EndField
                                          .Field(MemberAttributes.Private, typeof(DateTime), "_now").EndField
                                          .Field(typeof(DateTime), "_now2").EndField

                                          .Field(typeof(int), "_intValue").EndField
                                          .Property(MemberAttributes.Public, typeof(int), "IntValue")
                                          .Get
                                          .Return(Expr.Var("_intValue"))
                                          .EndGet
                                          .Set
                                          .Set(Expr.Var("_intValue"), Expr.Value())
                                          .EndSet
                                          .EndProperty

                                          .Method(MemberAttributes.Public, "OutMethod").Parameter(typeof(int), "outParam")
                                          .Set(Expr.Arg("outParam"), Expr.Primitive(55))
                                          .EndMethod

                                          .Method(MemberAttributes.Public, "HelloWorld")
                                          .CallStatic(typeof(Console), "WriteLine", Expr.Primitive("Hallo Welt"))
                                          .CallStatic(typeof(Console), "ReadLine")
                                          .EndMethod

                                          .Method("StringSplit").Parameter(typeof(string), "s").Parameter(typeof(char), "seperationChar").ReturnType(typeof(string))
                                          .Declare(typeof(string[]), "stringArr", Expr.CallMember(Expr.Arg("s"), "Split", Expr.Arg("seperationChar")))

                                          .If(Expr.Op(Expr.Primitive(5), CodeBinaryOperatorType.ValueEquality, Expr.Primitive(10)))
                                          .Declare(typeof(int), "abc")
                                          .Set(Expr.Var("abc"), Expr.Primitive(5))
                                          .Else
                                          .If(Expr.Primitive(false))
                                          .Call("HelloWorld")
                                          .EndIf

                                          .Declare(typeof(int[]), "array", Expr.NewArray(typeof(int), Expr.Primitive(5)))
                                          .ForArray("i", Expr.Var("array"))
                                          .EndFor
                                          .EndIf

                                          .Return(Expr.ArrayIndex(Expr.Var("stringArr"), Expr.Primitive(0)))
                                          .EndMethod
                                          .EndClass

                                          .Enum(MemberAttributes.Public, "SuperEnum")
                                          .Value("Unit", 1).EndValue
                                          .Value("Testing", 2).EndValue
                                          .Value("Sucks").EndValue
                                          .EndEnum
                                          .EndNamespace
                                          .EndFluent();

            TestGenerated(compileUnit);
        }