Пример #1
0
        public void Builder_AccessProperty()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples").AddClass("cls1")
                .AddField(typeof(int), "_i", () => 10)
                .AddProperty(typeof(int), MemberAttributes.Public, "I", "_i")
            .AddClass("cls2")
                .AddMethod(MemberAttributes.Public | MemberAttributes.Static, typeof(int), () => "foo",
                    Emit.declare("cls1", "cc", () => CodeDom.@new("cls1")),
                    Emit.@return((Var cc) => cc.Property<int>("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.cls2");

            Assert.IsNotNull(TestClass);

            int s = (int)TestClass.InvokeMember("foo",
                BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod,
                null, null, null);

            Assert.AreEqual(10, s);
        }
Пример #2
0
        static void Main()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples")
            .AddClass("TestClass")
                .AddMethod(MemberAttributes.Public | MemberAttributes.Static, (int a) => "Print",
                    Emit.stmt(() => Console.WriteLine("Hello, world!")),
                    Emit.stmt((int a) => Console.WriteLine(a)),
                    Emit.@if((int a) => a == 10,
                        Emit.stmt(() => Console.WriteLine("a equals 10")))
                //Builder.ifelse((Par<int> a) => a.GetHashCode() == 10 && a < 1 && (2 + 3) < 7,
                //    Builder.GetStmts(Builder.stmt(() => Console.WriteLine("true"))),
                //    Builder.stmt(() => Console.WriteLine("false")))
            );

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

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

            var method = c.Compile().GetType("Samples.TestClass").GetMethod("Print");

            Console.WriteLine("Program is compiled and prints");
            Console.WriteLine("-----------------------------");

            method.Invoke(null, new object[] { 10 });
        }
Пример #3
0
        public void ArrayComplexTypeCreate()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples").AddClass("cls")
                .AddMethod(MemberAttributes.Public | MemberAttributes.Static, () => "foo",
                    Emit.declare("cls[]", "d"),
                    Emit.declare(CodeDom.TypeRef(typeof(List<>), "cls"), "d2"),
                    Emit.assignVar("d", (Var d2) => d2.Call("ToArray"))
                )
            ;

            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);
        }
Пример #4
0
        public void WorkingWithEvent()
        {
            var c = new CodeDomGenerator();

            c.AddReference("System.Core.dll").AddNamespace("Samples").AddClass("cls")
                .AddEvent(typeof(Action), MemberAttributes.Public, "ev")
                .AddMethod(MemberAttributes.Public, () => "raise",
                    Emit.declare("cls2", "cc", () => CodeDom.@new("cls2")),
                    Emit.attachDelegate(CodeDom.@this, "ev", CodeDom.VarRef("cc"), "zoo"),
                    Emit.attachDelegate(CodeDom.@this, "ev", "cls2.foo"),
                    Emit.stmt(() => [email protected]("ev")()),
                    Emit.detachDelegate(CodeDom.@this, "ev", CodeDom.VarRef("cc"), "zoo")
                )
            .AddClass("cls2")
                .AddMethod(MemberAttributes.Public, () => "zoo",
                    Emit.stmt(() => Console.WriteLine("ok"))
                )
                .AddMethod(MemberAttributes.Public | MemberAttributes.Static, () => "foo",
                    Emit.stmt(() => Console.WriteLine("ok"))
                )
            ;

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

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

            var ass = c.Compile();

            Assert.IsNotNull(ass);

            Type cls = ass.GetType("Samples.cls");

            Assert.IsNotNull(cls);
        }
Пример #5
0
        public void ThisTest()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples")
                .AddClass("cls").Implements(typeof(IServiceProvider))
                .AddMethod(MemberAttributes.Public, typeof(object), (Type t) => "GetService",
                    Emit.@return(() => [email protected]<IServiceProvider>())
                ).Implements(typeof(IServiceProvider))
            ;

            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);
        }
Пример #6
0
        public void TestLambdaVB()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples")
                .AddClass("cls")
                .AddMethod(MemberAttributes.Public | MemberAttributes.Static, (bool f) => "foo",
                    Emit.declare("a", () => CodeDom.Lambda(() => 1)),
                    Emit.declare("c", () => CodeDom.Lambda((int aa) => aa+10,
                        new LambdaParam(typeof(int), "aa"))),
                    Emit.stmt((int a) => CodeDom.CallDelegate(CodeDom.VarRef("c").Call("Compile"))(
                        CodeDom.CallDelegate(CodeDom.VarRef("a").Call("Compile"))
                        ))
                )
            ;

            c.AddReference("System.Core.dll");

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

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

            Assert.IsNotNull(ass);

            Type TestClass = ass.GetType("Samples.cls");

            Assert.IsNotNull(TestClass);
        }
Пример #7
0
        public void StaticDelegate()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples").AddClass("cls")
                .AddMethod(MemberAttributes.Static | MemberAttributes.Public,
                    () => "foo",
                    Emit.declare(typeof(EventHandler), "h"),
                    Emit.assignDelegate("h", "zoo"),
                    Emit.stmt((Var h) => h.Call()(null, null))
                )
                .AddMethod(MemberAttributes.Static | MemberAttributes.Private, (object sender, EventArgs args) => "zoo")
            ;

            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);
        }
Пример #8
0
        public void ParamArrayTest()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples").AddClass("cls")
                .AddMethod(MemberAttributes.Static | MemberAttributes.Public,
                    (ParamArray<int> i) => "foo",
                    Emit.declare("j", () => "zzz")
                )
                .AddMethod(MemberAttributes.Static | MemberAttributes.Public,
                    (ParamArray<int[]> i) => "foo2",
                    Emit.declare("js", () => "zzz")
                )
            ;

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

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

            var ass = c.Compile();

            Assert.IsNotNull(ass);

            Type cls = ass.GetType("Samples.cls");

            Assert.IsNotNull(cls);
        }
Пример #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)
                //);
            }
        }
Пример #10
0
        public void TestReadonlyField()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples").AddClass(Define.Class("ee", TypeAttributes.Public)
                .AddMember(Define.ReadOnlyField(MemberAttributes.Family, new CodeTypeReference(typeof(int)), "fld1"))
                .AddMember(Define.ReadOnlyField(MemberAttributes.Private | MemberAttributes.Static, new CodeTypeReference(typeof(int)), "fld2"))
                .AddMember(Define.Const(MemberAttributes.Private, "cns1", ()=>1))
                .AddMember(Define.Const(MemberAttributes.Public, "cns2", () => "hi!"))
            );

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

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

            Assert.IsNotNull(ass);

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

            Assert.IsNotNull(eeClass);
        }
Пример #11
0
        public void TestPropertyInterface()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples")
                .AddClass(Define.Class("ee", TypeAttributes.Public, true).Implements(new CodeTypeReference("Ixxx"))
                    .AddMember(Define.Property(new CodeTypeReference(typeof(string)), MemberAttributes.Public,
                        "prop",
                        new CodeStatement[]{ Emit.@return(() => "sdfsf")},
                        new CodeStatement[]{ Emit.assignVar("value", ()=>"sadfaf")}
                    ).Implements(new CodeTypeReference("Ixxx"), "dsf"))
                ).AddInterface(Define.Interface("Ixxx")
                    .AddProperty(new CodeTypeReference(typeof(string)), MemberAttributes.Public, "dsf")
                );

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

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

            Assert.IsNotNull(ass);
        }
Пример #12
0
        public void TestPartialMethodsVB()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples").AddClass(Define.Class("ee", TypeAttributes.Public, true)
                .AddMember(new CodePartialMethod(Define.Method(MemberAttributes.Public,
                    () => "foo"
                )))
                //.AddMember(new CodePartialMethod(Define.Method(MemberAttributes.Public, typeof(string),
                //    () => "foo2"
                //)))
                )
                .AddClass(Define.Class("ee", TypeAttributes.Public, true)
                .AddMember(new CodePartialMethod(Define.Method(MemberAttributes.Public,
                    () => "foo", Emit.stmt(() => Console.WriteLine())
                )))
                )
            ;

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

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

            Assert.IsNotNull(ass);

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

            Assert.IsNotNull(eeClass);
        }
Пример #13
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);
        }
Пример #14
0
        public void TestCustomEvent()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples").AddClass(Define.Class("ee", TypeAttributes.Public, true)
                .AddMember(Define.Event(new CodeTypeReference(typeof(EventHandler)), MemberAttributes.Family,
                    "Event1", Define.GetProperty("", default(MemberAttributes), "",
                        Emit.stmt(()=>Console.WriteLine("add"))
                    ),
                    Define.GetProperty("", default(MemberAttributes), "",
                        Emit.stmt(() => Console.WriteLine("remove"))
                    ),
                    Define.Method(default(MemberAttributes), (object sender, EventArgs args)=>"",
                        Emit.stmt(() => Console.WriteLine("raise"))
                    )
                ))
            );

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

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

            Assert.IsNotNull(ass);

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

            Assert.IsNotNull(eeClass);
        }
Пример #15
0
        public void Builder_Struct()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples").AddStruct(Define.Struct("xxx")
                .AddField(typeof(bool), "_x")
                .AddField(typeof(int), "_y")
                .AddCtor((bool x) => MemberAttributes.Public,
                    Emit.assignField("_x", (bool x) => x),
                    Emit.assignField("_y", ()=> 100)
                )
                .AddGetProperty(typeof(int), MemberAttributes.Public, "Z", "_y")
            );

            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.xxx");

            Assert.IsNotNull(TestClass);

            object t = TestClass.InvokeMember(null, BindingFlags.CreateInstance, null, null,
                new object[] { false });

            Assert.IsNotNull(t);

            Assert.AreEqual(100, TestClass.InvokeMember("Z", BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t, null));
        }
Пример #16
0
        public void MultidimensionalArray()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples").AddClass("cls")
                .AddMethod(MemberAttributes.Public | MemberAttributes.Static, () => "foo",
                    Emit.declare("d", () => new int[2, 2]),
                    Emit.declare("d2", (int[,] d) => d[0, 1])
                )
            ;

            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);
        }
Пример #17
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);
        }
Пример #18
0
        public void ArrayComplexTypeMultiIndexer()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples").AddClass("cls")
                .AddMethod(MemberAttributes.Public | MemberAttributes.Static, () => "foo",
                    Emit.declare("cls[,]", "d"),
                    Emit.declare("cls", "d2"),
                    Emit.assignVar("d2", (Var d) => d.ArrayGet(1,0))
                )
            ;

            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);
        }
Пример #19
0
        public void ParamByRef()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples").AddClass("cls")
                .AddMethod(MemberAttributes.Static | MemberAttributes.Public,
                    (RefParam<int> i, DynTypeRef j) => "foo" + j.SetType(typeof(string)),
                    Emit.assignVar("i", () => 10),
                    Emit.assignVar("j", () => "zzz")
                )
            ;

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

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

            var ass = c.Compile();

            Assert.IsNotNull(ass);

            Type cls = ass.GetType("Samples.cls");

            Assert.IsNotNull(cls);

            object[] args = new object[] { 0, "" };
            System.Reflection.ParameterModifier p = new System.Reflection.ParameterModifier(2);
            p[0] = true;
            p[1] = true;

            string s = (string)cls.InvokeMember("foo",
                System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Static,
                null, new System.Reflection.ParameterModifier[] { p }, args);

            Assert.AreEqual(10, args[0]);
            Assert.AreEqual("zzz", args[1]);
        }
Пример #20
0
        public void ArrayCreate()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples").AddClass("cls")
                .AddMethod(MemberAttributes.Public | MemberAttributes.Static, () => "foo",
                    Emit.declare(typeof(int[]), "d"),
                    Emit.declare("d2", () => new int[] { 1, 2, 3 }),
                    Emit.assignVar("d", () => new int[] { 3, 4 }),
                    Emit.declare("d3", (int[] d) => d[0]),
                    Emit.declare("d4", ()=>new int[10])
                )
            ;
            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);
        }
Пример #21
0
        public void TestAnonymousMethod()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples")
                .AddClass("cls")
                .AddMethod(MemberAttributes.Public | MemberAttributes.Static, () => "foo",
                    Emit.declare("a", () => CodeDom.Lambda<Func<int, double>>(new []{new LambdaParam("s")},
                        Emit.@return((double s)=>s+1)
                    ))
                )
            ;

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

            c.AddReference("System.Core.dll");

            var ass = c.Compile();

            Assert.IsNotNull(ass);

            Type TestClass = ass.GetType("Samples.cls");

            Assert.IsNotNull(TestClass);
        }
Пример #22
0
        public void CastExpression()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples").AddClass("cls")
                .AddMethod(MemberAttributes.Public, () => "foo",
                    Emit.declare(typeof(object), "d"),
                    Emit.assignVar("d", () => 10d),
                    Emit.declare("dr", (object d) => (decimal)d),
                    Emit.declare("dr2", (object d) => CodeDom.cast(typeof(decimal), d)),
                    Emit.stmt((object d) => CodeDom.cast<Var>("cls", d).Call("foo"))
                )
            ;

            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);
        }
Пример #23
0
        public void TestAssignIndexer()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples")
                .AddClass("cls")
                .AddMethod(MemberAttributes.Public | MemberAttributes.Static, () => "foo",
                    Emit.declare("b", () => new int[10]),
                    Emit.assignExp((int[] b)=>b[0], CodeDom.GetExpression(()=>1)),
                    Emit.declare("a", () => new List<int>()),
                    Emit.assignExp((List<int> a) => a[0], CodeDom.GetExpression(() => 1))
                )
            ;

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

            var ass = c.Compile();

            Assert.IsNotNull(ass);

            Type TestClass = ass.GetType("Samples.cls");

            Assert.IsNotNull(TestClass);

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

            ass = c.Compile();

            Assert.IsNotNull(ass);

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

            Assert.IsNotNull(TestClass);
        }
Пример #24
0
        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);
        }
Пример #25
0
        public void TestTernary()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples")
                .AddClass("cls")
                .AddMethod(MemberAttributes.Public | MemberAttributes.Static, (bool f) => "foo",
                    Emit.declare("b", (bool f) => !f),
                    Emit.assignVar("b", (bool f)=>f?true:false)
                )
            ;

            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);
        }
Пример #26
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);
        }
Пример #27
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);
        }
Пример #28
0
        public void InstanceClassDelegate()
        {
            var c = new CodeDomGenerator();

            c.AddReference("System.Core.dll").AddNamespace("Samples").AddClass("cls")
                .AddMethod(MemberAttributes.Public, typeof(string), () => "foo", Emit.declare(typeof(Func<int, string>), "h2"), Emit.assignDelegate("h2", CodeDom.@this, "zoo"), Emit.@return((Func<int, string> h2) => h2(10)))
                .AddMethod(MemberAttributes.Public, typeof(string), (int i) => "zoo", Emit.@return((int i) => i.ToString()))
            .AddClass("cls2")
                .AddMethod(MemberAttributes.Public | MemberAttributes.Static, typeof(string), (DynType cc) => "foo" + cc.SetType("cls"), Emit.declare(typeof(Func<int, string>), "h2"), Emit.assignDelegate("h2", CodeDom.VarRef("cc"), "zoo"), Emit.@return((Func<int, string> h2) => h2(100)))
                .AddMethod(MemberAttributes.Public | MemberAttributes.Static, typeof(string), (DynType cc, DynType c2) => "foo" + cc.SetType("cls") + c2.SetType(typeof(string)), Emit.declare(typeof(Func<int, string>), "h2"), Emit.assignDelegate("h2", CodeDom.VarRef("cc"), "zoo"), Emit.@return((Func<int, string> h2, string c2) => h2(100) + c2))
            ;

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

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

            var ass = c.Compile();

            Assert.IsNotNull(ass);

            Type cls = ass.GetType("Samples.cls");

            Assert.IsNotNull(cls);

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

            Assert.IsNotNull(t);

            string s = (string)cls.InvokeMember("foo",
                System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Instance,
                null, t, null);

            Assert.AreEqual("10", s);

            Type cls2 = ass.GetType("Samples.cls2");

            Assert.IsNotNull(cls2);

            object t2 = cls2.InvokeMember(null, System.Reflection.BindingFlags.CreateInstance, null, null, null);

            Assert.IsNotNull(t2);

            string s2 = (string)cls2.InvokeMember("foo",
                System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Static,
                null, t2, new object[] { t });

            Assert.AreEqual("100", s2);
        }
Пример #29
0
        public void XorExpression()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples").AddClass("cls")
                .AddMethod(MemberAttributes.Static | MemberAttributes.Public, (int i) => "foo",
                    Emit.declare("d", (int i) => i ^ 5)
                )
            ;

            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);
        }
Пример #30
0
        public void InstanceDelegate()
        {
            var c = new CodeDomGenerator();

            c.AddReference("System.Core.dll").AddNamespace("Samples").AddClass("cls")
                .AddMethod(MemberAttributes.Static | MemberAttributes.Public, typeof(string), () => "foo", Emit.declare("h2", () => new Func<string>("aaa".ToString)), Emit.@return((Func<string> h2) => h2()))
            ;

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

            string s = (string)TestClass.InvokeMember("foo",
                System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.InvokeMethod,
                null, null, null);

            Assert.AreEqual("aaa", s);
        }