本文整理汇总了VB.NET中System.Reflection.Emit.DynamicMethod类的典型用法代码示例。如果您正苦于以下问题:VB.NET DynamicMethod类的具体用法?VB.NET DynamicMethod怎么用?VB.NET DynamicMethod使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DynamicMethod类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的VB.NET代码示例。
示例1: Test
' 导入命名空间
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Globalization
Public Class Test
' Declare a delegate type that can be used to execute the completed
' dynamic method.
Private Delegate Function HelloDelegate(ByVal msg As String, _
ByVal ret As Integer) As Integer
Public Shared Sub Main()
' Create an array that specifies the types of the parameters
' of the dynamic method. This dynamic method has a String
' parameter and an Integer parameter.
Dim helloArgs() As Type = {GetType(String), GetType(Integer)}
' Create a dynamic method with the name "Hello", a return type
' of Integer, and two parameters whose types are specified by
' the array helloArgs. Create the method in the module that
' defines the String class.
Dim hello As New DynamicMethod("Hello", _
GetType(Integer), _
helloArgs, _
GetType(String).Module)
' Create an array that specifies the parameter types of the
' overload of Console.WriteLine to be used in Hello.
Dim writeStringArgs() As Type = {GetType(String)}
' Get the overload of Console.WriteLine that has one
' String parameter.
Dim writeString As MethodInfo = GetType(Console). _
GetMethod("WriteLine", writeStringArgs)
' Get an ILGenerator and emit a body for the dynamic method,
' using a stream size larger than the IL that will be
' emitted.
Dim il As ILGenerator = hello.GetILGenerator(256)
' Load the first argument, which is a string, onto the stack.
il.Emit(OpCodes.Ldarg_0)
' Call the overload of Console.WriteLine that prints a string.
il.EmitCall(OpCodes.Call, writeString, Nothing)
' The Hello method returns the value of the second argument;
' to do this, load the onto the stack and return.
il.Emit(OpCodes.Ldarg_1)
il.Emit(OpCodes.Ret)
' Add parameter information to the dynamic method. (This is not
' necessary, but can be useful for debugging.) For each parameter,
' identified by position, supply the parameter attributes and a
' parameter name.
hello.DefineParameter(1, ParameterAttributes.In, "message")
hello.DefineParameter(2, ParameterAttributes.In, "valueToReturn")
' Create a delegate that represents the dynamic method. This
' action completes the method. Any further attempts to
' change the method are ignored.
Dim hi As HelloDelegate = _
CType(hello.CreateDelegate(GetType(HelloDelegate)), HelloDelegate)
' Use the delegate to execute the dynamic method.
Console.WriteLine(vbCrLf & "Use the delegate to execute the dynamic method:")
Dim retval As Integer = hi(vbCrLf & "Hello, World!", 42)
Console.WriteLine("Invoking delegate hi(""Hello, World!"", 42) returned: " _
& retval & ".")
' Execute it again, with different arguments.
retval = hi(vbCrLf & "Hi, Mom!", 5280)
Console.WriteLine("Invoking delegate hi(""Hi, Mom!"", 5280) returned: " _
& retval & ".")
Console.WriteLine(vbCrLf & "Use the Invoke method to execute the dynamic method:")
' Create an array of arguments to use with the Invoke method.
Dim invokeArgs() As Object = {vbCrLf & "Hello, World!", 42}
' Invoke the dynamic method using the arguments. This is much
' slower than using the delegate, because you must create an
' array to contain the arguments, and value-type arguments
' must be boxed.
Dim objRet As Object = hello.Invoke(Nothing, _
BindingFlags.ExactBinding, Nothing, invokeArgs, _
New CultureInfo("en-us"))
Console.WriteLine("hello.Invoke returned: {0}", objRet)
Console.WriteLine(vbCrLf & _
" ----- Display information about the dynamic method -----")
' Display MethodAttributes for the dynamic method, set when
' the dynamic method was created.
Console.WriteLine(vbCrLf & "Method Attributes: {0}", _
hello.Attributes)
' Display the calling convention of the dynamic method, set when the
' dynamic method was created.
Console.WriteLine(vbCrLf & "Calling convention: {0}", _
hello.CallingConvention)
' Display the declaring type, which is always Nothing for dynamic
' methods.
If hello.DeclaringType Is Nothing Then
Console.WriteLine(vbCrLf & "DeclaringType is always Nothing for dynamic methods.")
Else
Console.WriteLine("DeclaringType: {0}", hello.DeclaringType)
End If
' Display the default value for InitLocals.
If hello.InitLocals Then
Console.Write(vbCrLf & "This method contains verifiable code.")
Else
Console.Write(vbCrLf & "This method contains unverifiable code.")
End If
Console.WriteLine(" (InitLocals = {0})", hello.InitLocals)
' Display the module specified when the dynamic method was created.
Console.WriteLine(vbCrLf & "Module: {0}", hello.Module)
' Display the name specified when the dynamic method was created.
' Note that the name can be blank.
Console.WriteLine(vbCrLf & "Name: {0}", hello.Name)
' For dynamic methods, the reflected type is always Nothing.
If hello.ReflectedType Is Nothing Then
Console.WriteLine(vbCrLf & "ReflectedType is Nothing.")
Else
Console.WriteLine(vbCrLf & "ReflectedType: {0}", _
hello.ReflectedType)
End If
If hello.ReturnParameter Is Nothing Then
Console.WriteLine(vbCrLf & "Method has no return parameter.")
Else
Console.WriteLine(vbCrLf & "Return parameter: {0}", _
hello.ReturnParameter)
End If
' If the method has no return type, ReturnType is System.Void.
Console.WriteLine(vbCrLf & "Return type: {0}", hello.ReturnType)
' ReturnTypeCustomAttributes returns an ICustomeAttributeProvider
' that can be used to enumerate the custom attributes of the
' return value. At present, there is no way to set such custom
' attributes, so the list is empty.
If hello.ReturnType Is GetType(System.Void) Then
Console.WriteLine("The method has no return type.")
Else
Dim caProvider As ICustomAttributeProvider = _
hello.ReturnTypeCustomAttributes
Dim returnAttributes() As Object = _
caProvider.GetCustomAttributes(True)
If returnAttributes.Length = 0 Then
Console.WriteLine(vbCrLf _
& "The return type has no custom attributes.")
Else
Console.WriteLine(vbCrLf _
& "The return type has the following custom attributes:")
For Each attr As Object In returnAttributes
Console.WriteLine(vbTab & attr.ToString())
Next attr
End If
End If
Console.WriteLine(vbCrLf & "ToString: " & hello.ToString())
' Display parameter information.
Dim parameters() As ParameterInfo = hello.GetParameters()
Console.WriteLine(vbCrLf & "Parameters: name, type, ParameterAttributes")
For Each p As ParameterInfo In parameters
Console.WriteLine(vbTab & "{0}, {1}, {2}", _
p.Name, p.ParameterType, p.Attributes)
Next p
End Sub
End Class
开发者ID:VB.NET开发者,项目名称:System.Reflection.Emit,代码行数:170,代码来源:DynamicMethod 输出:
Use the delegate to execute the dynamic method:
Hello, World!
Invoking delegate hi("Hello, World!", 42) returned: 42.
Hi, Mom!
Invoking delegate hi("Hi, Mom!", 5280) returned: 5280.
Use the Invoke method to execute the dynamic method:
Hello, World!
hello.Invoke returned: 42
----- Display information about the dynamic method -----
Method Attributes: PrivateScope, Public, Static
Calling convention: Standard
DeclaringType is always Nothing for dynamic methods.
This method contains verifiable code. (InitLocals = True)
Module: CommonLanguageRuntimeLibrary
Name: Hello
ReflectedType is Nothing.
Method has no return parameter.
Return type: System.Int32
The return type has no custom attributes.
ToString: Int32 Hello(System.String, Int32)
Parameters: name, type, ParameterAttributes
message, System.String, In
valueToReturn, System.Int32, In
注:本文中的System.Reflection.Emit.DynamicMethod类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论