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

            c.AddNamespace("Samples").AddClass(Define.Class("TestClass")
                                               .AddFields(
                                                   Define.Field(MemberAttributes.Private, typeof(string), "_s"),
                                                   Define.Field(MemberAttributes.Private, typeof(int), "_i")
                                                   )
                                               .AddCtor(
                                                   Define.Ctor((int i, string s) => MemberAttributes.Public,
                                                               Emit.assignField("_s", (string s) => s),
                                                               Emit.assignField("_i", (int i) => i)
                                                               )
                                                   )
                                               .AddMethod(MemberAttributes.Static | MemberAttributes.Public, "TestClass", () => "Create", Emit.@return(() => CodeDom.@new("TestClass", 100, "yyy")))
                                               );

            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);
        }
Exemplo n.º 2
0
        public void GenericObjectCreate()
        {
            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("TestClass", "T"), () => "foo",
                                            Emit.declare(CodeDom.TypeRef("TestClass", "T"), "cc",
                                                         () => CodeDom.@new(CodeDom.TypeRef("TestClass", "T"))),
                                            Emit.@return((Var cc) => cc))
                                 .Generic("T")
                                 );

            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);
        }
Exemplo n.º 3
0
        public static CodeMemberField AddField <T>(this CodeTypeDeclaration classCode,
                                                   string name, Expression <Func <T> > exp)
        {
            var c = Define.Field(name, exp);

            classCode.Members_Add(c);

            return(c);
        }
Exemplo n.º 4
0
        public static CodeMemberField AddField(this CodeTypeDeclaration classCode,
                                               Type fieldType, string name)
        {
            var c = Define.Field(CorrectAttributes(classCode, MemberAttributes.Private), fieldType, name);

            classCode.Members_Add(c);

            return(c);
        }
Exemplo n.º 5
0
        public static CodeMemberField AddField <T>(this CodeTypeDeclaration classCode,
                                                   Type fieldType, MemberAttributes ma, string name, Expression <Func <T> > exp)
        {
            var c = Define.Field(CorrectAttributes(classCode, ma), fieldType, name, exp);

            classCode.Members_Add(c);

            return(c);
        }
Exemplo n.º 6
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));
        }
        public ERROR_CODE GenerateCode(string useNamespace, Dictionary <string, StructInfo> 구조체정의_사전, Dictionary <string, string> 상수정의_사전)
        {
            Clear();

            var codeDom       = new LinqToCodedom.CodeDomGenerator();
            var codeNamespace = codeDom.AddNamespace(useNamespace);


            var constantClass = codeNamespace.AddClass("ConstantSet");

            foreach (var 상수정보 in 상수정의_사전)
            {
                var value = Convert.ToInt32(상수정보.Value);
                constantClass.AddField(Type.GetType("System.Int32"),
                                       System.CodeDom.MemberAttributes.Public | System.CodeDom.MemberAttributes.Const,
                                       상수정보.Key, () => value);
            }


            foreach (var structInfo in 구조체정의_사전.Values)
            {
                var structClass = codeNamespace.AddClass(structInfo.Name);

                foreach (var member in structInfo.MemberList)
                {
                    var typeName = CSharpType(member);

                    if (typeName.Item1 == false)
                    {
                        structClass.AddField(Type.GetType(typeName.Item2), System.CodeDom.MemberAttributes.Public, member.Name);
                    }
                    else
                    {
                        structClass.AddFields(Define.Field(System.CodeDom.MemberAttributes.Public, typeName.Item2, member.Name));
                    }
                }
            }

            var generateCode  = codeDom.GenerateCode(LinqToCodedom.CodeDomGenerator.Language.CSharp);
            var GeneratedCode = generateCode.Split("\r\n").ToList();

            return(ERROR_CODE.NONE);
        }
Exemplo n.º 8
0
        public void Builder_GenericClass()
        {
            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")
                                               );

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

            object t = realType.InvokeMember(null, System.Reflection.BindingFlags.CreateInstance, null, null, null);

            Assert.IsNotNull(t);

            realType.InvokeMember("S", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.SetProperty, null, t,
                                  new object[] { "xxx" });

            Assert.AreEqual("xxx", realType.InvokeMember("S", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetProperty, null, t, null));
        }
Exemplo n.º 9
0
        public void Builder_Attribute()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples").AddClass(Define.Class("TestClass")
                                               .AddAttribute(Define.Attribute(typeof(SerializableAttribute)))
                                               .AddFields(
                                                   Define.Field(MemberAttributes.Private, typeof(string), "_s"),
                                                   Define.Field(MemberAttributes.Private, typeof(int), "_i")
                                                   )
                                               .AddCtor(
                                                   Define.Ctor(() => MemberAttributes.Public,
                                                               Emit.assignField("_s", () => "xxx"),
                                                               Emit.assignField("_i", () => 10)
                                                               )
                                                   )
                                               .AddCtor(
                                                   Define.Ctor((int i, string s) => MemberAttributes.Public,
                                                               Emit.assignField("_s", (string s) => s),
                                                               Emit.assignField("_i", (int i) => i)
                                                               )
                                                   )
                                               .AddGetProperty(typeof(string), MemberAttributes.Public, "S", "_s").Comment("This is a comment")
                                               .AddGetProperty(typeof(int), MemberAttributes.Public, "I", "_i").Document("This is a documentation")
                                               .AddMethod(MemberAttributes.Public | MemberAttributes.Static, typeof(object), (MemoryStream ms) => "Deserialize",
                                                          Emit.declare("f", () => new BinaryFormatter()),
                                                          Emit.stmt((MemoryStream ms) => ms.Seek(0, SeekOrigin.Begin)),
                                                          Emit.@return((BinaryFormatter f, MemoryStream ms) => f.Deserialize(ms))
                                                          )
                                               );

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

            using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter f = new BinaryFormatter();

                f.Serialize(ms, t);

                //object t2 = TestClass.GetMethod("Deserialize").Invoke(null, new object[] { ms });

                //Assert.IsNotNull(t2);

                //Assert.AreEqual(
                //    TestClass.InvokeMember("S", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetProperty, null, t, null),
                //    TestClass.InvokeMember("S", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetProperty, null, t2, null)
                //);

                //Assert.AreEqual(
                //    TestClass.InvokeMember("I", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetProperty, null, t, null),
                //    TestClass.InvokeMember("I", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetProperty, null, t2, null)
                //);
            }
        }
Exemplo n.º 10
0
        public void Builder_Ctors()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples").AddClass(Define.Class("TestClass")
                                               .AddFields(
                                                   Define.Field(MemberAttributes.Private, typeof(string), "_s"),
                                                   Define.Field(MemberAttributes.Private, typeof(int), "_i")
                                                   )
                                               .AddCtor(
                                                   Define.Ctor(() => MemberAttributes.Public,
                                                               Emit.assignField("_s", () => "xxx"),
                                                               Emit.assignField("_i", () => 10)
                                                               )
                                                   )
                                               .AddCtor(
                                                   Define.Ctor((int i, string s) => MemberAttributes.Public,
                                                               Emit.assignField("_s", (string s) => s),
                                                               Emit.assignField("_i", (int i) => i)
                                                               )
                                                   )
                                               .AddGetProperty(typeof(string), MemberAttributes.Public, "S", "_s").Comment("This is a comment")
                                               .AddGetProperty(typeof(int), MemberAttributes.Public, "I", "_i").Document("This is a documentation")
                                               .AddMethod(MemberAttributes.Static | MemberAttributes.Public, "TestClass", () => "Create",
                                                          Emit.@return(() => CodeDom.@new("TestClass", 100, "yyy")))
                                               );

            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("xxx", TestClass.InvokeMember("S", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetProperty, null, t, null));
            Assert.AreEqual(10, TestClass.InvokeMember("I", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetProperty, null, t, null));

            t = TestClass.InvokeMember(null, System.Reflection.BindingFlags.CreateInstance, null, null,
                                       new object[] { 100, "yyy" });

            Assert.IsNotNull(t);

            Assert.AreEqual("yyy", TestClass.InvokeMember("S", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetProperty, null, t, null));
            Assert.AreEqual(100, TestClass.InvokeMember("I", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetProperty, null, t, null));

            t = TestClass.InvokeMember("Create", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.InvokeMethod, null, null, null);

            Assert.IsNotNull(t);

            Assert.AreEqual("yyy", TestClass.InvokeMember("S", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetProperty, null, t, null));
            Assert.AreEqual(100, TestClass.InvokeMember("I", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetProperty, null, t, null));
        }
Exemplo n.º 11
0
        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);
        }