本文整理匯總了VB.NET中System.Reflection.Emit.UnmanagedMarshal類的典型用法代碼示例。如果您正苦於以下問題:VB.NET UnmanagedMarshal類的具體用法?VB.NET UnmanagedMarshal怎麽用?VB.NET UnmanagedMarshal使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了UnmanagedMarshal類的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Main
' 導入命名空間
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Runtime.InteropServices
Public Class Example
Public Shared Sub Main()
Dim myDomain As AppDomain = AppDomain.CurrentDomain
Dim myAsmName As New AssemblyName("EmitMarshalAs")
Dim myAssembly As AssemblyBuilder = _
myDomain.DefineDynamicAssembly(myAsmName, _
AssemblyBuilderAccess.RunAndSave)
Dim myModule As ModuleBuilder = _
myAssembly.DefineDynamicModule(myAsmName.Name, _
myAsmName.Name & ".dll")
Dim myType As TypeBuilder = _
myModule.DefineType("Sample", TypeAttributes.Public)
Dim myMethod As MethodBuilder = _
myType.DefineMethod("Test", MethodAttributes.Public, _
Nothing, new Type() { GetType(String) })
' Get a parameter builder for the parameter that needs the
' attribute, using the HasFieldMarshal attribute. In this
' example, the parameter is at position 0 and has the name
' "arg".
Dim pb As ParameterBuilder = _
myMethod.DefineParameter(0, _
ParameterAttributes.HasFieldMarshal, "arg")
' Get the MarshalAsAttribute constructor that takes an
' argument of type UnmanagedType.
'
Dim ciParameters() As Type = { GetType(UnmanagedType) }
Dim ci As ConstructorInfo = _
GetType(MarshalAsAttribute).GetConstructor(ciParameters)
' Create a CustomAttributeBuilder representing the attribute,
' specifying the necessary unmanaged type. In this case,
' UnmanagedType.BStr is specified.
'
Dim ciArguments() As Object = { UnmanagedType.BStr }
Dim cabuilder As New CustomAttributeBuilder(ci, ciArguments)
' Apply the attribute to the parameter.
'
pb.SetCustomAttribute(cabuilder)
' Emit a dummy method body.
Dim il As ILGenerator = myMethod.GetILGenerator()
il.Emit(OpCodes.Ret)
myType.CreateType()
myAssembly.Save(myAsmName.Name & ".dll")
End Sub
End Class