public static void LinqTest() { var ns = new FluentCodeCompileUnit().Namespace("TestNamespace"); ns.Class("AClass") .Method(MemberAttributes.Public, "LinqTest").ReturnType(typeof(bool)) .Declare(typeof(int), "a", Expr.Primitive(5)) .Declare(typeof(int), "b", Expr.Primitive(6)) .If((int a, int b) => a > b) .Declare(typeof(string), "text", Expr.Primitive("A is bigger than B")) .Stmt((string text) => Console.WriteLine(text)) .Stmt(ExprLinq.Expr(() => Console.Read())) .Else .Declare(typeof(string), "text", Expr.Primitive("B is bigger or equal A")) .Stmt((string text) => Console.WriteLine(text)) .EndIf .Return((int a, int b) => a > b) .EndMethod .EndFluent(); CodeCompileUnit compileUnit = ns.EndNamespace.EndFluent(); Assembly assembly = TestGenerated(compileUnit); object instance = GetClassInstance("TestNamespace.AClass", assembly); instance.GetType().GetMethod("LinqTest").Invoke(instance, new object[] { }); }
static void Main(string[] args) { CodeCompileUnit compileUnit = new FluentCodeCompileUnit() .Namespace("Sample4") .Class("Program") .Method("Test") .CallStatic(typeof(Console), "WriteLine", Expr.Primitive("Test")) .If(Expr.Op(Expr.Primitive(5), CodeBinaryOperatorType.GreaterThan, Expr.Primitive(3))) .EndIf .EndMethod .Method(MemberAttributes.Public | MemberAttributes.Static, "Main").Parameter(typeof(string[]), "args") .Stmt(ExprLinq.Expr(() => Console.WriteLine("Hello Linq2CodeDOM"))) .Declare(typeof(int), "random", ExprLinq.Expr(() => new Random().Next(10))) .If((int random) => random <= 5) .Stmt(ExprLinq.Expr(() => Console.WriteLine("Smaller or equal to 5."))) .Else .Stmt(ExprLinq.Expr(() => Console.WriteLine("Bigger than 5."))) .EndIf .EndMethod .EndClass .EndNamespace .EndFluent(); Console.WriteLine(Helper.CodeDomHelper.GenerateCodeAsString(compileUnit, new CSharpCodeProvider())); Assembly assembly = Helper.CodeDomHelper.CompileInMemory(compileUnit); assembly.GetType("Sample4.Program").GetMethod("Main").Invoke(null, new object[] { null }); }