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[] { }); }
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) { 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[] { }); }
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[] { }); }
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[] { })); }
public static void TryCatchTest() { var ns = new FluentCodeCompileUnit().Namespace("TestNamespace") .Import("System"); FluentCodeClass type = ns.Class(MemberAttributes.Public, "TryCatchClass"); type.Method(MemberAttributes.Public, "TryCatchMethod") .Try .Throw(Expr.New(typeof(Exception), Expr.Primitive("Evil Exception"))) .Catch(typeof(Exception), "ex") .CallStatic(typeof(Console), "WriteLine", Expr.Primitive("Catch Block")) .Finally .CallStatic(typeof(Console), "WriteLine", Expr.Primitive("Finally Block")) .EndTry .EndMethod.EndFluent(); CodeCompileUnit compileUnit = ns.EndNamespace.EndFluent(); TestGenerated(compileUnit); }