本文整理汇总了VB.NET中System.Reflection.Emit.TypeBuilder.DefineMethod方法的典型用法代码示例。如果您正苦于以下问题:VB.NET TypeBuilder.DefineMethod方法的具体用法?VB.NET TypeBuilder.DefineMethod怎么用?VB.NET TypeBuilder.DefineMethod使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.Emit.TypeBuilder
的用法示例。
在下文中一共展示了TypeBuilder.DefineMethod方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1:
' Define a Shared, Public method with standard calling
' conventions. Do not specify the parameter types or the
' return type, because type parameters will be used for
' those types, and the type parameters have not been
' defined yet.
Dim demoMethod As MethodBuilder = _
demoType.DefineMethod("DemoMethod", _
MethodAttributes.Public Or MethodAttributes.Static)
示例2: typeParamNames
' Defining generic parameters for the method makes it a
' generic method. By convention, type parameters are
' single alphabetic characters. T and U are used here.
'
Dim typeParamNames() As String = {"T", "U"}
Dim typeParameters() As GenericTypeParameterBuilder = _
demoMethod.DefineGenericParameters(typeParamNames)
' The second type parameter is constrained to be a
' reference type.
typeParameters(1).SetGenericParameterAttributes( _
GenericParameterAttributes.ReferenceTypeConstraint)
示例3: params
' Set parameter types for the method. The method takes
' one parameter, and its type is specified by the first
' type parameter, T.
Dim params() As Type = {typeParameters(0)}
demoMethod.SetParameters(params)
' Set the return type for the method. The return type is
' specified by the second type parameter, U.
demoMethod.SetReturnType(typeParameters(1))
示例4: Example
' 导入命名空间
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Security.Permissions
Public Interface IMyInterface
Function HelloMethod(parameter As String) As String
End Interface 'IMyInterface
Public Class Example
<PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
Public Shared Sub Main()
Dim myNestedClassType As Type = CreateCallee(Thread.GetDomain())
' Create an instance of 'MyNestedClass'.
Dim myInterface As IMyInterface = _
CType(Activator.CreateInstance(myNestedClassType), IMyInterface)
Console.WriteLine(myInterface.HelloMethod("Bill"))
End Sub
' Create the callee transient dynamic assembly.
Private Shared Function CreateCallee(myAppDomain As AppDomain) As Type
Dim myAssemblyName As New AssemblyName()
myAssemblyName.Name = "Example"
' Create the callee dynamic assembly.
Dim myAssembly As AssemblyBuilder = _
myAppDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run)
' Create a dynamic module in the callee assembly.
Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule")
' Define a public class named "MyHelloWorld".
Dim myHelloWorldType As TypeBuilder = _
myModule.DefineType("MyHelloWorld", TypeAttributes.Public)
' Define a public nested class named 'MyNestedClass'.
Dim myNestedClassType As TypeBuilder = _
myHelloWorldType.DefineNestedType("MyNestedClass", TypeAttributes.NestedPublic, _
GetType(Example), New Type() {GetType(IMyInterface)})
' Implement 'IMyInterface' interface.
myNestedClassType.AddInterfaceImplementation(GetType(IMyInterface))
' Define 'HelloMethod' of 'IMyInterface'.
Dim myHelloMethod As MethodBuilder = _
myNestedClassType.DefineMethod("HelloMethod", MethodAttributes.Public Or _
MethodAttributes.Virtual, GetType(String), New Type() {GetType(String)})
' Generate IL for 'GetGreeting' method.
Dim myMethodIL As ILGenerator = myHelloMethod.GetILGenerator()
myMethodIL.Emit(OpCodes.Ldstr, "Hi! ")
myMethodIL.Emit(OpCodes.Ldarg_1)
Dim infoMethod As MethodInfo = _
GetType(String).GetMethod("Concat", New Type() {GetType(String), GetType(String)})
myMethodIL.Emit(OpCodes.Call, infoMethod)
myMethodIL.Emit(OpCodes.Ret)
Dim myHelloMethodInfo As MethodInfo = GetType(IMyInterface).GetMethod("HelloMethod")
' Implement 'HelloMethod' of 'IMyInterface'.
myNestedClassType.DefineMethodOverride(myHelloMethod, myHelloMethodInfo)
' Create 'MyHelloWorld' type.
Dim myType As Type = myHelloWorldType.CreateType()
' Create 'MyNestedClass' type.
Return myNestedClassType.CreateType()
End Function 'CreateCallee
End Class