Jump to content
Xtreme .Net Talk

Recommended Posts

  • Leaders
Posted

I have begun to write a program that will allow users to write scripts in VB/C#. Their scripts are compiled to in-memory assemblies using the compilers obtained by the Microsoft.VisualBasic.VBCodeProvider.GetCompiler and Microsoft.CSharp.CSharpCodeProvider.GetCompiler methods. I have tried handling the AppDomain.CurrentDomain.Unhandled exception and Application.CurrentThreadException (from both outside and inside the script) and script exceptions are not caught by these handlers.

 

What I want to do is catch script exceptions before they crash my program, end the script, and notify the user. How can I go about catching an unhandled exception in the script so that I can do this? I can not assume that the script writer will write unhandled exception proof code. The script can actually create a GUI in a panel that will be hosted in my application and utilize event handling, so I can not simply wrap function calls with a try/catch. Is there hope for my nifty-scriptable program?

[sIGPIC]e[/sIGPIC]
  • Leaders
Posted

I am using a method nearly identical to that of a tutorial found on these forums.

 

CompileScript, well... compiles the script. FindInterface returns an instance of the first object it finds (and there should only be one defined in the script) that implements my script interface, IScript.

   Public Shared Function CompileScript(ByVal Source As String) As CompilerResults
       'Reference to host app's exe
       Dim Reference As String = Application.ExecutablePath

       Dim Provider As New Microsoft.VisualBasic.VBCodeProvider
       Dim Compiler As ICodeCompiler = Provider.CreateCompiler
       Dim CompilerParam As New CompilerParameters
       Dim Results As CompilerResults

       'Set parameters 
       CompilerParam.GenerateExecutable = False
       CompilerParam.GenerateInMemory = True
       CompilerParam.IncludeDebugInformation = False
       CompilerParam.ReferencedAssemblies.Add("System.Windows.Forms.dll")
       CompilerParam.ReferencedAssemblies.Add("System.dll")
       CompilerParam.ReferencedAssemblies.Add("System.Drawing.dll")
       If Not Reference Is Nothing AndAlso Reference.Length <> 0 Then _
           CompilerParam.ReferencedAssemblies.Add(Reference)
       Results = Compiler.CompileAssemblyFromSource(CompilerParam, Source)

       Provider.Dispose()

       Return Results
   End Function

   Public Shared Function FindInterface(ByVal DLL As Reflection.Assembly, ByVal InterfaceName As String) As Object
       Dim t As Type

       'Loop through types looking for one that implements the given interface
       For Each t In DLL.GetTypes()
           If Not (t.GetInterface(InterfaceName, True) Is Nothing) Then
               Return DLL.CreateInstance(t.FullName)
           End If
       Next

       Return Nothing
   End Function

The panel for the UI is obtained by an IScript function and, for the time being, is shown in a newly generated form. Ultimately, I am going to implement a tabbed interface.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...