Click here to Skip to main content
15,867,942 members
Articles / Programming Languages / C#
Article

C# Script Engine tutorial

Rate me:
Please Sign up or sign in to vote.
3.08/5 (12 votes)
21 Aug 2002 140.5K   1.3K   54   14
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.

Image 2

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

C#
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

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

Here is the VB script sample

VB.NET
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


Written By
Web Developer
China China
Have more than 10 programming experience. know SNA server, COM, CORBA well, and now I am moving to .net framework.

Comments and Discussions

 
GeneralSNA Communication Pin
Mario_F12-Aug-08 5:56
Mario_F12-Aug-08 5:56 
GeneralVS 2005 Pin
lshyamal31-May-07 20:27
lshyamal31-May-07 20:27 
GeneralRe: VS 2005 Pin
spinoza7-Nov-07 0:04
spinoza7-Nov-07 0:04 
General.NET 2 Pin
Boaz V2-Aug-05 23:32
Boaz V2-Aug-05 23:32 
GeneralExecuting VBScripts from C# application Pin
For_IT26-Oct-04 0:25
For_IT26-Oct-04 0:25 
AnswerRe: Executing VBScripts from C# application Pin
Shmarya21-Oct-05 3:57
Shmarya21-Oct-05 3:57 
GeneralCool! And one suggestion! Pin
TeleStar17-Oct-03 10:25
TeleStar17-Oct-03 10:25 
AnswerRe: Cool! And one suggestion! Pin
pconsulting16-Sep-05 3:34
pconsulting16-Sep-05 3:34 
GeneralRe: Cool! And one suggestion! Pin
TeleStar16-Sep-05 12:36
TeleStar16-Sep-05 12:36 
Generalproblem Pin
Jesus Oliva17-Dec-02 3:14
Jesus Oliva17-Dec-02 3:14 
GeneralRe: problem Pin
zhangzq7117-Dec-02 3:45
zhangzq7117-Dec-02 3:45 
GeneralC++ Scripting Pin
Alois Kraus22-Aug-02 20:36
Alois Kraus22-Aug-02 20:36 
GeneralVery nice example. Pin
Gevik Babakhani22-Aug-02 3:26
Gevik Babakhani22-Aug-02 3:26 
GeneralRe: Very nice example. Pin
zhangzq7122-Aug-02 3:42
zhangzq7122-Aug-02 3:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.