Пример #1
0
        public static void ConstructorTest()
        {
            FluentCodeCompileUnit compileUnit = new FluentCodeCompileUnit();

            compileUnit.Namespace("TestNamespace")
            .Class("AClass").Inherits("BaseClass")
            .Constructor(MemberAttributes.Public)
            .BaseArgs()
            .EndConstructor

            .Constructor(MemberAttributes.Public).Parameter(typeof(int), "IntArg")
            .ThisArgs(Expr.Arg("IntArg"), Expr.Primitive("Hello"))
            .EndConstructor

            .Constructor(MemberAttributes.Public).Parameter(typeof(int), "IntArg").Parameter(typeof(string), "StringArg")
            .BaseArgs(Expr.Arg("StringArg"))
            .EndConstructor
            .EndClass

            .Class("BaseClass")
            .Constructor(MemberAttributes.Public)
            .EndConstructor

            .Constructor(MemberAttributes.Public).Parameter(typeof(string), "TextToPrint")
            .CallStatic(typeof(Console), "WriteLine", Expr.Arg("TextToPrint"))
            .EndConstructor
            .EndClass
            .EndFluent();

            Assembly assembly = TestGenerated(compileUnit.EndFluent());

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

            instance.GetType().GetConstructor(new Type[] { typeof(int) }).Invoke(instance, new object[] { 32 });
        }
Пример #2
0
        public static void ConstructorTest()
        {
            FluentCodeCompileUnit compileUnit = new FluentCodeCompileUnit();
            compileUnit.Namespace("TestNamespace")
                .Class("AClass").Inherits("BaseClass")
                    .Constructor(MemberAttributes.Public)
                        .BaseArgs()
                    .EndConstructor

                    .Constructor(MemberAttributes.Public).Parameter(typeof(int), "IntArg")
                        .ThisArgs(Expr.Arg("IntArg"), Expr.Primitive("Hello"))
                    .EndConstructor

                    .Constructor(MemberAttributes.Public).Parameter(typeof(int), "IntArg").Parameter(typeof(string), "StringArg")
                        .BaseArgs(Expr.Arg("StringArg"))
                    .EndConstructor
                .EndClass

                .Class("BaseClass")
                    .Constructor(MemberAttributes.Public)
                    .EndConstructor

                    .Constructor(MemberAttributes.Public).Parameter(typeof(string), "TextToPrint")
                        .CallStatic(typeof(Console), "WriteLine", Expr.Arg("TextToPrint"))
                    .EndConstructor
                .EndClass
            .EndFluent();

            Assembly assembly = TestGenerated(compileUnit.EndFluent());

            object instance = GetClassInstance("TestNamespace.AClass", assembly);
            instance.GetType().GetConstructor(new Type[] { typeof(int)}).Invoke(instance, new object[] { 32 });
        }
Пример #3
0
        public static void EnumTest()
        {
            FluentCodeCompileUnit compileUnit = new FluentCodeCompileUnit();

            compileUnit.Namespace("TestNamespace")
            .Enum("TestEnum").CustomAttribute(typeof(FlagsAttribute))
            .Value("Value1", Expr.Primitive(1)).EndValue
            .Value("Value2", Expr.Primitive(2)).EndValue
            .Value("Value3", Expr.Primitive(4)).EndValue
            .EndEnum
            .EndFluent();

            TestGenerated(compileUnit.EndFluent());
        }
Пример #4
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);
        }
Пример #5
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);
        }
Пример #6
0
        public static void EnumTest()
        {
            FluentCodeCompileUnit compileUnit = new FluentCodeCompileUnit();
            compileUnit.Namespace("TestNamespace")
                .Enum("TestEnum").CustomAttribute(typeof(FlagsAttribute))
                    .Value("Value1", Expr.Primitive(1)).EndValue
                    .Value("Value2", Expr.Primitive(2)).EndValue
                    .Value("Value3", Expr.Primitive(4)).EndValue
                .EndEnum
            .EndFluent();

            TestGenerated(compileUnit.EndFluent());
        }