RE: Convert String to Object or Method

G

Guest

John,

It is possible. I posted something yesterday with the subject ".NET equivalent to VBA". I was given a link to http://www.eggheadcafe.com/articles/20030908.asp by Cor. Have a look, you will need to set-up a function that takes transformer as a parameter so it can be updated.

Chris.

----- John F wrote: -----

Hi All,

Is it possible to read text form a file and execute or convert it to an object? e.g.

File contains the following text

transformer.core.conductor.material = "M3"
transformer.core.conductor.stackedHeight = 160
...

where these woruld be valid lines of code in my programme
 
G

Guest

Chris

I get the following erro

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dl

Additional information: File or assembly name nfyq0rkq.dll, or one of its dependencies, was not found

The dll file in the second line changes every time I try to use this code.
 
G

Guest

John

I didn't quite use the full example. I put some code behind a button
The code for the form I used is below, excluding the "Windows Form Designer generated code" region

The dll name is changing becauses it's creating a temporary file to hold the code, this is then invoked
If you still get the same problem put a try/catch round the lot and print the full exception in the command window. If you post the full exception I'll have a look but can't promise anything

Let me know how you get on

Chris

Imports Syste
Imports System.Tex
Imports System.CodeDom.Compile
Imports System.Reflectio
Imports System.I
Imports Siclops.Data.sicSystemOption

Public Class Form
Inherits System.Windows.Forms.For
#Region " Windows Form Designer generated code
#End Regio

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Clic
Dim c As VBCodeProvider = New VBCodeProvide
Dim icc As ICodeCompiler = c.CreateCompiler(
Dim cp As CompilerParameters = New CompilerParameter

cp.ReferencedAssemblies.Add("system.dll"
cp.ReferencedAssemblies.Add("system.xml.dll"
cp.ReferencedAssemblies.Add("system.data.dll"

' Sample code for adding your own referenced assemblie
'cp.ReferencedAssemblies.Add("c:\yourProjectDir\bin\YourBaseClass.dll"
'cp.ReferencedAssemblies.Add("YourBaseclass.dll"
cp.CompilerOptions = "/t:library
cp.GenerateInMemory = Tru
Dim sb As StringBuilder = New StringBuilder(""
sb.Append("Imports System" & vbCrLf
sb.Append("Imports System.Xml" & vbCrLf
sb.Append("Imports System.Data" & vbCrLf
sb.Append("Imports System.Data.SqlClient" & vbCrLf
sb.Append("Namespace myNamespace " & vbCrLf
sb.Append("Class myLib " & vbCrLf

sb.Append("Public Function UsersCode() As Object " & vbCrLf
'sb.Append("YourNamespace.YourBaseClass thisObject = New YourNamespace.YourBaseClass()"
sb.Append(vbCode() & vbCrLf
sb.Append("End Function " & vbCrLf
sb.Append("End Class " & vbCrLf
sb.Append("End Namespace" & vbCrLf
Debug.WriteLine(sb.ToString()) ' look at this to debug your eval strin
Dim cr As CompilerResults = icc.CompileAssemblyFromSource(cp, sb.ToString()
Dim a As System.Reflection.Assembly = cr.CompiledAssembl
Dim o As Objec
Dim mi As MethodInf
o = a.CreateInstance("myNamespace.myLib"
Dim t As Type = o.GetType(
mi = t.GetMethod("UsersCode"
Dim s As Objec
s = mi.Invoke(o, Nothing
MsgBox(s.GetType.ToString
End Su

Private Function vbCode() As Strin
Dim v As Strin

v = "Dim strConn As String = " & Chr(34) & "Server=(local);dataBase=Northwind;User id=sa;Password=;" & Chr(34
v &= vbCrLf & "Dim cmd As New SqlCommand()
v &= vbCrLf & "Dim cn As New SqlConnection(strConn)
v &= vbCrLf & "cn.Open()
v &= vbCrLf & "cmd.Connection = cn
v &= vbCrLf & "cmd.CommandText = " & Chr(34) & "select * from employees" & Chr(34
v &= vbCrLf & "cmd.CommandType = CommandType.Text
v &= vbCrLf & "Dim ds As DataSet = New DataSet()
v &= vbCrLf & "Dim da As New SqlDataAdapter()
v &= vbCrLf & "da.SelectCommand = cmd
v &= vbCrLf & "da.Fill(ds)
'v &= vbCrLf & "Microsoft.VisualBasic.MsgBox(" & Chr(34) & "About to return" & Chr(34) & ")
v &= vbCrLf & "Return ds

Return
End Functio
End Clas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top