public void TestGetExpression() { CodeExpression exp = CodeDom.GetExpression(() => 1); Assert.IsTrue(exp is CodePrimitiveExpression); Assert.AreEqual(1, ((CodePrimitiveExpression)exp).Value); CodeExpression exp2 = CodeDom.GetExpression((int g) => 1 + g); Assert.IsTrue(exp2 is CodeBinaryOperatorExpression); CodeExpression exp3 = CodeDom.GetExpression((int g) => [email protected] <int>("d") + g - 10); Assert.IsTrue(exp3 is CodeBinaryOperatorExpression); CodeExpression exp4 = CodeDom.GetExpression(() => CodeDom.Call(exp, "ToString")()); Assert.IsTrue(exp4 is CodeMethodInvokeExpression); Assert.AreEqual(exp, ((CodeMethodInvokeExpression)exp4).Method.TargetObject); CodeExpression exp5 = CodeDom.GetExpression(() => CodeDom.Call(exp3, "ToString")()); Assert.IsTrue(exp5 is CodeMethodInvokeExpression); Assert.AreEqual(exp3, ((CodeMethodInvokeExpression)exp5).Method.TargetObject); }
public void DefaultExpression() { var c = new CodeDomGenerator(); c.AddNamespace("Samples").AddClass("cls") .AddMethod(MemberAttributes.Static | MemberAttributes.Public, () => "foo", Emit.stmt(() => CodeDom.Call(CodeDom.TypeRef("cls"), "zoo")(default(int))) ) .AddMethod(MemberAttributes.Static | MemberAttributes.Private, (int i) => "zoo", Emit.stmt((int i) => Console.WriteLine(i)) ) ; Console.WriteLine(c.GenerateCode(CodeDomGenerator.Language.CSharp)); Console.WriteLine(c.GenerateCode(CodeDomGenerator.Language.VB)); var ass = c.Compile(); Assert.IsNotNull(ass); Type TestClass = ass.GetType("Samples.cls"); Assert.IsNotNull(TestClass); }
static void Main2() { var c = new CodeDomGenerator(); c.AddNamespace("Samples").AddClass("cls") .AddMethod(MemberAttributes.Public | MemberAttributes.Static, typeof(string), (int i) => "Print", Emit.@if((int i) => i < 10, Emit.@return(() => "i less than 10") ), Emit.@return(() => "i greater than 10") ); Console.WriteLine(c.GenerateCode(LinqToCodedom.CodeDomGenerator.Language.CSharp)); var c2 = new CodeDomGenerator(); CodeMemberMethod method = Define.Method(MemberAttributes.Public, () => "foo"); c2.AddNamespace("TestNS").AddClass("Fibonacci") .AddMethod(MemberAttributes.Public | MemberAttributes.Static, (int x) => "Calc", Emit.@if((int x) => x <= 1, Emit.@return(() => 1)), Emit.@return((int x) => CodeDom.Call <int>("Calc")(x - 1) + CodeDom.Call <int>("Calc")(x - 2)) ) ; Console.WriteLine(c2.GenerateCode(LinqToCodedom.CodeDomGenerator.Language.CSharp)); Console.WriteLine(c2.GenerateCode(LinqToCodedom.CodeDomGenerator.Language.VB)); }
public void Builder_MethodCall() { var c = new CodeDomGenerator(); c.AddNamespace("Samples").AddClass(Define.Class("TestClass") .AddMethod(MemberAttributes.Public, typeof(int), (int a) => "Test", Emit.@return((int a) => a + 100)) .AddMethod( Define.Method(MemberAttributes.Public, typeof(int), (int a) => "Test1", Emit.@return(() => CodeDom.VarRef <int>("a") + [email protected] <int>("Test")(3))) ) ).AddClass(Define.Class("cls") .AddMethod(MemberAttributes.Public | MemberAttributes.Static, () => "foo", Emit.declare("TestClass", "cc"), Emit.stmt(() => CodeDom.Call(CodeDom.VarRef("cc"), "Test1")(3)) ) .AddMethod(MemberAttributes.Public | MemberAttributes.Static, () => "foo2", Emit.declare("TestClass", "cc", () => CodeDom.@new("TestClass")), Emit.stmt((Var cc) => cc.Call("Test1")(3)) ) .AddMethod(MemberAttributes.Public | MemberAttributes.Static, (int s) => "foo3", Emit.declare("TestClass", "cc"), Emit.declare(typeof(int), "i"), Emit.stmt((int s) => CodeDom.Call(CodeDom.VarRef("cc"), "Test1")(CodeDom.VarRef <int>("i") + s)) ) .AddMethod(MemberAttributes.Public, () => "foo4", Emit.@foreach("mi", () => [email protected] <Type>("GetType")().GetMethods(BindingFlags.NonPublic | BindingFlags.Static), Emit.stmt(() => CodeDom.VarRef("mi").Call("GetParameters")), Emit.@if((MethodInfo mi) => mi.Name == "dfdfd", Emit.@return() ), Emit.@return() ) ) ); Console.WriteLine(c.GenerateCode(CodeDomGenerator.Language.CSharp)); Console.WriteLine(c.GenerateCode(CodeDomGenerator.Language.VB)); var ass = c.Compile(); Assert.IsNotNull(ass); Type TestClass = ass.GetType("Samples.TestClass"); Assert.IsNotNull(TestClass); object t = TestClass.InvokeMember(null, System.Reflection.BindingFlags.CreateInstance, null, null, null); Assert.IsNotNull(t); Assert.AreEqual(104, TestClass.InvokeMember("Test", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.InvokeMethod, null, t, new object[] { 4 })); Assert.AreEqual(104, TestClass.InvokeMember("Test1", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.InvokeMethod, null, t, new object[] { 1 })); }
private CodeCompileUnit GenerateModificationTracker(WXMLModel model, WXMLCodeDomGeneratorSettings setting) { var c = new CodeDomGenerator(); string mtName = "LinqModificationTracker"; var cls = c.AddNamespace(model.Namespace).AddClass(mtName); _ctxName = model.Namespace + "." + model.LinqSettings.ContextName; _mtName = model.Namespace + "." + mtName; string conn = ConfigurationManager.ConnectionStrings["wms"].ConnectionString; cls.Implements(typeof(IModificationTracker)) .AddEnum("ActionEnum") .AddFields( Define.StructField("Update"), Define.StructField("Insert"), Define.StructField("Delete") ); cls.AddField("_changed", () => new List <object>()); cls.AddField("_deleted", () => new List <object>()); var tableType = new CodeTypeReference(typeof(System.Data.Linq.Table <>)); tableType.TypeArguments.Add(new CodeTypeReference("T")); cls .AddMethod(MemberAttributes.Public | MemberAttributes.Final, (ParamArray <object> entities) => "Add", Emit.stmt((List <object> _changed, object[] entities) => _changed.AddRange(entities))) .Implements(typeof(IModificationTracker)) .AddMethod(MemberAttributes.Public | MemberAttributes.Final, (ParamArray <object> entities) => "Delete", Emit.stmt((List <object> _deleted, object[] entities) => _deleted.AddRange(entities))) .Implements(typeof(IModificationTracker)) .AddMethod(MemberAttributes.Public | MemberAttributes.Final, () => "AcceptModifications", Emit.@using(new CodeTypeReference(_ctxName), "ctx", () => CodeDom.@new(_ctxName, conn), Emit.@foreach("entity", () => [email protected] <IEnumerable <object> >("_changed"), Emit.stmt((object entity) => [email protected]("SyncEntity")(CodeDom.VarRef("ctx"), entity, false)) ), Emit.@foreach("entity", () => [email protected] <IEnumerable <object> >("_deleted"), Emit.stmt((object entity) => [email protected]("SyncEntity")(CodeDom.VarRef("ctx"), entity, true)) ), Emit.stmt(() => CodeDom.VarRef("ctx").Call("SubmitChanges")), Emit.@foreach("entity", () => [email protected] <IEnumerable <object> >("_changed"), Emit.stmt((object entity) => [email protected]("AcceptChanges")(entity)) ), Emit.stmt(() => [email protected] <List <object> >("_changed").Clear()), Emit.@foreach("entity", () => [email protected] <IEnumerable <object> >("_deleted"), Emit.stmt((object entity) => [email protected]("AcceptChanges")(entity)) ), Emit.stmt(() => [email protected] <List <object> >("_deleted").Clear()) ) ).Implements(typeof(IModificationTracker)) .AddMethod(MemberAttributes.Private, () => "Dispose").Implements(typeof(IDisposable)) .AddMethod(MemberAttributes.Private, (object entity) => "AcceptChanges", Emit.declare("mi", (object entity) => entity.GetType().GetMethod("AcceptChanges")), Emit.@if(() => CodeDom.VarRef("mi") != null, Emit.stmt((MethodInfo mi, object entity) => mi.Invoke(entity, null)) ) ) .AddMethod(MemberAttributes.Private, (DynType ctx, object entity, bool delete) => "SyncEntity" + ctx.SetType(_ctxName), Emit.@foreach("mi", () => [email protected] <Type>("GetType")().GetMethods(BindingFlags.NonPublic | BindingFlags.Static), Emit.@if((bool delete, MethodInfo mi, object entity) => ((delete && mi.Name == "_DelEntity") || (!delete && mi.Name == "_SyncEntity")) && mi.GetParameters().Count() == 2 && mi.GetParameters().Last().ParameterType == entity.GetType(), Emit.stmt((MethodInfo mi, object entity) => mi.Invoke(null, BindingFlags.Static, null, new object[] { CodeDom.VarRef("ctx"), entity }, null)), Emit.exitFor() ) ) ) .AddMethod(MemberAttributes.Private | MemberAttributes.Static, (DynType p, DynType action, DynType table) => "SyncEntity" + p.SetType("T") + action.SetType("ActionEnum") + table.SetType(tableType), Emit.ifelse(() => CodeDom.VarRef("action") == CodeDom.Field(new CodeTypeReference("ActionEnum"), "Insert"), CodeDom.CombineStmts(Emit.stmt(() => CodeDom.VarRef("table").Call("InsertOnSubmit")(CodeDom.VarRef("p")))), Emit.ifelse(() => CodeDom.VarRef("action") == CodeDom.Field(new CodeTypeReference("ActionEnum"), "Delete"), CodeDom.CombineStmts( Emit.stmt(() => CodeDom.VarRef("table").Call("Attach")(CodeDom.VarRef("p"))), Emit.stmt(() => CodeDom.VarRef("table").Call("DeleteOnSubmit")(CodeDom.VarRef("p"))) ), Emit.stmt(() => CodeDom.VarRef("table").Call("Attach")(CodeDom.VarRef("p"), true)) ) ) ).Generic("T", typeof(object)) ; WXMLCodeDomGeneratorNameHelper n = new WXMLCodeDomGeneratorNameHelper(setting); foreach (var entity in model.GetActiveEntities()) { if (entity.GetPkProperties().Count() == 1) { //string entityName = entity.Name; string entityProp = WXMLCodeDomGeneratorNameHelper.GetMultipleForm(entity.Name); string entityType = n.GetEntityClassName(entity, true); string pkName = entity.GetPkProperties().Single().Name; cls.AddMethod(MemberAttributes.Static | MemberAttributes.Private, (DynType ctx, DynType p) => "_DelEntity" + ctx.SetType(_ctxName) + p.SetType(entityType), Emit.stmt(() => CodeDom.Call(null, "SyncEntity", new CodeTypeReference(entityType))( CodeDom.VarRef("p"), CodeDom.Field(new CodeTypeReference("ActionEnum"), "Delete"), CodeDom.VarRef("ctx").Property(entityProp)) ) ) .AddMethod(MemberAttributes.Static | MemberAttributes.Private, (DynType ctx, DynType p) => "_SynEntity" + ctx.SetType(_ctxName) + p.SetType(entityType), Emit.stmt(() => CodeDom.Call(null, "SyncEntity", new CodeTypeReference(entityType))( CodeDom.VarRef("p"), CodeDom.VarRef("p").Field <int>(pkName) == 0 ? CodeDom.Field(new CodeTypeReference("ActionEnum"), "Insert") : CodeDom.Field(new CodeTypeReference("ActionEnum"), "Update"), CodeDom.VarRef("ctx").Property(entityProp)) ) ) ; } } string debug = c.GenerateCode(CodeDomGenerator.Language.CSharp); return(c.GetCompileUnit(CodeDomGenerator.Language.CSharp)); }
public void Builder_GenericMethod() { var c = new CodeDomGenerator(); c.AddNamespace("Samples").AddClass(Define.Class("TestClass").Generic("T") .AddFields( Define.Field(MemberAttributes.Private, "T", "_s") ) .AddProperty("T", MemberAttributes.Public, "S", "_s") ).AddClass(Define.Class("cls") .AddMethod(MemberAttributes.Public | MemberAttributes.Static, CodeDom.TypeRef(new CodeTypeReference("TestClass"), new CodeTypeReference("T")), () => "foo", Emit.declare(CodeDom.TypeRef(new CodeTypeReference("TestClass"), new CodeTypeReference("T")), "cc", () => CodeDom.@new(CodeDom.TypeRef(new CodeTypeReference("TestClass"), new CodeTypeReference("T")))), Emit.@return((Var cc) => cc)) .Generic("T") .AddMethod(MemberAttributes.Public | MemberAttributes.Static, () => "foo2", Emit.stmt(() => CodeDom.Call(CodeDom.TypeRef(new CodeTypeReference("cls")), "foo", typeof(int))) ) .AddMethod(MemberAttributes.Static, () => "foo3", Emit.@return() ).Generic("T", true, typeof(object)) .AddMethod(MemberAttributes.Static, () => "foo5", Emit.@return() ).Generic("T", true, "System.Object") .AddMethod(MemberAttributes.Static, () => "foo6", Emit.@return() ).Generic("T", true, new CodeTypeReference(typeof(object))) .AddMethod(MemberAttributes.Static, () => "foo4", Emit.@return() ).Generic("T", typeof(ValueType)) .AddMethod(MemberAttributes.Static, () => "foo7", Emit.@return() ).Generic("T", new CodeTypeReference(typeof(ValueType))) .AddMethod(MemberAttributes.Static, () => "foo8", Emit.@return() ).Generic("T", "cls") ); Console.WriteLine(c.GenerateCode(CodeDomGenerator.Language.CSharp)); Console.WriteLine(c.GenerateCode(CodeDomGenerator.Language.VB)); var ass = c.Compile(); Assert.IsNotNull(ass); Type TestClass = ass.GetType("Samples.TestClass`1"); Assert.IsNotNull(TestClass); Type realType = TestClass.MakeGenericType(new Type[] { typeof(string) }); Assert.IsNotNull(realType); Type clsType = ass.GetType("Samples.cls"); Assert.IsNotNull(clsType); System.Reflection.MethodInfo mi = clsType.GetMethod("foo"); Assert.IsNotNull(mi); System.Reflection.MethodInfo rmi = mi.MakeGenericMethod(typeof(string)); object t = rmi.Invoke(null, null); Assert.IsNotNull(t); }