fs = New System.IO.FileStream(FileName, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read) Dim Reader As New System.IO.StreamReader(fs, System.Text.Encoding.Default) tmpStr = Reader.ReadToEnd Reader.Close() fs.Close() _StringBuilder.Append(tmpStr) End Sub '//返回代码串 Public Overrides Function ToString() As String Return _StringBuilder.ToString End Function '//清除原有代码 Public Sub Clear() If _StringBuilder.Length > 0 Then _StringBuilder.Remove(0, _StringBuilder.Length - 1) End Sub End Class 'CodeBuilder '测试 Dim MyComp As SourceComp Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '如果不重新生成实例,则因重新编译时输出同名的临时程序集,会出错 MyComp = New SourceComp Console.WriteLine(MyComp.GetExpressionValue("Math.Round(Math.SQRT(123 * 456), 2) ").ToString) '结果236.83 MyComp = New SourceComp Console.WriteLine(MyComp.GetExpressionValue("123 * 456 > 12 * 6987").ToString) '结果False End Sub |