本文整理匯總了VB.NET中System.AppDomain.CreateInstanceAndUnwrap方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET AppDomain.CreateInstanceAndUnwrap方法的具體用法?VB.NET AppDomain.CreateInstanceAndUnwrap怎麽用?VB.NET AppDomain.CreateInstanceAndUnwrap使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.AppDomain
的用法示例。
在下文中一共展示了AppDomain.CreateInstanceAndUnwrap方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Worker
' 導入命名空間
Imports System.Reflection
Public Class Worker
Inherits MarshalByRefObject
Public Sub PrintDomain()
Console.WriteLine("Object is executing in AppDomain ""{0}""", _
AppDomain.CurrentDomain.FriendlyName)
End Sub
End Class
Class Example
Public Shared Sub Main()
' Create an ordinary instance in the current AppDomain
Dim localWorker As New Worker()
localWorker.PrintDomain()
' Create a new application domain, create an instance
' of Worker in the application domain, and execute code
' there.
Dim ad As AppDomain = AppDomain.CreateDomain("New domain")
Dim remoteWorker As Worker = CType( _
ad.CreateInstanceAndUnwrap( _
GetType(Worker).Assembly.FullName, _
"Worker"), _
Worker)
remoteWorker.PrintDomain()
End Sub
End Class
輸出:
Object is executing in AppDomain "source.exe" Object is executing in AppDomain "New domain"
示例2: ADDyno
' 導入命名空間
Imports System.IO
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Runtime.Remoting
Class ADDyno
Public Shared Function CreateADynamicAssembly(ByRef myNewDomain As AppDomain, executableNameNoExe As String) As Type
Dim executableName As String = executableNameNoExe + ".exe"
Dim myAsmName As New AssemblyName()
myAsmName.Name = executableNameNoExe
myAsmName.CodeBase = Environment.CurrentDirectory
Dim myAsmBuilder As AssemblyBuilder = myNewDomain.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.RunAndSave)
Console.WriteLine("-- Dynamic Assembly instantiated.")
Dim myModBuilder As ModuleBuilder = myAsmBuilder.DefineDynamicModule(executableNameNoExe, executableName)
Dim myTypeBuilder As TypeBuilder = myModBuilder.DefineType(executableNameNoExe, TypeAttributes.Public, GetType(MarshalByRefObject))
Dim myFCMethod As MethodBuilder = myTypeBuilder.DefineMethod("CountLocalFiles", MethodAttributes.Public Or MethodAttributes.Static, Nothing, New Type() {})
Dim currentDirGetMI As MethodInfo = GetType(Environment).GetProperty("CurrentDirectory").GetGetMethod()
Dim writeLine0objMI As MethodInfo = GetType(Console).GetMethod("WriteLine", New Type() {GetType(String)})
Dim writeLine2objMI As MethodInfo = GetType(Console).GetMethod("WriteLine", New Type() {GetType(String), GetType(Object), GetType(Object)})
Dim getFilesMI As MethodInfo = GetType(Directory).GetMethod("GetFiles", New Type() {GetType(String)})
myFCMethod.InitLocals = True
Dim myFCIL As ILGenerator = myFCMethod.GetILGenerator()
Console.WriteLine("-- Generating MSIL method body...")
Dim v0 As LocalBuilder = myFCIL.DeclareLocal(GetType(String))
Dim v1 As LocalBuilder = myFCIL.DeclareLocal(GetType(Integer))
Dim v2 As LocalBuilder = myFCIL.DeclareLocal(GetType(String))
Dim v3 As LocalBuilder = myFCIL.DeclareLocal(GetType(String()))
Dim evalForEachLabel As Label = myFCIL.DefineLabel()
Dim topOfForEachLabel As Label = myFCIL.DefineLabel()
' Build the method body.
myFCIL.EmitCall(OpCodes.Call, currentDirGetMI, Nothing)
myFCIL.Emit(OpCodes.Stloc_S, v0)
myFCIL.Emit(OpCodes.Ldc_I4_0)
myFCIL.Emit(OpCodes.Stloc_S, v1)
myFCIL.Emit(OpCodes.Ldstr, "---")
myFCIL.EmitCall(OpCodes.Call, writeLine0objMI, Nothing)
myFCIL.Emit(OpCodes.Ldloc_S, v0)
myFCIL.EmitCall(OpCodes.Call, getFilesMI, Nothing)
myFCIL.Emit(OpCodes.Stloc_S, v3)
myFCIL.Emit(OpCodes.Br_S, evalForEachLabel)
' foreach loop starts here.
myFCIL.MarkLabel(topOfForEachLabel)
' Load array of strings and index, store value at index for output.
myFCIL.Emit(OpCodes.Ldloc_S, v3)
myFCIL.Emit(OpCodes.Ldloc_S, v1)
myFCIL.Emit(OpCodes.Ldelem_Ref)
myFCIL.Emit(OpCodes.Stloc_S, v2)
myFCIL.Emit(OpCodes.Ldloc_S, v2)
myFCIL.EmitCall(OpCodes.Call, writeLine0objMI, Nothing)
' Increment counter by one.
myFCIL.Emit(OpCodes.Ldloc_S, v1)
myFCIL.Emit(OpCodes.Ldc_I4_1)
myFCIL.Emit(OpCodes.Add)
myFCIL.Emit(OpCodes.Stloc_S, v1)
' Determine if end of file list array has been reached.
myFCIL.MarkLabel(evalForEachLabel)
myFCIL.Emit(OpCodes.Ldloc_S, v1)
myFCIL.Emit(OpCodes.Ldloc_S, v3)
myFCIL.Emit(OpCodes.Ldlen)
myFCIL.Emit(OpCodes.Conv_I4)
myFCIL.Emit(OpCodes.Blt_S, topOfForEachLabel)
'foreach loop end here.
myFCIL.Emit(OpCodes.Ldstr, "---")
myFCIL.EmitCall(OpCodes.Call, writeLine0objMI, Nothing)
myFCIL.Emit(OpCodes.Ldstr, "There are {0} files in {1}.")
myFCIL.Emit(OpCodes.Ldloc_S, v1)
myFCIL.Emit(OpCodes.Box, GetType(Integer))
myFCIL.Emit(OpCodes.Ldloc_S, v0)
myFCIL.EmitCall(OpCodes.Call, writeLine2objMI, Nothing)
myFCIL.Emit(OpCodes.Ret)
Dim myType As Type = myTypeBuilder.CreateType()
myAsmBuilder.SetEntryPoint(myFCMethod)
myAsmBuilder.Save(executableName)
Console.WriteLine("-- Method generated, type completed, and assembly saved to disk.")
Return myType
End Function 'CreateADynamicAssembly
Public Shared Sub Main()
Dim executableName As String = Nothing
Dim domainDir As String
Console.Write("Enter a name for the file counting assembly: ")
Dim executableNameNoExe As String = Console.ReadLine()
executableName = executableNameNoExe + ".exe"
Console.WriteLine("---")
domainDir = Environment.CurrentDirectory
Dim curDomain As AppDomain = Thread.GetDomain()
' Create a new AppDomain, with the current directory as the base.
Console.WriteLine("Current Directory: {0}", Environment.CurrentDirectory)
Dim mySetupInfo As New AppDomainSetup()
mySetupInfo.ApplicationBase = domainDir
mySetupInfo.ApplicationName = executableNameNoExe
mySetupInfo.LoaderOptimization = LoaderOptimization.SingleDomain
Dim myDomain As AppDomain = AppDomain.CreateDomain(executableNameNoExe, Nothing, mySetupInfo)
Console.WriteLine("Creating a new AppDomain '{0}'...", executableNameNoExe)
Console.WriteLine("-- Base Directory = '{0}'", myDomain.BaseDirectory)
Console.WriteLine("-- Shadow Copy? = '{0}'", myDomain.ShadowCopyFiles)
Console.WriteLine("---")
Dim myFCType As Type = CreateADynamicAssembly(curDomain, executableNameNoExe)
Console.WriteLine("Loading '{0}' from '{1}'...", executableName, myDomain.BaseDirectory.ToString())
Dim bFlags As BindingFlags = BindingFlags.Public Or BindingFlags.CreateInstance Or BindingFlags.Instance
Dim myObjInstance As [Object] = myDomain.CreateInstanceAndUnwrap(executableNameNoExe, executableNameNoExe, False, bFlags, Nothing, Nothing, Nothing, Nothing, Nothing)
Console.WriteLine("Executing method 'CountLocalFiles' in {0}...", myObjInstance.ToString())
myFCType.InvokeMember("CountLocalFiles", BindingFlags.InvokeMethod, Nothing, myObjInstance, New Object() {})
End Sub
End Class
示例3: Test
' 導入命名空間
Imports System.Reflection
Module Test
Sub Main()
InstantiateINT32(False) ' Failed!
InstantiateINT32(True) ' OK!
End Sub
Sub InstantiateINT32(ignoreCase As Boolean)
Try
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
Dim instance As Object = currentDomain.CreateInstanceAndUnwrap( _
"mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", _
"SYSTEM.INT32", _
ignoreCase, _
BindingFlags.Default, _
Nothing, _
Nothing, _
Nothing, _
Nothing, _
Nothing _
)
Console.WriteLine(instance.GetType())
Catch e As TypeLoadException
Console.WriteLine(e.Message)
End Try
End Sub
End Module 'Test