如何评估存储在字符串中的表达式?

时间:2014-02-28 07:34:39

标签: .net vb.net

我需要这样做....

dim mVar as String = ""
dim mVar1 as double
dim mVar2 as double

mvar= " mVar1> mVar2 "

if mvar Then
.
.
.
End IF

vb.net是否支持?

如果是,那么如何?

mvar将根据我的要求提供不同的条件。

1 个答案:

答案 0 :(得分:1)

您可以根据自己的条件使用String,而不是使用Delegate

示例:

Dim var1 As Double = 14.03
Dim var2 As Double = 13.99

Dim biggerThan = Function(a As Double, b As Double) a > b
Dim areEqual = Function(a As Double, b As Double) a = b

If biggerThan(var1, var2) Then
    Console.WriteLine("var1 > var2")
Else If areEqual(var1, var2) Then
    Console.WriteLine("var1 = var2")
End If

var1 = 13.99

If biggerThan(var1, var2) Then
    Console.WriteLine("var1 > var2")
Else If areEqual(var1, var2) Then
    Console.WriteLine("var1 = var2")
End If

<强>输出:

var1 > var2
var1 = var2

如果您真的想要或必须使用字符串,请查看Flee库。

示例:

Dim context = New ExpressionContext()
context.Variables("var1") = 14.03
context.Variables("var2") = 13.99

Dim result = context.CompileGeneric(Of Boolean)("var1 > var2").Evaluate()
Console.WriteLine(result)

<强>输出:

True