0

用户可以从一个主应用程序启动多个 InMemory 程序集实例。问题是 InMemory 程序集中的公共变量似乎相互覆盖。有没有办法来解决这个问题?

我还尝试将 Namespace Evaluator 和 Class Evaluator 重命名为每个实例的不同名称,但这并没有帮助。

Namespace Evaluator

    Public Module GlobalSettings
        Public Var1 As String
        Public Var2 As String
        Public TextBox1 As New TextBox
        Public TextBox2 As New TextBox
    End Module

    Public Class Evaluator

        '...insert code here

    End Class

End Namespace

Friend Function CompileCode() As Object
    Dim compResults As CompilerResults
    Dim code = GetCode()

    Dim provOptions As New Dictionary(Of String, String)
    provOptions.Add("CompilerVersion", "v3.5")

    Dim vbProv = New VBCodeProvider(provOptions)

    Dim vbParams = New CompilerParameters()
    vbParams.ReferencedAssemblies.Add("mscorlib.dll")
    vbParams.ReferencedAssemblies.Add("System.dll")
    vbParams.ReferencedAssemblies.Add("System.Windows.Forms.dll")
    vbParams.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll")
    vbParams.ReferencedAssemblies.Add("System.Data.dll")

    vbParams.TreatWarningsAsErrors = False
    vbParams.GenerateExecutable = False
    vbParams.GenerateInMemory = True

    compResults = vbProv.CompileAssemblyFromSource(vbParams, code)

     _evaluator = compResults.CompiledAssembly.CreateInstance("Evaluator.Evaluator")

End Function
4

1 回答 1

0

经过大量的试验和测试,我找到了解决方案。它与公共变量无关,并且更改每个实例的命名空间和类名称也不起作用,每个代码程序集仍在来回共享信息。所以答案... 因为多个实例是从同一个父应用程序调用的,所以 _evaluator 对象需要独立于编译器代码和调用代码中的每个实例。

我最终允许从 Windows 应用程序同时启动 5 个实例。如果一个实例已关闭,则程序将下一个调用的实例分配给我存储在公共 List(Of...) 配对值中的可用 id。该代码未在此处显示如何分配和删除。

Dim _evaluator1 As Object
Dim _evaluator2 As Object
Dim _evaluator3 As Object
Dim _evaluator4 As Object
Dim _evaluator5 As Object

Friend Function CompileCode() As Object
   Dim compResults As CompilerResults
   Dim code = GetCode()

   Dim provOptions As New Dictionary(Of String, String)
   provOptions.Add("CompilerVersion", "v3.5")

   Dim vbProv = New VBCodeProvider(provOptions)

   Dim vbParams = New CompilerParameters()
   vbParams.ReferencedAssemblies.Add("mscorlib.dll")
   vbParams.ReferencedAssemblies.Add("System.dll")
   vbParams.ReferencedAssemblies.Add("System.Windows.Forms.dll")
   vbParams.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll")
   vbParams.ReferencedAssemblies.Add("System.Data.dll")

   vbParams.TreatWarningsAsErrors = False
   vbParams.GenerateExecutable = False
   vbParams.GenerateInMemory = True

   compResults = vbProv.CompileAssemblyFromSource(vbParams, code)

     Select Case CurrentInstance
         Case 1 : _evaluator1 = compResults.CompiledAssembly.CreateInstance("Evaluator.Evaluator")
         Case 2 : _evaluator2 = compResults.CompiledAssembly.CreateInstance("Evaluator.Evaluator")
         Case 3 : _evaluator3 = compResults.CompiledAssembly.CreateInstance("Evaluator.Evaluator")
         Case 4 : _evaluator4 = compResults.CompiledAssembly.CreateInstance("Evaluator.Evaluator")
         Case 5 : _evaluator5 = compResults.CompiledAssembly.CreateInstance("Evaluator.Evaluator")
     End Select

End Function

调用

  Dim objResult As Object

        Select Case CurrentInstance
            Case 1 : objResult = _evaluator1.GetType.InvokeMember(name, BindingFlags.DeclaredOnly Or BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.InvokeMethod, Nothing, _evaluator1, args)
            Case 2 : objResult = _evaluator2.GetType.InvokeMember(name, BindingFlags.DeclaredOnly Or BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.InvokeMethod, Nothing, _evaluator2, args)
            Case 3 : objResult = _evaluator3.GetType.InvokeMember(name, BindingFlags.DeclaredOnly Or BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.InvokeMethod, Nothing, _evaluator3, args)
            Case 4 : objResult = _evaluator4.GetType.InvokeMember(name, BindingFlags.DeclaredOnly Or BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.InvokeMethod, Nothing, _evaluator4, args)
            Case 5 : objResult = _evaluator5.GetType.InvokeMember(name, BindingFlags.DeclaredOnly Or BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.InvokeMethod, Nothing, _evaluator5, args)
        End Select
于 2013-07-25T15:47:47.447 回答