Click here to Skip to main content
Click here to Skip to main content

.Net Script Editor (C#,Vb.net Mini IDE)

By , 13 Jul 2008
Rate this:
Please Sign up or sign in to vote.

Add scripting functionality to your application,features of MS Script control (Functions like AddObject) and some features of VSA/Visual Studio editor like intellisence, code completion etc. Mini .Net IDE in single winform control.

Introduction

The actual requirement of this control is to build scripting feature in my application as available in all Microsoft Office applications (VBA) and is alternative to Microsoft product VSA (Microsoft Visual Studio for Applications).

The source code / article published here is to provide features of MS Script control (Functions like AddObject) and some features of VSA/Visual Studio editor like intellisence, code completion etc. The control supports C# and VB.net language however can be easily modified to support any .net language. You can use COM references, .Net references & Web References. The control supports Visual studio IDE features like bookmark, highlighting interpreter errors & compiler errors, adding multiple classes and interfaces. You can imagine visual studio IDE build in single control with feature like AddObject from MS Script Control. To use this control you just need to drag and drop it to your winforms and add few lines of code to add instance of object which you want to access in the scripts.

VSA History

The actual requirement of this control is to build scripting feature in my application as available in all Microsoft Office applications (VBA) and is alternative to Microsoft product VSA (Microsoft Visual Studio for Applications)

Many developers are looking for scripting solution and are very disappointed with Microsoft for not providing any equivalent feature of Microsoft scripting control in .Net framework. Script controls have some very nice features like AddObject, which allows you to add object instance (at run time) to the scripting engine which can be accessed by the script. For more details on VBScript Editor with Intellisense using MS Script control check my previously published article on plant source code.

Some developer’s tries to use MS Scripting control using .net interop however not all the features are available and it is quite unstable. AddObject method can’t be used to access .net objects.

Microsoft has replaced VBA (Visual Basic for applications ) with VSA (Visual Studio for Applications) VSA namespace was available in versions before .net 2.0, unfortunately vsa namespace is now obsolete (in .net 2.0 and further versions).

Microsoft has released separate product “Microsoft Visual Studio for Applications” to add scripting capabilities to your applications like VBA, however you can now use C#, vb.net etc in VSA editor. VSA is shipped with SQL server 2005 Integration services (SSIS), which is another name of DTS (SQL Server 2000). While designing package in SSIS you can use VSA editor to write .Net code. You need to buy this (VSA) product if you want to use it in your product/application.

Using the code

Note: The complete source code for ScriptControl is published on sourceforge.net and only wrapper project ( and binaries) to test the control is posted on code project.com

The source code / article published here is to provide features of MS Script control (Functions like AddObject) and some features of VSA/Visual Studio editor like intellisence, code completion etc. The control supports C# and VB.net language however can be easily modified to support any .net language. You can use COM references, .Net references & Web References. The control supports Visual studio IDE features like bookmark, highlighting interpreter errors & compiler errors, adding multiple classes and interfaces. You can imagine visual studio IDE build in single control with feature like AddObject from MS Script Control. To use this control you just need to drag and drop it to your winforms and add few lines of code to add instance of object which you want to access in the scripts.

When you compile the scripts it will generate AIMSScript.dll assembly, which holds you compiled .net code. When you click on execute button the assembly will get loaded into separate appdomain and will be execute there and the appdomain is then unloaded completely as there no way to unload single assembly from appdomain.

The published source code (on sourceforge.net) contains following sub projects

1. Script Control:
Description: Main control which provide visual studio like IDE in single window control. It uses all below mentioned components to provide the functionality. Auto list window, code compilation feature, Intellesense feature, com, .net and web reference feature etc are all implemented in this control. This is based on libraries from SharpDevelop open source C# IDE
2. Script Engine
Description: This is control used to execute your assembly generated from script code in separate App Domain and returns the exit code to calling program. It uses IRun Interface, which is part of ScriptRun project (Mentioned below) for cross App Domain communication.
3. Script Run
Description: This project exposes IRun interface and ScriptInstance (drived from MarshalByRefObject) class and is used to communicate with cross app domains using remoting.
4. Code Editor
Description: Code Editor control is control where you write your code. It uses syntaxdocument to implement code colour in the editor window.
The syntax highlight support library is based on Fireball.CodeEditor . Some features/concepts like completion window, insight window of code editor control are derived from SharpDevelop open source project.
5. SyntaxDocument
Description: SyntaxDocument project is DOM implementation to support colour codes for various languages. This library is based on open source project Fireball.CodeEditor.
6. SyntaxFiles
Description: The project assembly persists various colour rules for each language. It supports varieties of Syntax Files for languages like C#, CPP, CSS, Delphi, Fortran, foxpro, HTML, Java, Lotus Script, PHP and lot more. Each syntax file holds rules for colour coding for particular language in XML format which can be applied to CodeEditor control using CodeEditorSyntaxLoader class. This library is based on open source project Fireball.CodeEditor .
7. Docking
Description:The docking library provides GUI controls like Auto Hide Strips, dock pane, dock windows etc. and is based on libraries from open source project dockpanelsuite and Fireball.CodeEditor

8. Test – Project to test control
Description: This is the startup project and contains single form to host the script control and Engine control.Following code is used to link script control and engine control.

public <place />Main</place />()
        {
            InitializeComponent();
            scriptControl1.AddObject("Container", this);
            scriptControl1.StartEditor();
            scriptControl1.Execute += new EventHandler(scriptControl1_Execute);
        }
        void scriptControl1_Execute(object sender, EventArgs e)
        {
            try
            {
                engine1.OutputAssemblyName = scriptControl1.OutputAssemblyName;
                engine1.StartMethodName = scriptControl1.StartMethodName;
                engine1.DefaultNameSpace = scriptControl1.DefaultNameSpace;
                engine1.RemoteVariables = scriptControl1.RemoteVariables;
                engine1.DefaultClassName = scriptControl1.DefaultClassName;




                object ret = engine1.Execute(null);
                MessageBox.Show("Return Code : " + ret.ToString());

            }
            catch
            {
            }
        }        




Points of Interest : AddObject Feature

Note that scriptControl1.AddObject("Container", this); code is adding instance of parent form to be accessed by script by name "Container".If you type code like Container.Text = "Hi"; it will change the caption of parent form.

To implement AddObject feature following code is used which is hidden in file Program.sys:This class is drived from MarshalByRefObject and implements Irun interface for cross appdomain communication.

            

#region System Generated Source Code.Please do not change ...

namespace AIMS.Script
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Reflection;
    public partial class Program : MarshalByRefObject, <city /><place />IRun
    {
        public static System.Windows.Forms.Form Container = null;

         [DebuggerStepperBoundary()]

        void IRun.Initialize(IDictionary<string, object> Variables)
        {  foreach (string name in Variables.Keys)
            { object value = null;
                try { Variables.TryGetValue(name, out value);
                 FieldInfo fInfo = this.GetType().GetField(name, BindingFlags.Public | BindingFlags.Static);
                      fInfo.SetValue(this, value);
               }
               catch (Exception ex)
                {
                   throw ex;
                }
           }
        }
        [DebuggerStepperBoundary()]
        object IRun.Run(string StartMethod, params object[] Parameters)
        {try
           {MethodInfo methodInfo = this.GetType().GetMethod(StartMethod, BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance);
                  return methodInfo.Invoke(this, Parameters);
           }
          catch (Exception ex)
            {
              throw ex;

            }
      }
       [DebuggerStepperBoundary()]
       void IRun.Dispose(IDictionary<string, object> Variables)
       {
           foreach (string name in Variables.Keys)
            {
             object value = null; 
               try
               {FieldInfo fInfo = this.GetType().GetField(name, BindingFlags.Public | BindingFlags.Static);

                       fInfo.SetValue(this, value);
                }
              catch (Exception ex)
               {
                  throw ex;
               }
            }
       }
    }
}
#endregion

It is not possible to provide all technical implementation (of this project) in this article but I promises to publish further information if you are really interested in this control. Your feed backs are very valuable and keeps me motivating to send some time on such projects and publish them, so please don’t forget to add your feedback.

Further Enhancements:

1. Support for debugger & break points

- Current project allows you to mark break point in GUI only however you can use debugger.break function to break the code at any line and if .Net is installed on your machine its .net debugger will be attached to your assembly. SharpDevelop IDE support debugging and it can be implemented in the system.

2. Form Designer :

- Form designer was not required in my application and hence I have not implemented, However you may implement form designer component (see SharpDevelop for details )

3. Save option is not yet implemented you may design structure of project file (XML) and its content files.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author


Comments and Discussions

 
Generalgood article and very nice control Pinmemberpita200018-Nov-08 2:16 
GeneralForms PinmemberJ Boden13-Nov-08 9:44 
QuestionWill there be new versions in future? PinmemberJonathan3213-Nov-08 6:29 
AnswerRe: Will there be new versions in future? PinmemberMember 48046902-Jun-09 20:14 
It depends if #Develop adds some new features, then Rajneesh can also enhance his Scripting Editor.
GeneralStrange exception handling PinmemberGSerjo13-Jul-08 23:58 
GeneralRe: Strange exception handling Pinmemberleosco4-Dec-11 1:19 
GeneralDockPanel Suite Copyright PinmemberPaul Selormey13-Jul-08 21:48 
GeneralArticle Width PinmemberPaul Selormey13-Jul-08 18:16 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140421.2 | Last Updated 13 Jul 2008
Article Copyright 2008 by Rajneesh Noonia
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid