Library tutorials & articles
Using .NET to make your Application Scriptable
- Introduction
- The Object Model, Host Application & Script Editor
- Dynamic Compilation
- Analysing the Compile Results
- Finishing Off
Analysing the Compile Results
Back to our script editing dialog. In the handler procedure for the Ok button, we need to use the procedure we've just written to compile the source code. We'll start by getting the path to the assembly the compiler needs to reference - our interfaces assembly. This is easy since it's in the same directory as the running host application. Then we clear our "errors" listview and run the CompileScript function.
[VB]
Dim results As CompilerResults
Dim reference As String
'Find reference
reference = System.IO.Path.GetDirectoryName(Application.ExecutablePath)
If Not reference.EndsWith("\") Then reference &= "\"
reference &= "interfaces.dll"
'Compile script
lvwErrors.Items.Clear()
results = Scripting.CompileScript(ScriptSource, reference, _
Scripting.Languages.VB)
[C#]
CompilerResults results;
string reference;
// Find reference
reference = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
if (!reference.EndsWith(@"\"))
reference += @"\";
reference += "interfaces.dll";
// Compile script
lvwErrors.Items.Clear();
results = Scripting.CompileScript(ScriptSource, reference,
Scripting.Languages.VB);
Next we look at the Errors collection of our results. If it is empty, the compile succeeded. In this case, we use our FindInterface method to instantiate the class from the assembly and store it, then return DialogResult.Ok to the procedure that showed the script editor. If the Errors collection is not empty, we iterate through it populating the listview with all the errors that occured during the compile.
[VB]
Dim err As CompilerError
Dim l As ListViewItem
'Add each error as a listview item with its line number
For Each err In results.Errors
l = New ListViewItem(err.ErrorText)
l.SubItems.Add(err.Line.ToString())
lvwErrors.Items.Add(l)
Next
MessageBox.Show("Compile failed with " & results.Errors.Count.ToString() & _
" errors.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Stop)
[C#]
ListViewItem l;
// Add each error as a listview item with its line number
foreach (CompilerError err in results.Errors)
{
l = new ListViewItem(err.ErrorText);
l.SubItems.Add(err.Line.ToString());
lvwErrors.Items.Add(l);
}
MessageBox.Show("Compile failed with " + results.Errors.Count.ToString() +
" errors.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Stop);
Related articles
Related discussion
-
hey developers out there
by pitsophera (0 replies)
-
How can i develop opc server using .net?
by vairajaig (1 replies)
-
Creating a Windows Service in VB.NET
by davidvanr (108 replies)
-
High-Performance .NET Application Development & Architecture
by Manjot Bawa (0 replies)
-
An Introduction to VB.NET and Database Programming
by carlosmen (14 replies)
Related podcasts
-
A Practical Look at Silverlight 2 Part 1
Now that Silverlight 2 is at the Olympics and making a big splash, we wanted to explore this fascinating technology more. Microsoft Silverlight 2 is a cross-browser, cross-platform, and cross-device plug-in for delivering the next generation of .NET based media experiences and rich interactive ap...
Events coming up
-
Dec
9
GL.net Group Meeting - December 2009
Gloucester, United Kingdom
The beginning of this year holiday season will belong to mocks. Ronnie and Stephen will take us for a tour around exciting world of unit testing.
How can we expose host application objects to the script that we dynamically compile?
How can we expose object in the host application to the script that we dynamically compile?
This thread is for discussions of Using .NET to make your Application Scriptable.