Vb.net 反序列化动态编译代码的程序集时出错
我试图序列化动态生成/编译的程序集以存储到SQL中,然后提取该程序集以供使用。然而,我遇到了一个让我发疯的错误 无法加载文件或程序集、版本=0.0.0.0、区域性=中性、PublicKeyToken=null或其依赖项之一。系统找不到指定的文件 当我尝试反序列化程序集时。我会很感激你的指点。部分代码:Vb.net 反序列化动态编译代码的程序集时出错,vb.net,.net-assembly,binary-deserialization,Vb.net,.net Assembly,Binary Deserialization,我试图序列化动态生成/编译的程序集以存储到SQL中,然后提取该程序集以供使用。然而,我遇到了一个让我发疯的错误 无法加载文件或程序集、版本=0.0.0.0、区域性=中性、PublicKeyToken=null或其依赖项之一。系统找不到指定的文件 当我尝试反序列化程序集时。我会很感激你的指点。部分代码: '-------------------------------------------------------------- 'This section SUCCESSFULLY
'--------------------------------------------------------------
'This section SUCCESSFULLY Compile & Execute the Dynamic Code
' vsResult returns ok.
' **Note: This is partial code to create the compiler.
'--------------------------------------------------------------
Dim voCompileResults As System.CodeDom.Compiler.CompilerResults = voCompiler.CompileAssemblyFromSource(voCompilerParams, sb.ToString)
Dim voAssembly As Reflection.Assembly = voCompileResults.CompiledAssembly
Dim voInstance As Object = Activator.CreateInstance(voAssembly.GetType("libARules.clsMyRoutine"), {New libARules.Sys.clsInterProcessData(New System.Xml.XmlDocument), New libArrowOps.clsDBAccess})
Dim vsResult As String = voInstance.GetType().InvokeMember("Run", Reflection.BindingFlags.InvokeMethod, Nothing, voInstance, Nothing)
'----------------------------------------------------
'Attempt to Serialize the Assembly for store/forward
'----------------------------------------------------
Dim voMemStream As New IO.MemoryStream
Dim voBF As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
voBF.AssemblyFormat = Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple
voBF.Serialize(voMemStream, voCompileResults.CompiledAssembly)
'--------------------------------------
'Reset the MemoryStream position to begining
'--------------------------------------
voMemStream.Position = 0
'--------------------------------------
'Attempt to De-Serialize the MemoryStream back to an assembly
'--------------------------------------
voBF = New Runtime.Serialization.Formatters.Binary.BinaryFormatter
voBF.AssemblyFormat = Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple
voAssembly = voBF.Deserialize(voMemStream)
'----- **Error Here** ---------------------------------
此时出错:在voAssembly=voBF.反序列化(voMemStream)
无法加载文件或程序集“…”、版本=0.0.0.0、区域性=中立、PublicKeyToken=null”或其依赖项之一系统找不到指定的文件好的,我打盹后终于找到了。。。在这里为“后代”记录:P 显然,I不应该对编译的程序集进行序列化/反序列化
'-----------------------------------------------------
'Instead of Generating in Memory, generate it into a temporary Assembly file (I don't need the CompiledAssembly from the compiler).
' Note: Again, this is partial code, with only the important parts here.
'-----------------------------------------------------
voCompilerParams.ReferencedAssemblies.Add("mscorlib.dll")
voCompilerParams.GenerateInMemory = False
voCompilerParams.OutputAssembly = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "deleteNow_" & Guid.NewGuid().ToString() & ".dll")
Dim voCompileResults As System.CodeDom.Compiler.CompilerResults = voCompiler.CompileAssemblyFromSource(voCompilerParams, sb.ToString)
'--------------------------------------------------------------------------
'After the Assembly is generated, open the Assembly file and read the entire content into a byte buffer.
'Note that this byte buffer **CAN BE STORED into an SQL BLOB field.**
'--------------------------------------------------------------------------
Dim voInstance As Object = Nothing
Dim vsResult As String = ""
Dim voAssembly As Reflection.Assembly = Nothing
If Not voCompileResults.Errors.HasErrors Then
'----------------------------------------------------------------
'No compilation error, open and read the generated assembly file
' into a Byte array.
'----------------------------------------------------------------
Dim voBuffer() As Byte = Nothing
Dim voFileStream As IO.FileStream = IO.File.OpenRead(voCompilerParams.OutputAssembly)
If voFileStream.CanRead Then
ReDim voBuffer(voFileStream.Length - 1)
voFileStream.Read(voBuffer, 0, voFileStream.Length)
End If
voFileStream.Close()
IO.File.Delete(voCompilerParams.OutputAssembly) 'Clean-up after ourselves.
'----------------------------------------------------------------
'Now, re-create the CompiledAssembly from the Byte array.
'----------------------------------------------------------------
If Not IsNothing(voBuffer) Then
voAssembly = Reflection.Assembly.Load(voBuffer)
End If
If Not IsNothing(voAssembly) Then
'--------------------------------------------
'Instantiate my dynamically compiled class
'--------------------------------------------
voInstance = Activator.CreateInstance(voAssembly.GetType("libARules.clsMyRoutine"), {New libARules.Sys.clsInterProcessData(New System.Xml.XmlDocument), New libMyLib.clsMyClass})
If Not IsNothing(voInstance) Then
'------------------------------------------------
'Run my dynamic code to proof that it is working
'------------------------------------------------
vsResult = voInstance.GetType().InvokeMember("Run", Reflection.BindingFlags.InvokeMethod, Nothing, voInstance, Nothing)
End If
End If
End If
哎呀。。。将填充vsResult
。这证明了CompiledAssembly
可以在以后存储和重用注意在从voBuffer()
重新创建voAssembly
对象之前,实际的临时程序集文件已被删除