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