Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

C# Script Engine tutorial

0.00/5 (No votes)
21 Aug 2002 1  
Introduces how to use Microsoft.vsa to make your programs have script functions

Sample Image - CSScriptEngine.gif

Introduction

Many modern applications support script functions that provide the user a way to programmatically control the application itself. This technology lets the user have the ability to control the flow of the program as they wish. Before, Microsoft had provided IActiveScript interface to access the script engine. In .NET framework, Microsoft provides many classes to support script engine, please refer to ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfMicrosoftVsa.htm. I have written a sample program to show how to do it in C#. In this sample program, I created a winform control and put it in my sample program, later I will show how to use VB script to call method in this program, and handle the event from this control using the script. The user control is named UserControl1, it has one method and one event.

You must implement Microsoft.Vsa.IVsaSite, it provides a bridge between your program and script engine

public class TestVsaSite : Microsoft.Vsa.IVsaSite
{
    private System.Collections.Hashtable eventInstance;

    public TestVsaSite()
    {
        eventInstance = new Hashtable();
    }

    public void AddEvent(string name, string type, object instance, 
           bool events)
    {
        eventInstance.Add(name, instance);
    }

    public void GetCompiledState(out byte[] pe, out byte[] debugInfo)
    {
    }

    public object GetEventSourceInstance(string itemName, 
            string eventSourceName)
    {
        return eventInstance[eventSourceName];
    }

    public object GetGlobalInstance(string name)
    {
        return (object)null;
    }

    public void Notify(string notify, object info)
    {
    }

    public bool OnCompilerError(IVsaError error)
    {
        return false;
    }
}

Now create a script engine instance

m_vbEngine = new Microsoft.VisualBasic.Vsa.VsaEngine();
You can hook the event by calling
m_vbScript.AddEventSource(name, type);
m_vbTempRef = (IVsaReferenceItem)m_vbItems.CreateItem(name, 
    VsaItemType.Reference, VsaItemFlag.None);

Here is the VB script sample

imports myNotClass
imports System
Public Module Vespu

#Region "Automatically generated code, do not modify"
'Automatically generated code, do not modify

'Event Sources Begin

    <System.ContextStaticAttribute()> Public WithEvents ctrlInScript _
      As myNotClass.UserControl1
'Event Sources End

'End of automatically generated code

#End Region


Sub main
End Sub

Sub ctrlInScript_tick(count As Integer) Handles ctrlInScript.tick
dim disp as string
disp = "tick counter is: " & count.ToString()
ctrlInScript.SetText(disp)
End Sub
End Module

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