可能重复:
如何将当前程序集公开给 CodeProvider
我写了一个代码,它从 vb.net 中的文本框中转换字符串,就像在这个例子中一样:如何在 c# 中将字符串转换为代码
但是在这个类中,我不能从主项目中调用任何静态变量和方法。那是代码vb.net:
Dim vb As New VBCodeProvider()
Dim icc As ICodeCompiler = vb.CreateCompiler()
cp = New CompilerParameters
cp.ReferencedAssemblies.Add("System.dll")
cp.ReferencedAssemblies.Add("System.Windows.Forms.dll")
cp.ReferencedAssemblies.Add("System.Xml.dll")
cp.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll")
cp.ReferencedAssemblies.Add("Comatic(de-ch).exe")
cp.ReferencedAssemblies.Add("System.Data.dll")
cp.ReferencedAssemblies.Add("mscorlib.dll")
cp.CompilerOptions = "/t:library"
cp.GenerateInMemory = True
Dim sb As New StringBuilder("")
sb.Append("Imports System" & vbNewLine)
sb.Append("Imports Microsoft.VisualBasic" & vbNewLine)
sb.Append("Imports System.Windows.Forms" & vbNewLine)
sb.Append("Imports System.Xml" & vbNewLine)
sb.Append("Imports Comatic" & vbNewLine)
sb.Append("Imports System.Data" & vbNewLine)
sb.Append("<System.ComponentModel.ToolboxItem(False)> _" & vbNewLine)
sb.Append("Public Class VbCodeEvaler" & vbNewLine)
sb.Append("Public Sub EvalCode()" & vbNewLine)
sb.Append(TxVbCode.Text & vbNewLine)
sb.Append("End Sub" & vbNewLine)
sb.Append("Dim t As New System.Threading.Thread(AddressOf Me.EvalCode)" & vbNewLine) 'System.Threading.
sb.Append("Public Sub StartThred()" & vbNewLine)
sb.Append("t.Start()" & vbNewLine)
sb.Append("End Sub" & vbNewLine)
sb.Append("Public Sub StopThred()" & vbNewLine)
sb.Append("t.Abort()" & vbNewLine)
sb.Append("End Sub" & vbNewLine)
sb.Append("End Class" & vbNewLine)
cr = icc.CompileAssemblyFromSource(cp, sb.ToString())
If cr.Errors.Count > 0 Then
MsgBox("Error: " & cr.Errors(0).ErrorText)
Return Null
End If
Dim a As System.Reflection.Assembly = cr.CompiledAssembly
Dim o As Object = a.CreateInstance("VbCodeEvaler")
Dim t As Type = o.GetType()
'Dim mi As MethodInfo = t.GetMethod("EvalCode")
Dim mi As MethodInfo = t.GetMethod("StartThred")
mi.Invoke(o, Null)
如何从主项目中调用所有静态和公共方法和变量?谢谢!