0
|
1 ''@ example:
|
|
2 ''@ emit Emit
|
|
3 Imports System
|
|
4 Imports NUnit.Framework
|
|
5
|
|
6 Imports BLToolkit.Reflection
|
|
7 Imports BLToolkit.Reflection.Emit
|
|
8
|
|
9 Namespace Examples.Reflection.Emit
|
|
10
|
|
11 <TestFixture()> _
|
|
12 Public Class HelloWorld
|
|
13
|
|
14 Public Interface IHello
|
|
15 Sub SayHello(ByVal toWhom As String)
|
|
16 End Interface
|
|
17
|
|
18 <Test()> _
|
|
19 Sub Test()
|
|
20 Dim assemblyHelper As AssemblyBuilderHelper = New AssemblyBuilderHelper("HelloWorld.dll")
|
|
21 Dim typeHelper As TypeBuilderHelper = assemblyHelper.DefineType("Hello", GetType(Object), GetType(IHello))
|
|
22 Dim methodHelper As MethodBuilderHelper = typeHelper.DefineMethod(GetType(IHello).GetMethod("SayHello"))
|
|
23 Dim emit As EmitHelper = methodHelper.Emitter
|
|
24
|
|
25 ' string.Format("Hello, {0} World!", toWhom)
|
|
26 '
|
|
27 emit _
|
|
28 .ldstr("Hello, {0} World!") _
|
|
29 .ldarg_1 _
|
|
30 .call(GetType(String), "Format", GetType(String), GetType(Object))
|
|
31
|
|
32 ' Console.WriteLine("Hello, World!");
|
|
33 '
|
|
34 emit _
|
|
35 .call(GetType(Console), "WriteLine", GetType(String)) _
|
|
36 .ret()
|
|
37
|
|
38 Dim type As Type = typeHelper.Create()
|
|
39
|
|
40 Dim hello As IHello = TypeAccessor.CreateInstance(type)
|
|
41
|
|
42 hello.SayHello("VB")
|
|
43 End Sub
|
|
44
|
|
45 End Class
|
|
46
|
|
47 End Namespace
|