Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C#

VSA Scripting in .NET

Rate me:
Please Sign up or sign in to vote.
4.85/5 (27 votes)
27 Feb 200518 min read 140K   3.3K   101  
Using Visual Studio for Applications to add scripting capabilities to your .NET apps.
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using VsaScripting;

namespace Scripting
{
	/// <summary>
	/// Summary description for Startup.
	/// </summary>
	public class Startup
	{
		private static ScriptableForm _scriptableForm;
		private static VsaScriptingHostFactory _factory;

		/// <summary>
		/// The entry point for the application
		/// </summary>
		/// <param name="args">The command line arguments passed to the application</param>
		[STAThread()]
		public static void Main(string[] args)
		{		
			// create a new form instance
			_scriptableForm = new ScriptableForm();
						
			// create a new host factory
			_factory = new VsaScriptingHostFactory();

			// create a new set of hosts
			VsaScriptingHost[] hosts = _factory.Create(@"MyScriptingHost", @"Scripting", true, Application.StartupPath);

			// wire up to the events of each host
			foreach(VsaScriptingHost host in hosts)
			{				
				host.AssemblyReferencesNeeded += new ScriptingHostEventHandler(OnAssemblyReferencesNeeded);
				host.CompilerException += new ScriptingHostCompilerExceptionEventHandler(OnCompilerException);
				host.GlobalItemsNeeded += new ScriptingHostEventHandler(OnGlobalItemsNeeded);
			}

			// execute the hosts
			foreach(VsaScriptingHost host in hosts)
				host.Execute();						

			// show the form
			_scriptableForm.ShowDialog();
		}
		
		/// <summary>
		/// Occurs when assembly references are needed
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private static void OnAssemblyReferencesNeeded(object sender, VsaScriptingHostEventArgs e)
		{
			// add the current assembly as a reference
			e.Host.CreateAssemblyReferences(Assembly.GetExecutingAssembly().Location);
		}

		/// <summary>
		/// Occurs when an error occurs during compilation or execution
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private static void OnCompilerException(object sender, VsaScriptingHostCompilerExceptionEventArgs e)
		{
			Debug.WriteLine(e.Exception);
		}

		/// <summary>
		/// Occurs when global items are needed
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private static void OnGlobalItemsNeeded(object sender, VsaScriptingHostEventArgs e)
		{
			// publish the scriptable form so that the scripts may modify it
			e.Host.CreateGlobalItem("_scriptableForm", _scriptableForm, false);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
Senior Application Developer specializing in Windows desktop and network development.

Professional Experience
- B.S. of Computer Science (Graduated 2001 - PSU)
- Senior Application Developer (8+ yrs)
- Microsoft Certified Professional

Primary Interests
- C#, C++, HTML, Javascript
- XML, ASP.NET, Web Services, SOAP, UDDI
- Socket programming and anything network related
- Reflection, Serialization, and Plugin Frameworks
- Owner-drawn controls and GDI+ goodness

Comments and Discussions