1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| Imports System.CodeDom.Compiler
Imports System.Reflection
Public Class Form1
Private stringFormule As String
Private stringParamFn As String
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
inputFormule.Text = "Un*In_recepteur" 'puissance apparente en KVA
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If stringFormule.Length = 0 Then Return
Dim inputData As String = "txtOutput.Text = Puissance_Absorbe(220,5)"
Dim code As Object = <code>
Imports System
Imports System.Windows.Forms
Public Class Test
Public Pa As Double
Public Un As Double
Public In_recepteur As Double
Public racine3 As Double = 1.73205
Public cosphi As Double
Public Sub UpdateText(ByVal txtOutput As TextBox)
<%= inputData %>
End Sub
Public Function Puissance_Absorbe(<%= stringParamFn %> ) as Double
Pa = Un * In_recepteur * racine3 * cosphi
Dim formule As Double = <%= stringFormule %> 'on court-circuite le code precedent
Pa = formule
Return Pa
End Function
End Class
</code>
Dim provider As New VBCodeProvider()
Dim vbParams = New CompilerParameters()
' Add referenced assemblies.
vbParams.ReferencedAssemblies.Add("mscorlib.dll")
vbParams.ReferencedAssemblies.Add("System.dll")
vbParams.ReferencedAssemblies.Add("System.Windows.Forms.dll")
vbParams.GenerateExecutable = False
vbParams.GenerateInMemory = True
vbParams.OutputAssembly = "LibFonctions"
Dim compilResults As CompilerResults = provider.CompileAssemblyFromSource(vbParams, code.Value)
If compilResults.Errors.Count > 0 Then
' Show each error.
For Each er In compilResults.Errors
MessageBox.Show(er.ToString())
Next
Else
txtSourceCode.Text = code.value
Dim obj As Object = compilResults.CompiledAssembly.CreateInstance("Test")
Dim args() As Object = {Me.txtOutput}
' Execute the method by passing the method name and arguments.
obj.GetType().InvokeMember("UpdateText", BindingFlags.InvokeMethod, Nothing, obj, args)
End If
End Sub
Private Sub inputFormule_TextChanged(sender As System.Object, e As System.EventArgs) Handles inputFormule.TextChanged
If inputFormule.Text.Length = 0 Then Return
stringFormule = inputFormule.Text
Dim arr() As String = inputFormule.Text.Split(New Char() {"+", "-", "*", "/", "(", ")"}, StringSplitOptions.RemoveEmptyEntries)
For Each item As String In arr
stringParamFn += "ByVal " + item + " as Double ,"
Next
stringParamFn = stringParamFn.TrimEnd(New Char() {","})
End Sub
End Class |
Partager