掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
VB2010 文字列の計算式で演算関数も対応するには? (ID:148121)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
> ScriptControlで対応できるのですね。 「Microsoft.Jscript.DLL」でもできますよ。 Imports System.CodeDom.Compiler Module Module1 Sub Main() Dim funcList = <![CDATA[ package Andy { public class Sample { //--- evalを公開 public function EVal(expression:System.String):System.Object { return eval(expression); } //--- 独自関数 public function Pow(a, b) { return Math.pow(a, b); }; public function Exp(a) { return Math.exp(a); }; } } ]]>.Value Using js As New Microsoft.JScript.JScriptCodeProvider() Dim cc = js.CreateCompiler() Dim res = cc.CompileAssemblyFromSource( New CompilerParameters() With {.GenerateInMemory = True}, funcList) If res.Errors.HasErrors Then Dim msg = String.Join(vbNewLine, _ res.Errors.OfType(Of CompilerError)().Select(Function(c) c)) MsgBox(msg, MsgBoxStyle.Exclamation Or MsgBoxStyle.SystemModal) Return End If Dim andySample = Activator.CreateInstance(res.CompiledAssembly.GetType("Andy.Sample")) Dim EVal = Function(expression As Object) Return CallByName(andySample, "EVal", CallType.Method, expression) End Function MsgBox(EVal("150+12*Pow(10,2)"), MsgBoxStyle.Information Or MsgBoxStyle.SystemModal) MsgBox(EVal("Exp(123.45)"), MsgBoxStyle.Information Or MsgBoxStyle.SystemModal) MsgBox(Math.Log(EVal("Exp(123.45)")), MsgBoxStyle.Information Or MsgBoxStyle.SystemModal) End Using End Sub End Module
←解決時は質問者本人がここをチェックしてください。
更新する
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.