0
|
1 //@ example:
|
|
2 //@ emit Emit
|
|
3 using System;
|
|
4
|
|
5 using NUnit.Framework;
|
|
6
|
|
7 using BLToolkit.Reflection;
|
|
8 using BLToolkit.Reflection.Emit;
|
|
9
|
|
10 namespace Examples.Reflection.Emit
|
|
11 {
|
|
12 [TestFixture]
|
|
13 public class HelloWorld
|
|
14 {
|
|
15 public interface IHello
|
|
16 {
|
|
17 void SayHello(string toWhom);
|
|
18 }
|
|
19
|
|
20 [Test]
|
|
21 public void Test()
|
|
22 {
|
|
23 EmitHelper emit = new AssemblyBuilderHelper("HelloWorld.dll")
|
|
24 .DefineType ("Hello", typeof(object), typeof(IHello))
|
|
25 .DefineMethod(typeof(IHello).GetMethod("SayHello"))
|
|
26 .Emitter;
|
|
27
|
|
28 /*[a]*/emit/*[/a]*/
|
|
29 // string.Format("Hello, {0}!", toWhom)
|
|
30 //
|
|
31 ./*[a]*/ldstr/*[/a]*/ ("Hello, {0}!")
|
|
32 ./*[a]*/ldarg_1/*[/a]*/
|
|
33 ./*[a]*/call/*[/a]*/ (typeof(string), "Format", typeof(string), typeof(object))
|
|
34
|
|
35 // Console.WriteLine("Hello, World!");
|
|
36 //
|
|
37 ./*[a]*/call/*[/a]*/ (typeof(Console), "WriteLine", typeof(string))
|
|
38 ./*[a]*/ret/*[/a]*/()
|
|
39 ;
|
|
40
|
|
41 Type type = emit.Method.Type.Create();
|
|
42
|
|
43 IHello hello = (IHello)TypeAccessor.CreateInstance(type);
|
|
44
|
|
45 hello.SayHello("World");
|
|
46 }
|
|
47 }
|
|
48 }
|