Imports System
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
_
Class DynamicJumpTableDemo
Public Shared Function BuildMyType() As Type
Dim myDomain As AppDomain = Thread.GetDomain()
Dim myAsmName As New AssemblyName()
myAsmName.Name = "MyDynamicAssembly"
Dim myAsmBuilder As AssemblyBuilder = myDomain.DefineDynamicAssembly(myAsmName, _
AssemblyBuilderAccess.Run)
Dim myModBuilder As ModuleBuilder = myAsmBuilder.DefineDynamicModule("MyJumpTableDemo")
Dim myTypeBuilder As TypeBuilder = myModBuilder.DefineType("JumpTableDemo", _
TypeAttributes.Public)
Dim myMthdBuilder As MethodBuilder = myTypeBuilder.DefineMethod("SwitchMe", _
MethodAttributes.Public Or MethodAttributes.Static, _
GetType(String), New Type() {GetType(Integer)})
Dim myIL As ILGenerator = myMthdBuilder.GetILGenerator()
Dim defaultCase As Label = myIL.DefineLabel()
Dim endOfMethod As Label = myIL.DefineLabel()
' We are initializing our jump table. Note that the labels
' will be placed later using the MarkLabel method.
Dim jumpTable() As Label = {myIL.DefineLabel(), _
myIL.DefineLabel(), _
myIL.DefineLabel(), _
myIL.DefineLabel(), _
myIL.DefineLabel()}
' arg0, the number we passed, is pushed onto the stack.
' In this case, due to the design of the code sample,
' the value pushed onto the stack happens to match the
' index of the label (in IL terms, the index of the offset
' in the jump table). If this is not the case, such as
' when switching based on non-integer values, rules for the correspondence
' between the possible case values and each index of the offsets
' must be established outside of the ILGenerator.Emit calls,
' much as a compiler would.
Console.Write("Enter an integer between 0 and 5: ")
Dim theValue As Integer = Convert.ToInt32(Console.ReadLine())
Console.WriteLine("---")
Dim myInstance As [Object] = Activator.CreateInstance(myType, New Object() {})
Console.WriteLine("Yes, there {0} today!", myType.InvokeMember("SwitchMe", _
BindingFlags.InvokeMethod, Nothing, _
myInstance, New Object() {theValue}))
[Visual Basic] Overloads Public Overridable Sub Emit(OpCode)
[C#] public virtual void Emit(OpCode);
[C++] public: virtual void Emit(OpCode);
[JScript] public function Emit(OpCode);
将指定的指令和字符参数放在 Microsoft 中间语言 (MSIL) 指令流上。
[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, Byte)
[C#] public virtual void Emit(OpCode, byte);
[C++] public: virtual void Emit(OpCode, unsigned char);
[JScript] public function Emit(OpCode, Byte);
将指定构造函数的指定指令和元数据标记放到 Microsoft 中间语言 (MSIL) 指令流上。
[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, ConstructorInfo)
[C#] public virtual void Emit(OpCode, ConstructorInfo);
[C++] public: virtual void Emit(OpCode, ConstructorInfo*);
[JScript] public function Emit(OpCode, ConstructorInfo);
将指定的指令和数值参数放在 Microsoft 中间语言 (MSIL) 指令流上。
[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, Double)
[C#] public virtual void Emit(OpCode, double);
[C++] public: virtual void Emit(OpCode, double);
[JScript] public function Emit(OpCode, double);
将指定字段的指定指令和元数据标记放到 Microsoft 中间语言 (MSIL) 指令流上。
[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, FieldInfo)
[C#] public virtual void Emit(OpCode, FieldInfo);
[C++] public: virtual void Emit(OpCode, FieldInfo*);
[JScript] public function Emit(OpCode, FieldInfo);
将指定的指令和数值参数放在 Microsoft 中间语言 (MSIL) 指令流上。
[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, Short)
[C#] public virtual void Emit(OpCode, short);
[C++] public: virtual void Emit(OpCode, short);
[JScript] public function Emit(OpCode, Int16);
将指定的指令和数值参数放在 Microsoft 中间语言 (MSIL) 指令流上。
[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, Integer)
[C#] public virtual void Emit(OpCode, int);
[C++] public: virtual void Emit(OpCode, int);
[JScript] public function Emit(OpCode, int);
将指定的指令和数值参数放在 Microsoft 中间语言 (MSIL) 指令流上。
[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, Long)
[C#] public virtual void Emit(OpCode, long);
[C++] public: virtual void Emit(OpCode, __int64);
[JScript] public function Emit(OpCode, long);
将指定的指令放在 Microsoft 中间语言 (MSIL) 流上,并留出在完成修正时加上标签所需的空白。
[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, Label)
[C#] public virtual void Emit(OpCode, Label);
[C++] public: virtual void Emit(OpCode, Label);
[JScript] public function Emit(OpCode, Label);
将指定的指令放在 Microsoft 中间语言 (MSIL) 流上,并留出在完成修正时加上标签所需的空白。
[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, Label())
[C#] public virtual void Emit(OpCode, Label[]);
[C++] public: virtual void Emit(OpCode, Label[]);
[JScript] public function Emit(OpCode, Label[]);
将指定的指令放到 Microsoft 中间语言 (MSIL) 流上,后跟给定局部变量的索引。
[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, LocalBuilder)
[C#] public virtual void Emit(OpCode, LocalBuilder);
[C++] public: virtual void Emit(OpCode, LocalBuilder*);
[JScript] public function Emit(OpCode, LocalBuilder);
将指定的指令放到 Microsoft 中间语言 (MSIL) 流上,后跟给定方法的元数据标记。
[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, MethodInfo)
[C#] public virtual void Emit(OpCode, MethodInfo);
[C++] public: virtual void Emit(OpCode, MethodInfo*);
[JScript] public function Emit(OpCode, MethodInfo);
将指定的指令和字符参数放在 Microsoft 中间语言 (MSIL) 指令流上。此方法不符合 CLS。
[Visual Basic] Overloads Public Sub Emit(OpCode, SByte)
[C#] public void Emit(OpCode, sbyte);
[C++] public: void Emit(OpCode, char);
[JScript] public function Emit(OpCode, SByte);
将指定的指令和签名标记放在 Microsoft 中间语言 (MSIL) 指令流上。
[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, SignatureHelper)
[C#] public virtual void Emit(OpCode, SignatureHelper);
[C++] public: virtual void Emit(OpCode, SignatureHelper*);
[JScript] public function Emit(OpCode, SignatureHelper);
将指定的指令和数值参数放在 Microsoft 中间语言 (MSIL) 指令流上。
[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, Single)
[C#] public virtual void Emit(OpCode, float);
[C++] public: virtual void Emit(OpCode, float);
[JScript] public function Emit(OpCode, float);
将指定的指令放到 Microsoft 中间语言 (MSIL) 流上,后跟给定字符串的元数据标记。
[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, String)
[C#] public virtual void Emit(OpCode, string);
[C++] public: virtual void Emit(OpCode, String*);
[JScript] public function Emit(OpCode, String);
将指定的指令放到 Microsoft 中间语言 (MSIL) 流上,后跟给定类型的元数据标记。
[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, Type)
[C#] public virtual void Emit(OpCode, Type);
[C++] public: virtual void Emit(OpCode, Type*);
[JScript] public function Emit(OpCode, Type);
示例
[Visual Basic, C#, C++] 下面的代码示例阐释了如何用跳转表创建动态方法。使用 Label 数组创建跳转表。
Private Sub btnView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnView.Click
Dim temp(3) As Byte
temp(0) = &HCE
temp(1) = &HF7
temp(2) = &HC4
temp(3) = &HCF
Me.Label1.Text = System.Text.Encoding.Default.GetString(temp)
End Sub
嗯,在vb6里面有个函数cstr(),你可以把这个数值赋给一个数组,比如你已经知道了4个byte的数字,想把它用textbox显示出来,你可以这样:(.net我不熟,先用vb6描述一下)
dim temp(0 to 3) as byte
temp(0)=&HCE '&H是转换十六进制到十进制
temp(1)=&HF7
temp(2)=&HC4
temp(3)=&HCF
textbox.text=trim(cstr(temp))
这样就应该是显示“西南”了,我去vb.net看看有没有这个函数