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