コード例 #1
0
        public static void UsingTest()
        {
            string executingAssemblyPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            string path = System.IO.Path.GetDirectoryName(executingAssemblyPath) + "\\..\\..\\test.txt";

            var ns = new FluentCodeCompileUnit().Namespace("TestNamespace");

            ns.Class("AClass")
            .Method(MemberAttributes.Public, "UsingTest")
            .Declare(typeof(System.IO.FileStream), "fs", Expr.CallStatic(typeof(System.IO.File), "OpenRead", Expr.Primitive(path)))
            .UsingEmu(Expr.Var("fs"))
            .UsingEmu(Expr.Declare(typeof(StreamReader), "sr", Expr.New(typeof(StreamReader), Expr.Var("fs"))))
            .CallStatic(typeof(Console), "WriteLine", Expr.CallMember(Expr.Var("sr"), "ReadToEnd"))
            .EndUsing
            .EndUsing
            .EndMethod
            .EndFluent();

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

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

            instance.GetType().GetMethod("UsingTest").Invoke(instance, new object[] { });
        }
コード例 #2
0
        private static TInterface ImplentInterface <TInterface>()
        {
            Type interfaceType     = typeof(TInterface);
            FluentCodeNamespace ns = new FluentCodeCompileUnit().Namespace("Sample2");

            // public class <Interface>Test : <Interface>
            string typeName = string.Format("{0}Test", interfaceType.Name);
            var    type     = ns.Class(typeName).Inherits(interfaceType);

            foreach (MethodInfo methodInfo in interfaceType.GetMethods())
            {
                // Console.WriteLine("<Method> called with Parameters:")
                var method =
                    type.Method(MemberAttributes.Public, methodInfo.Name)
                    .CallStatic(typeof(Console), "WriteLine", Expr.Primitive(string.Format("{0} called with parameters:", methodInfo.Name)));

                foreach (ParameterInfo paramInfo in methodInfo.GetParameters())
                {
                    method.Parameter(paramInfo.ParameterType, paramInfo.Name);

                    // Console.WriteLine("<ParamName>: <Value>")
                    method.CallStatic(typeof(Console), "WriteLine",
                                      Expr.CallStatic(typeof(string), "Format",
                                                      Expr.Primitive(string.Format("{0}: {1}", paramInfo.Name, "{1}")),
                                                      Expr.CallMember(Expr.Arg(paramInfo.Name), "ToString")
                                                      )
                                      );
                }
            }

            CodeCompileUnit compileUnit = ns.EndNamespace.EndFluent();

            // Display Code
            string code = Helper.CodeDomHelper.GenerateCodeAsString(compileUnit, new CSharpCodeProvider());

            Console.WriteLine(code);
            Assembly assembly = Helper.CodeDomHelper.CompileInMemory(compileUnit);

            return((TInterface)assembly.GetType(string.Format("Sample2.{0}", typeName))
                   .GetConstructor(Type.EmptyTypes)
                   .Invoke(new object[] { }));
        }
コード例 #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);
        }