Hello, I have another question about dynamically compiled code. I am using the VBCodeProvider class to compile, test, and run "scripts" that users type into my program (Code Below).
Public Function Compile(ByVal vbCode As String, ByVal argsString As String) As Object
Dim oCodeProvider As VBCodeProvider = New VBCodeProvider
' Obsolete in 2.0 framework
' Dim oICCompiler As ICodeCompiler = oCodeProvider.CreateCompiler
Dim oCParams As CompilerParameters = New CompilerParameters
Dim oCResults As CompilerResults
Dim oAssy As System.Reflection.Assembly
Dim oType As Type
Try
' Setup the Compiler Parameters
' Add any referenced assemblies
oCParams.ReferencedAssemblies.Add("system.dll")
oCParams.ReferencedAssemblies.Add("system.xml.dll")
oCParams.ReferencedAssemblies.Add("system.data.dll")
oCParams.CompilerOptions = "/t:library"
oCParams.GenerateInMemory = True
' Generate the Code Framework
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.Collections.Generic" & vbCrLf)
sb.Append("Imports RBDS_Control.RBDS" & vbCrLf)
' Build a little wrapper code, with our passed in code in the middle
sb.Append("Namespace dValuate" & vbCrLf)
sb.Append("Class EvalRunTime " & vbCrLf)
sb.Append("Public Function EvaluateIt(" & argsString & ") As Object " & vbCrLf)
sb.Append(vbCode & vbCrLf)
sb.Append("End Function " & vbCrLf)
sb.Append("End Class " & vbCrLf)
sb.Append("End Namespace" & vbCrLf)
Debug.WriteLine(sb.ToString())
Try
' Compile and get results
' 2.0 Framework - Method called from Code Provider
oCResults = oCodeProvider.CompileAssemblyFromSource(oCParams, sb.ToString)
' 1.1 Framework - Method called from CodeCompiler Interface
' cr = oICCompiler.CompileAssemblyFromSource (cp, sb.ToString)
' Check for compile time errors
If oCResults.Errors.Count <> 0 Then
Me.CompilerErrors = oCResults.Errors
Throw New Exception(oCResults.Errors.Item(0).ToString)
Else
' No Errors On Compile, so continue to process...
oAssy = oCResults.CompiledAssembly
oExecInstance = oAssy.CreateInstance("dValuate.EvalRunTime")
oType = oExecInstance.GetType
oMethodInfo = oType.GetMethod("EvaluateIt")
End If
Catch ex As Exception
' Compile Time Errors Are Caught Here
' Some other weird error
Debug.WriteLine(ex.Message)
End Try
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
Return Nothing
End Function
Everything works great, however I am unable to add reference to more code. I have a class in my project which defines a new data type used troughout my program. The code the user writes is contained in a predefined function which is passed a dictionary of this datatype in my code. However, VB complains that it cannot find this class because the dynamically compiled code is run out of its own dll. The VBCodeProvider has the ability to add referenced assemblies oCParams.ReferencedAssemblies.Add("system.dll")
however I do not know how to add my class since it is not in a dll and is not part of the .NET framework.
Any help is greatly appreciated. Thank you in advance.