Beispiel #1
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)
                //);
            }
        }