Click here to Skip to main content
13,355,726 members (58,088 online)
Click here to Skip to main content
Add your own
alternative version


55 bookmarked
Posted 25 Aug 2008

A Library for Writing/Building Scripts in C#

, 14 Oct 2008
Rate this:
Please Sign up or sign in to vote.
Designed to make it easier to write scripts such as JavaScript in C#


ClockWork Script Builder is a .NET library (2.0) to make the building of scripts more readable and structured.

An extendible architecture lets developers add script building blocks (Script Items) for languages such as the ones provided in the download for JavaScript (Js) and XML (Xs).


Recently I started playing with ExtJs and I ended up needing to build large chunks of JavaScript from .NET code. I've never found a nice way to construct scripts in C# so this time I decided to write a library to help.

Using the Code

(CHM documentation is available from Tony's Wibbles.)

In its simplest form, it is a way to join a bunch of strings together so that each is written on a new line:

Script script = Sb.Script(
    "line 1;",
    "line 2;",
    "line 3;"

string result = script.Render();


It results in the following output:

line 1;
line 2;
line 3;

However there's a lot more to it. Scripts don't just understand strings but accept any type of object. In particular, objects based on an IScriptItem are specifically designed to work with scripts and can be written by anyone to extend scripting abilities.

This example demonstrates the use of some core layout items.

Script script = Sb.Script(    
        Sb.Line("indented ","1","2","3","4","5",";"),
        Sb.Line("indented ","1","2","3","4","5",";"),
            Sb.Line("Some objects"),
            Sb.Line("Number = ", 47),
            Sb.Line("True = ", true),
            Sb.Line("Now is = ", DateTime.Now)

string result = script.Render();


This results in the following output:

    indented 12345;
    indented 12345;
    Some objects
    Number = 47
    True = True
    Now is = 23/08/2008 11:23:00 AM

You will have noticed all the Script items are created using "Sb.". To make scripts easier to write and read, I have created a helper classes for each set of script items. They provide static methods that will create instances of script items for you.

Script Languages

I have currently developed the following sets of script items to help in writing scripts:

Script SetDescriptionHelper Class
CoreCore Script Items to enable constructing formatted scriptsClockWork.ScriptBuilder.Sb
JavaScriptItems to help construct standard JavaScript elements such as Objects, Arrays, Lists, Functions, Calls, Blocks, StatementsClockWork.ScriptBuilder.JavaScript.Js
XMLItems to construct basic XML nodes of Element, Attribute, Text and CData. Also supports direct writing to an XmlElement or XmlDocumentClockWork.ScriptBuilder.XmlScript.Xs
ExtJsClass and Component items designed to make it easier to write subclasses for ExtJs classesClockWork.ScriptBuilder.JavaScript.ExtJs.ExtJs

Points of Interest

DOM & Rendering

All objects added to a script are stored as-is (like a DOM). The string representation of the script is only created when its Render method is called. Among other things, this lets you build the script in steps letting you structure your builder code.

If an IScriptItem is found while rendering then it will use its own Render method to render itself. Other objects are rendered using ToString with an optional format provider.

Rendering is performed using a ScriptWriter which is passed into the Render methods. This object provides the ability to render to several destinations and also handles indentation.


Most items have the option to select a layout mode (Inline, InlineBlock, Block). The effects if the item will render across multiple lines.


This item will do an if-then-else check at render time and then render the object that won.

A collection based ScriptItem (ScriptSet) will result in false if its collection is empty. Therefore you can decide if something is rendered based on the content of another item (all at render time).

Objects can implement the IScriptIfCondition interface if they wish to dynamically provide results for the if condition.


  • 25th August, 2008: Initial version 1.0.0
  • 12th October, 2008: Source file updated (version 1.0.1)


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


About the Author

Software Developer (Senior) Web Site Advantage
Australia Australia
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralGreat job! Pin
Muhammad Dehghan2-Dec-08 18:57
memberMuhammad Dehghan2-Dec-08 18:57 
GeneralT4 Pin
sam.hill14-Oct-08 16:27
membersam.hill14-Oct-08 16:27 
Generalgood Pin
JLKEngine0088-Oct-08 16:44
memberJLKEngine0088-Oct-08 16:44 
NewsRe: good Pin
Tigger214-Oct-08 17:35
memberTigger214-Oct-08 17:35 
GeneralPractical example Pin
Parthasarathy Mandayam26-Aug-08 6:28
memberParthasarathy Mandayam26-Aug-08 6:28 
GeneralRe: Practical example Pin
Tigger24-Sep-08 20:06
memberTigger24-Sep-08 20:06 
AnswerRe: Practical example [modified] Pin
Tigger229-Sep-08 6:58
memberTigger229-Sep-08 6:58 
GeneralSuggestion for Improvement Pin
Andrew Rissing26-Aug-08 4:51
memberAndrew Rissing26-Aug-08 4:51 
GeneralRe: Suggestion for Improvement Pin
Tigger24-Sep-08 20:14
memberTigger24-Sep-08 20:14 
AnswerRe: Suggestion for Improvement Pin
Tigger229-Sep-08 7:10
memberTigger229-Sep-08 7:10 
GeneralRe: Suggestion for Improvement Pin
Andrew Rissing29-Sep-08 8:00
memberAndrew Rissing29-Sep-08 8:00 
GeneralYou saved me. Pin
gwestwater25-Aug-08 13:00
membergwestwater25-Aug-08 13:00 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 14 Oct 2008
Article Copyright 2008 by Tiggerito
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid