Assembly 1に次のコードがあります
Public Interface ITest
Sub Run()
End Interface
Public Class Runner
Inherits MarshalByRefObject
Public Sub Run(pluginPath As String)
For Each assemblyPath As String In Directory.GetFiles(pluginPath, "*.dll", SearchOption.AllDirectories)
ScanAssemblyForTasks(assemblyPath)
Next
End Sub
Private Sub ScanAssemblyForTasks(assemblyPath As String)
Dim assembly As Assembly = assembly.LoadFile(assemblyPath)
For Each type As Type In assembly.GetTypes().Where(Function(t) t.IsClass AndAlso
Not t.IsAbstract AndAlso
Not t.IsGenericType AndAlso
GetType(ITest).IsAssignableFrom(t))
Dim test As ITest = CType(type.GetConstructor(type.EmptyTypes).Invoke(Nothing), ITest)
test.Run()
Next
End Sub
End Class
Module Module1
Private mAppDomain As AppDomain
Private mCachePath As String
Private mPluginDirectory As String
Private mRunner As Runner
Sub Main()
mCachePath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "ShadowCache")
mPluginDirectory = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "Assemblies")
If (Not Directory.Exists(mCachePath)) Then
Directory.CreateDirectory(mCachePath)
End If
If (Not Directory.Exists(mPluginDirectory)) Then
Directory.CreateDirectory(mPluginDirectory)
End If
Console.WriteLine("Press any key to load assemblies")
Console.ReadKey()
Dim setup As New AppDomainSetup()
setup.ApplicationName = "MyApplication"
setup.ShadowCopyFiles = "true"
setup.CachePath = mCachePath
setup.ShadowCopyDirectories = mPluginDirectory
setup.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
mAppDomain = AppDomain.CreateDomain("MyApplication", AppDomain.CurrentDomain.Evidence, setup)
mRunner = CType(mAppDomain.CreateInstanceAndUnwrap(GetType(Runner).Assembly.FullName, GetType(Runner).FullName), Runner)
mRunner.Run(mPluginDirectory)
Console.WriteLine("Press any key to end application")
Console.ReadKey()
End Sub
End Module
mPluginDirectory
そして、私にあるAssembly2には次のコードがあります
Public Class Class1
Implements ITest
Public Sub Run() Implements ITest.Run
Console.WriteLine("Run")
End Sub
End Class
アプリケーションを実行すると、Assembly2 がアプリケーション ドメインに読み込まれ、「Run」が出力されますが、ディレクトリmCachePath
が空で、アプリケーションの実行中に Assembly2.dll を変更/削除できません。.net でシャドウを使用しないのはなぜですか使用するように言ったときのコピー機能。