本文整理汇总了VB.NET中System.Reflection.Emit.AssemblyBuilder.SetCustomAttribute方法的典型用法代码示例。如果您正苦于以下问题:VB.NET AssemblyBuilder.SetCustomAttribute方法的具体用法?VB.NET AssemblyBuilder.SetCustomAttribute怎么用?VB.NET AssemblyBuilder.SetCustomAttribute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.Emit.AssemblyBuilder
的用法示例。
在下文中一共展示了AssemblyBuilder.SetCustomAttribute方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: MyAttribute
<AttributeUsage(AttributeTargets.All, AllowMultiple := False)> _
Public Class MyAttribute
Inherits Attribute
Public s As String
Public x As Integer
Public Sub New(s As String, x As Integer)
Me.s = s
Me.x = x
End Sub
End Class
Class MyApplication
Public Shared Sub Main()
Dim customAttribute As Type = CreateCallee(Thread.GetDomain())
Dim attributes As Object() = customAttribute.Assembly.GetCustomAttributes(True)
Console.WriteLine("MyAttribute custom attribute contains : ")
Dim index As Integer
For index = 0 To attributes.Length - 1
If TypeOf attributes(index) Is MyAttribute Then
Console.WriteLine("s : " + CType(attributes(index), MyAttribute).s)
Console.WriteLine("x : " + CType(attributes(index), MyAttribute).x.ToString())
Exit For
End If
Next index
End Sub
Private Shared Function CreateCallee(domain As AppDomain) As Type
Dim myAssemblyName As New AssemblyName()
myAssemblyName.Name = "EmittedAssembly"
Dim myAssembly As AssemblyBuilder = _
domain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run)
Dim myType As Type = GetType(MyAttribute)
Dim infoConstructor As ConstructorInfo = _
myType.GetConstructor(New Type(1) {GetType(String), GetType(Integer)})
Dim attributeBuilder As New CustomAttributeBuilder(infoConstructor, New Object(1) {"Hello", 2})
myAssembly.SetCustomAttribute(attributeBuilder)
Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule")
' Define a public class named "HelloWorld" in the assembly.
Dim helloWorldClass As TypeBuilder = myModule.DefineType("HelloWorld", TypeAttributes.Public)
Return helloWorldClass.CreateType()
End Function 'CreateCallee
End Class
示例2: MyAttribute
' 导入命名空间
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
<AttributeUsage(AttributeTargets.All, AllowMultiple := False)> _
Public Class MyAttribute
Inherits Attribute
Public s As Boolean
Public Sub New(s As Boolean)
Me.s = s
End Sub
End Class
Class MyApplication
Public Shared Sub Main()
Dim customAttribute As Type = CreateCallee(Thread.GetDomain())
Dim attributes As Object() = customAttribute.Assembly.GetCustomAttributes(True)
Console.WriteLine("MyAttribute custom attribute contains : ")
Dim index As Integer
For index = 0 To attributes.Length - 1
If TypeOf attributes(index) Is MyAttribute Then
Console.WriteLine("s : " + CType(attributes(index), MyAttribute).s.ToString())
Exit For
End If
Next index
End Sub
Private Shared Function CreateCallee(domain As AppDomain) As Type
Dim myAssemblyName As New AssemblyName()
myAssemblyName.Name = "EmittedAssembly"
Dim myAssembly As AssemblyBuilder = domain.DefineDynamicAssembly(myAssemblyName, _
AssemblyBuilderAccess.Run)
Dim myType As Type = GetType(MyAttribute)
Dim infoConstructor As ConstructorInfo = myType.GetConstructor(New Type() {GetType(Boolean)})
myAssembly.SetCustomAttribute(infoConstructor, New Byte() {01, 00, 01})
Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule")
' Define a public class named "HelloWorld" in the assembly.
Dim helloWorldClass As TypeBuilder = myModule.DefineType("HelloWorld", TypeAttributes.Public)
Return helloWorldClass.CreateType()
End Function 'CreateCallee
End Class