本文整理汇总了VB.NET中System.AppDomain.TypeResolve事件的典型用法代码示例。如果您正苦于以下问题:VB.NET AppDomain.TypeResolve事件的具体用法?VB.NET AppDomain.TypeResolve怎么用?VB.NET AppDomain.TypeResolve使用的例子?那么, 这里精选的事件代码示例或许可以为您提供帮助。您也可以进一步了解该事件所在类System.AppDomain
的用法示例。
在下文中一共展示了AppDomain.TypeResolve事件的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Test
Option Strict On
Option Explicit On
Imports System.Reflection
Imports System.Reflection.Emit
Module Test
' For this code example, the following information needs to be
' available to both Main and the HandleTypeResolve event
' handler:
Private ab As AssemblyBuilder
Private moduleName As String
Sub Main()
Dim currDom As AppDomain = AppDomain.CurrentDomain
' Create a dynamic assembly with one module, to be saved to
' disk (AssemblyBuilderAccess.Save).
'
Dim aName As AssemblyName = new AssemblyName()
aName.Name = "Transient"
moduleName = aName.Name + ".dll"
ab = currDom.DefineDynamicAssembly(aName, _
AssemblyBuilderAccess.Save)
Dim mb As ModuleBuilder = _
ab.DefineDynamicModule(aName.Name, moduleName)
' The dynamic assembly has just one dummy type, to demonstrate
' type resolution.
Dim tb As TypeBuilder = mb.DefineType("Example")
tb.CreateType()
' First, try to load the type without saving the dynamic
' assembly and without hooking up the TypeResolve event. The
' type cannot be loaded.
Try
Dim temp As Type = Type.GetType("Example", true)
Console.WriteLine("Loaded type {0}.", temp)
Catch ex As TypeLoadException
Console.WriteLine("Loader could not resolve the type.")
End Try
' Hook up the TypeResolve event.
'
AddHandler currDom.TypeResolve, AddressOf HandleTypeResolve
' Now try to load the type again. The TypeResolve event is
' raised, the dynamic assembly is saved, and the dummy type is
' loaded successfully. Display it to the console, and create
' an instance.
Dim t As Type = Type.GetType("Example", true)
Console.WriteLine("Loaded type ""{0}"".", t)
Dim o As Object = Activator.CreateInstance(t)
End Sub
Private Function HandleTypeResolve(ByVal sender As Object, _
ByVal e As ResolveEventArgs) As [Assembly]
Console.WriteLine("TypeResolve event handler.")
' Save the dynamic assembly, and then load it using its
' display name. Return the loaded assembly.
'
ab.Save(moduleName)
Return [Assembly].Load(ab.FullName)
End Function
End Module
输出:
Loader could not resolve the type. TypeResolve event handler. Loaded type "Example".