2016-03-24 3 views
3

実行時に文字列の中でいくつかのコードを実行しようとしています。 I.動的コードの実行:String - >ランタイムコードVB.net

Dim code As String = "IIf(1 = 2, True, False)" 

code文字列内でコードを実行するにはどうすればよいですか?

+0

** CodeDom **コンパイラクラスを使用します。 https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.vbcodeprovider(v=vs.110).aspx分析できるラッパーがあります:https://github.com/ElektroStudios/ElektroKit /blob/master/Solution/v1.5/Elektro.Interop/Types/VisualBasicCompiler.vb算術式のみを評価したい場合は、** NCalc ** library:https://ncalc.codeplexを使用することをお勧めします.com /(MicrosoftのJSエバリュエーターは推奨されていないため)。また、** IF()**の代わりに** IF()**を使用する必要があります。 – ElektroStudios

+0

ありがとうございます。私は、このメソッドを使用してCodeDomを使って試してみると思います:http://stackoverflow.com/questions/21379926/load-vb-net-code-from-txt-file-and-execute-it-on-fly -using-system-codedom-compi – Developer

答えて

1

@ElektroStudiosが言ったように、これを行う適切な方法はCodeDom compilerを使用することですが、これはこれほど単純なものでは少し残忍です。

することはできソートのカンニング例えばのでDataColumn Expression

のパワーを活用:

Public Function EvaluateExpression(Of T, K)(input As T, formula As String) As K 
    'add a columns to hold the value 
    Dim colStatus As New DataColumn 
    With colStatus 
     .DataType = GetType(T) 
     .ColumnName = "Condition" 
    End With 

    'add a column to compute the expression 
    Dim colExp As New DataColumn 
    With colExp 
     .DataType = GetType(K) 
     .ColumnName = "Expression" 
     .Expression = formula 
    End With 

    'create a table and add the columns 
    Dim dt As New DataTable 
    With dt.Columns 
     .Add(colStatus) 
     .Add(colExp) 
    End With 

    'now add a row and set the condition to the value we have 
    Dim row As DataRow = dt.NewRow 
    row.SetField(Of T)("Condition", input) 
    dt.Rows.Add(row) 

    'now read back the computed value based on the expression being evaluated 
    Return row.Field(Of K)("Expression") 
End Function 
:あなたはこのような、より一般的な関数にこのすべてを包むことができ

Dim formula = "IIF(Condition = 'Yes', 'Go', 'Stop')" 
    Dim value As String = "Yes" 
    Dim result As String 

    'add a columns to hold the value 
    Dim colStatus As New DataColumn 
    With colStatus 
     .DataType = System.Type.GetType("System.String") 
     .ColumnName = "Condition" 
    End With 

    'add a column to compute the expression 
    Dim colExp As New DataColumn 
    With colExp 
     .DataType = System.Type.GetType("System.String") 
     .ColumnName = "Expression" 
     .Expression = formula 
    End With 

    'create a table and add the columns 
    Dim dt As New DataTable 
    With dt.Columns 
     .Add(colStatus) 
     .Add(colExp) 
    End With 

    'now add a row and set the condition to the value we have 
    Dim row As DataRow = dt.NewRow 
    row.SetField(Of String)("Condition", value) 
    dt.Rows.Add(row) 

    'now read back the computed value based on the expression being evaluated 
    result = row.Field(Of String)("Expression") 
    MessageBox.Show(result) 

それで、あなたは次のように呼び出すことができます:

Dim result = EvaluateExpression(Of Integer, Boolean)(1, "IIF(Condition = 1, True, False)") 
+1

datacolumn式を使用すると、これまでに見たことのないスマートなソリューションとなりました。これを共有してくれてありがとう。私は、文字列の場合、ユーザは単一の引用符を供給する必要があり、代わりにその引用符を二重引用符で囲む必要があることに注意しました。 (** Condition = 'string' **)、ダブルクォートのエスケープが面倒だった理由を私に聞いていたので、ちょっとコメントしたかったのですが、一重引用符で試しました。 – ElektroStudios

関連する問題