vb.net中实现javascript中包含的功能,绝对不会有问题。做过javascript开发的程序员对eval都会有深刻的印象。只是在vb.net中实现这个功能没有想象得那么简单。不管怎么说能实现就好。但不
vb.net中实现javascript中包含的功能,绝对不会有问题。做过javascript开发的程序员对eval都会有深刻的印象。只是在vb.net中实现这个功能没有想象得那么简单。不管怎么说能实现就好。但不知道是否还有其他比较简单的方法。
例子代码如下:
Imports
System.CodeDom.Compiler
Public
Class Form1
Class Form1
Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(Calculate("1+2*3+4").ToString)
End Sub
Public Function Calculate()Function Calculate(ByVal expression As String) As Object
Dim className As String = "clsF"
Dim methodName As String = "funCal"
Dim classSource As New System.Text.StringBuilder
classSource.Append("public class " + className + vbCrLf)
classSource.Append(" public function " + methodName + "() as object" + vbCrLf)
classSource.Append(" return " + expression + vbCrLf)
classSource.Append(" end function" + vbCrLf)
classSource.Append("end class")
Dim codeProvider As New VBCodeProvider
Dim cParams As New CompilerParameters
cParams.GenerateExecutable = False
cParams.GenerateInMemory = False
Dim cResults As CompilerResults = codeProvider.CompileAssemblyFromSource(cParams, classSource.ToString)
Dim asm As System.Reflection.Assembly = cResults.CompiledAssembly
Dim eval As Object = asm.CreateInstance(className)
Dim method As System.Reflection.MethodInfo = eval.GetType().GetMethod(methodName)
Dim args() As String = Nothing
Dim reObj As Object = method.Invoke(eval, args)
GC.Collect()
Return reObj
End Function
End Class
【文章转自日本多IP服务器 http://www.558idc.com/japzq.html提供,感恩】