Exemplo n.º 1
0
        public void TestDefaultProperty()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples").AddClass(Define.Class("ee", TypeAttributes.Public, true)
                                               .AddMember(Define.GetProperty(new CodeTypeReference(typeof(string)), MemberAttributes.Family,
                                                                             (int idx) => "prop", true,
                                                                             Emit.@return((int idx) => idx.ToString())
                                                                             ))
                                               .AddMember(Define.Property(new CodeTypeReference(typeof(string)), MemberAttributes.Family,
                                                                          (int idx) => "prop2", false,
                                                                          CodeDom.CombineStmts(Emit.@return((int idx) => idx.ToString())),
                                                                          Emit.declare("i", (int idx) => idx)
                                                                          ))
                                               );

            Console.WriteLine(c.GenerateCode(CodeDomGenerator.Language.VB));

            Console.WriteLine(c.GenerateCode(CodeDomGenerator.Language.CSharp));

            var ass = c.Compile(null, CodeDomGenerator.Language.VB);

            Assert.IsNotNull(ass);

            Type eeClass = ass.GetType("Samples.ee");

            Assert.IsNotNull(eeClass);
        }
Exemplo n.º 2
0
        public void Builder_Interface()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples")
            .AddClass(Define.Class("TestClass", TypeAttributes.Public)
                      .Implements(typeof(IDisposable))
                      .AddMethod(MemberAttributes.Private, () => "Dispose",
                                 Emit.@throw(() => new NotImplementedException())
                                 ).Implements(typeof(IDisposable))
                      ).AddInterface(Define.Interface("Ixxx")
                                     .AddMethod(MemberAttributes.Public, () => "First")
                                     .AddMethod(MemberAttributes.Public, typeof(DateTime), () => "Second")
                                     .AddProperty("TestClass", MemberAttributes.Public, "Third")
                                     .AddGetProperty("TestClass", MemberAttributes.Public, "Fifth")
                                     .AddEvent(typeof(EventHandler), MemberAttributes.Public, "Fourth")
                                     ).AddClass("xxx").Implements(new CodeTypeReference("Ixxx"))
            .AddMethod(MemberAttributes.Private, () => "First",
                       Emit.@throw(() => new NotImplementedException())
                       ).Implements(new CodeTypeReference("Ixxx"))
            .AddMethod(MemberAttributes.Public, typeof(DateTime), () => "Second",
                       Emit.@throw(() => new NotImplementedException())
                       ).Implements(new CodeTypeReference("Ixxx"))
            .AddProperty("TestClass", MemberAttributes.Public, "Third",
                         CodeDom.CombineStmts(Emit.@throw(() => new NotImplementedException())),
                         Emit.@throw(() => new NotImplementedException())
                         ).Implements("Ixxx")
            .AddGetProperty("TestClass", MemberAttributes.Public, "Fifth",
                            Emit.@throw(() => new NotImplementedException())
                            ).Implements(new CodeTypeReference("Ixxx"))
            .AddEvent(typeof(EventHandler), MemberAttributes.Public, "Fourth"
                      ).Implements("Ixxx")
            .AddField(typeof(int), "_z", () => 100)
            ;

            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);

            ass = c.Compile(CodeDomGenerator.Language.VB);

            Assert.IsNotNull(ass);

            TestClass = ass.GetType("Samples.xxx");

            Assert.IsNotNull(TestClass);
        }
Exemplo n.º 3
0
        public void Builder_Property()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples").AddClass(Define.Class("TestClass")
                                               .AddFields(
                                                   Define.Field(MemberAttributes.Private, typeof(string), "_s")
                                                   )
                                               .AddProperty(
                                                   Define.Property(typeof(string), MemberAttributes.Public, "Test",
                                                                   CodeDom.CombineStmts(Emit.@return(() => [email protected] <string>("_s"))),
                                                                   Emit.assignField("_s", (string value) => value)
                                                                   )
                                                   )
                                               .AddProperty(
                                                   Define.Property(typeof(string), MemberAttributes.Public, "Test2", "_s")
                                                   )
                                               .AddProperty(typeof(string), MemberAttributes.Public, "Test3", "_s")
                                               .AddProperty("TestClass", MemberAttributes.Public, "Test4",
                                                            CodeDom.CombineStmts(Emit.@return(() => CodeDom.@this)),
                                                            Emit.@throw(() => new NotImplementedException(
                                                                            CodeDom.Property <string>(CodeDom.VarRef("value"), "Test")
                                                                            ))
                                                            )
                                               .AddMethod(MemberAttributes.Public, typeof(string), (int a) => "Test1",
                                                          Emit.assignProperty("Test", () => Guid.NewGuid().ToString()),
                                                          Emit.@return((int a) =>
                                                                       a.ToString() + [email protected] <string>("Test")))
                                               );

            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);

            string s = TestClass.InvokeMember("Test1", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.InvokeMethod, null, t,
                                              new object[] { 4 }) as string;

            Assert.IsNotNull(s);

            string TestProperty = s.Substring(1);

            Assert.AreEqual(TestProperty, TestClass.InvokeMember("Test", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetProperty, null, t, null));
        }
Exemplo n.º 4
0
        public void TypeOfExpression()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples").AddClass("cls")
            .AddMethod(MemberAttributes.Static | MemberAttributes.Public, typeof(string), (object o) => "foo",
                       Emit.ifelse((object o) => o.GetType() == typeof(int),
                                   CodeDom.CombineStmts(Emit.@return(() => "int")),
                                   Emit.@return(() => "other")))
            ;

            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);
        }
Exemplo n.º 5
0
        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));
        }
Exemplo n.º 6
0
        public void Builder_If()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples").AddClass(Define.Class("TestClass")
                                               .AddMethod(
                                                   Define.Method(MemberAttributes.Public | MemberAttributes.Static, typeof(int), (int a) => "Test",
                                                                 Emit.ifelse((int a) => a == 10,
                                                                             CodeDom.CombineStmts(Emit.@return(() => 1)),
                                                                             Emit.@return(() => - 1)
                                                                             ))
                                                   )
                                               .AddMethod(
                                                   Define.Method(MemberAttributes.Public | MemberAttributes.Static, typeof(int), (int a) => "Test2",
                                                                 Emit.ifelse((int a) => a < 10,
                                                                             CodeDom.CombineStmts(Emit.@return(() => 1)),
                                                                             Emit.@return(() => - 1)
                                                                             ))
                                                   )
                                               .AddMethod(
                                                   Define.Method(MemberAttributes.Public | MemberAttributes.Static, typeof(int), (int a) => "Test3",
                                                                 Emit.ifelse((int a) => a * 3 < 7,
                                                                             CodeDom.CombineStmts(Emit.@return(() => 1)),
                                                                             Emit.@return(() => - 1)
                                                                             ))
                                                   )
                                               .AddMethod(
                                                   Define.Method(MemberAttributes.Public | MemberAttributes.Static, typeof(int), (int a) => "Test4",
                                                                 Emit.ifelse((int a) => Math.Abs(a) * 3 < 7 + Math.Min(4, a),
                                                                             CodeDom.CombineStmts(Emit.@return(() => 1)),
                                                                             Emit.@return(() => - 1)
                                                                             ))
                                                   )
                                               );

            Console.WriteLine(c.GenerateCode(CodeDomGenerator.Language.CSharp));

            Console.WriteLine(c.GenerateCode(CodeDomGenerator.Language.VB));

            Assert.AreEqual(1, c.Compile().GetType("Samples.TestClass").GetMethod("Test")
                            .Invoke(null, new object[] { 10 }));

            Assert.AreEqual(-1, c.Compile().GetType("Samples.TestClass").GetMethod("Test")
                            .Invoke(null, new object[] { 100 }));

            Assert.AreEqual(1, c.Compile().GetType("Samples.TestClass").GetMethod("Test2")
                            .Invoke(null, new object[] { 7 }));

            Assert.AreEqual(-1, c.Compile().GetType("Samples.TestClass").GetMethod("Test2")
                            .Invoke(null, new object[] { 100 }));

            Assert.AreEqual(-1, c.Compile().GetType("Samples.TestClass").GetMethod("Test3")
                            .Invoke(null, new object[] { 10 }));

            Assert.AreEqual(1, c.Compile().GetType("Samples.TestClass").GetMethod("Test3")
                            .Invoke(null, new object[] { 2 }));

            Assert.AreEqual(1, c.Compile().GetType("Samples.TestClass").GetMethod("Test4")
                            .Invoke(null, new object[] { 2 }));

            Assert.AreEqual(-1, c.Compile().GetType("Samples.TestClass").GetMethod("Test4")
                            .Invoke(null, new object[] { -10 }));
        }