|
The
Questions
- What
is the CS-Script?
- Other
.NET scripting solutions
- Open-source
script editors
- CS-Script
.NET2.0
- CS-Script
.NET3.0
- Debugging
- CS-Script
and
COM
- CS-Script
and
WebServices
- CS-Script
and Remoting
- Scripting
from Web applications
- Using
MS Visual Studio with CS-Script
- CS-Script
and
Compact Framework
- Is
there are 2 way communications between
Host and Script?
- Script
hosting scenarios
- Integration
with the host application
- Can
CS-Script be used IE?
- How to
fix "Access to the
path ...\*.csc' is
denied" problem.
- Classlesss
scripts.
- Run
script by double-clicking
- Interactive
Environment for CS-Script
- Running
the script without script file
- MS
powershell
vs.CS-Script
- How to
host script engine in
.NET3.5 application
- App.exe.config
equivalent for C# script
- Passing
the object between scripts
- Referencing
assembly
which has
non-standard namespace
- CS-Script
and other languages
-
How to
uninstall CS-Script
manually
-
How
to install
CS-Script
manually
- Installation
on "Windows 7 / Windows
Server 2008 problem: "Could not load
file or assembly 'CSScriptLibrary'"
- Commercial
support
The
Answers
-
What is
the CS-Script?
Q.
I kinda curious, why/when do
need to use script?
A. CS-Script is a CLR
based scripting system which uses
ECMA-compliant C# as a programming
language. CS-Script currently targets
Microsoft implementation of CLR
(.NET1.1/2.0). However CS-Script
offers limited support for Mono and
other CLR programming languages.
CS-Script combines the power and
richness of C# and FCL with the
flexibility of a scripting system.
CS-Script can be useful for the system
and network administrators, developers
and testers. For any one who needs an
automation for solving variety of
programming tasks such as:
- analysing and adjusting system
configuration
- extending functionality of a
software system with flexible
scripting
- configuring development or testing
environment
- automating software batch build
- automating testing, and collecting
test results
- ......
Virtually any type of automation on the
system and application levels.
CS-Script as a development system does
not compete with other C# based
development systems (.NET +Visual
Studio, Mono + SharpDevelop, ...). It is
just a flexible alternative for the
tasks which are hard (or effort
consuming) to implement with
non-scripting approaches.
Back
to top
- Other .NET
scripting solutions
Q.
...I am curious about
whether or not you will get together
with the creator of NScript...
A. I have heard about the
NScript for the first time just after my
article was published in the CodeProject.
It is really good. Looks like it's author
was trying to achieve the same (almost the
same) objective as me. Probably that is
why the internal design of his scripting
engine is similar (to the some extend) to
mine.
Though all C# scripting solutions that I
have seen (including NScript) had some
fundamental design problems, even despite
of sometimes brilliant implementation. All
of them required either custom flavor of
C# or extra config files accompanying the
script file (e.g. NScript).
On my opinion script must be a
self-sufficient distributable software
component. Single script file is all what
you should need in order to run it on the
corresponding script engine. All
successful scripting platforms uses the
same approach (Python, Boo,
JavaScript, Perl, VBScript etc.).
Back
to top
- Is there open source
editor for CS-Script?
Q.
...I like CS-Script. But, I
think its time that it was adapted to
use one of the open source editors or an
editor was created for it...
A. The CS-Script is a runtime
environment only. And as such it is
completely transparent to the syntax of
the script. Because of this there is no
need to develop and maintain any language
specific compiler and development tools.
The existing development infrastructure
associated with the language can be used
without any modification.
A good syntax-aware editor is extremely
important for development and a number of
them are already available. CS-Script uses
just standard C# and this is the reason
why any C# editor can be used to compose
the scripts.
CS-Script can be used with SitePad,
Antechinus, TextPad, UltraEdit,
open-source SharpDevelop IDE, any edition
of VisualStudio and many others. My
favorites are Notepad++ and MS
VisualStudio Express. They are free and
very light for their development tools
categories (Notepad++ is an editor and VS
is a full scale IDE ). However you
can find plenty of others.
The development of yet another C# editor
will require significant time and effort
and would bring very little extra value to
the CS-Script itself.
Back
to top
- CS-Script and .NET 2.0
Q.
I noticed that C# Script for .Net
2.0...Do you intend to add support for
generics?
A. CS-Script does not implement (or
duplicate) any .NET functionality. It just
allows you to use (in a scripting manner)
what ever .NET can offer.
You can script with generics simply
because CS-Script allows execution under
the .NET 2.0 CLR and generics is a part of
the .NET 2.0.
The following script will run just fine:
using System;
using System.Collections.Generic;
class Script
{
static public void Main(string[] args)
{
List<string> myList = new List<>(string);
myList.Add("111");
myList.Add("222");
myList.Add("333");
foreach (string item in myList)
Console.WriteLine(item);
}
}
The same apply to any version
of .NET. If you install it and
specify it's runtime as a target CLR
version in the Configuration console
CS-Script.
Back
to top
- CS-Script and .NET 3.0
Q. What about support for
.NET3?
A. .NET3.0 is based on the CLR 2.0.
This means that virtually any script,
which can be executed on .NET2.0 can
be executed on .NET3.0 as well
.NET3.0 also includes new framework
extensions (eg. WPF, WCF...), which relay
heavily on the
app.config files. Starting from
v1.7.0 CS-Script includes support
for application
configuration files. Practically it
means that if you need to execute the
script (client.cs) written to
exercise WCF functionality, you need to
create corresponding config file (client.cs.config
or client.exe.config)
and use command-line switch
/sconfig. This switch will force the
script engine to execute the script
application in a separate AppDomain with
correct config file.
Back
to top
- Debugging
Q. How can I debug my scripts?
A. If you need to debug your script
firstly make sure you have a CLR
compatible debugger installed. Almost any
CLR IDE can be used as such a debugger.
You can start debugging by using one of
the these possible approaches:
All these approaches are described in
details here.
Back
to top
- CS-Script and COM
Q. How can I instantiate a COM
object?
A. There
are two possible
approaches to
access COM
objects:
-
importing type
libraries
manually
-
simplified
"Single-line COM
access"
approach.
It is
recommended to
use "Single-line
COM access" as
it provides
seamless access
to COM (no
manual steps are
required). It
also allows
referencing the
COM objects by
GUIDS, ProgID or
file name
(.dll/.ocx)
directly from
the code.
The following
script creates
(permanently)
new Environment
Variable by
using the
WScript.Shell
COM object
available on any
Windows platform
as part of the
VBScript.
//css_pre com(WScript.Shell, swshell.dll);
using System;
using swshell;
namespace Scripting
{
class Script
{
static public void Main(string[] args)
{
object envType = "SYSTEM";
IWshEnvironment wshSysEnv = new WshShellClass().get_Environment(ref envType);
wshSysEnv["TEST"] = "MyDirectory";
}
}
}
See Using COM
tutorial
for details.
Back
to top
- CS-Script and WebServices
Q.
C#Script is a great tool! Is
there a way to add web service reference
to the script? Can you give some sample
on how to use Web Service?
A. In order to access WebService
you need to generate service wrapper. Such
wrapper (.cs) is generated for you by
Visual Studio when you add Web reference.
The wrapper can be imported by your script
as any other .cs file.
For convenience you can instruct the
script engine to create the wrapper
automatically thus all can be done from
the script code. The
following
script creates
wrapper
HelloService.cs for the local WebService Hello.
//css_pre wsdl(http://localhost/hello/hello.asmx?WSDL, HelloService);
//css_imp HelloService;
using System;
using System.Xml;
class Program
{
static void Main(string[] args)
{
Console.WriteLine(new HelloWorld().SayHello());
}
}
See CS-Script
and
Web Services
for details.
Back
to top
- CS-Script
and
Remoting
Q.
What
is the way of accessing
Remoting?
A. When you use
Remoting your server application
exposes it's interface in the
following ways:
- interface is defined in
the separate class library
assembly
-
interface is
defined in the
server assembly
-
interface
is defined in
the class
library
assembly
generated with
the
SOAPSUDS.EXE
utility
Remoting
client script
application
just
references the
assembly with
the interface
definition and
uses it in
usual
manner.
This is the
example of how
you can use
Remoting
server CountryList
(CompanyList
MSDN example).
//css_ref CountryList.dll;
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
class Script
{
static public void Main(string[] args)
{
CountryList cLst = (CountryList)Activator.GetObject(typeof(CountryList),
"http://localhost:8086/CountryList",
WellKnownObjectMode.Singleton);
cLst.AddCountry("Australia");
}
}
Interestingly
SOAPSUDS.EXE
is capable of
producing
assembly
containing the
implementation
of the
CountryList
class (proxy
assembly).
Thus instead
of
distributing
the CountryList
class assembly
you can
recreate it on
client site
on-fly.
CS-Script is
distributed
with soapsuds.cs
script. Thus
generation of
the interface
assembly can
be done
automatically
as a
pre-execution
step, what
means all can
be managed
directly from
code.
//css_pre soapsuds(http://localhost:8086//MyRemotingApp/CountryList?WSDL, CountryList, -new);
//css_ref CompanyLists.dll;
using System;
using System.Runtime.Remoting;
.....
Back
to top
- Scripting from Web
applications
Q.
How
to use CS-Script from WebService?
A. Generally speaking there is
no difference if the host application is a
local or Web application. However some
security issues may arise depending on
your particular runtime scenario.
The following is
the code is a
fragment of the
source file,
which Visual
Studio creates
for a new
WebService
project:
public class Service : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld() {
return "Hello World!";
}
.....
This
method can be
modified to use
the scripting
engine for dynamic
code execution.
Just add reference
to
the CSScriptLibrary.dll
and modify
you code:
using CSScriptLibrary;
.....
public class Service : System.Web.Services.WebService
{
static string scriptCode =
"using System;\n" +
"public class Script" +
"{" +
" static public string GetUser()" +
" {" +
" return Environment.UserName;" +
" }" +
"}";
[WebMethod]
public string HelloWorld() {
AsmHelper script = new AsmHelper(CSScript.LoadCode(scriptCode, "",
false));
return "Hello World! From " + (string)script.Invoke("Script.GetUser");
}
.....
Of course you can
load script file
instead of having
script code
hardcoded. In this
case you can even
"step in" into the
script code when
debugging your Web
application.
Note \n after
using System;.
It is required
that all using
statements were
written as a
single line of
code.
Back
to top
- Using
MS
Visual Studio with CS-Script
Q.
I would like to use VS with
CS-Script. How it can be done?
A. You can easily use Visual
Studio 2008, 2010 or Express to
develop your script applications. Visual
studio is a preferred IDE for CS-Script
as it provides comprehensive set of
editing tools, including diseigners and
advanced CLR debugger.
You can read about enabling the
Visual Studio integration here.
Back
to top
- CS-Script
and Compact
Framework
Q.
Does CS-Script work on PocketPC -
Compact Framework 1.x / 2.x ?
A. No Compact Framework (CF) is not
supported by CS-Script. The reason is that
CF does not have implementation of the C#
ICodeCompiler. In other words the
following code would not compile:
ICodeCompiler
compiler
= (new
CSharpCodeProvider()).CreateCompiler();
If, in any future version of CF, Microsoft
includes the CSharpCodeProvider
implementation the CS-Script will work on
such platform.
Starting from version 1.3 CS-Script
supports pluggable
custom compilers. It means that if
you have third-party implementation of the
CLR compiler that can work on CF it can be
used instead of the Microsoft compiler.
Basically you need any workable C#
compiler either managed or native that
would work on CF.
I have tried to resolve the problem by
isolating the compiler from full version
of .NET Framework and bringing it on CF
but did not succeeded. Also I was trying
to use third-party compilers (after all it
does not matter what software does the
conversion C#->IL). The Pocket
C#
compiler looked promising but I
could not get it working. Even despite I
have seen some reports dated 2004 that it
actually did work.
However CS-Script does provide some
limited support for CF (see this article
for details).
Back
to top
- Communications
between
Host and
Script in both
directions
Q.
Is there are 2 way communications
between Host and Script?
A. Yes there is. Such communication
scenario is a case of the "type sharing"
pattern described in the documentation.
You can pass to and from the script an
object of any type which is known/shared
by both the script and the host. That is
why, without any extra work, you can pass
to the script and get back any GAC type
(e.g. System.String, System.Windows.Form).
However if your type (to be shared) is not
registered with GAC you will need to
reference the assembly implementing this
type from the script. Making this assembly
available at runtime can be difficult,
particularly if this assembly cannot be
located by CS-Script easily.
The preferred way of handling such problem
is to use Simplified
Hosting Model. When the script is
executed according this model it has
access to all loaded assemblies of the
host application (including the host
application itself). This CS-Script
mode is enabled by default.
using System;
using CSScriptLibrary;
public class Host
{
public static string greeting = "Hello World!";
static void Main()
{
AsmHelper scriptAsm = new AsmHelper(CSScript.LoadCode(
@"using System;
public class Script
{
public static void SayHello()
{
Console.WriteLine(Host.greeting);
}
}"));
scriptAsm.Invoke("Script.SayHello");
}
}
Note that the script code does not even
references the host application assembly
and yet script engine is capable to
resolve this assembly for the C#
compiler at compile time and for the CLR
at runtime.
Back
to top
- Script
hosting
scenarios
Q.
What is the simplest way to host
the script engine?
A. The
following
are some hosting
scenarios
examples. All of
them illustrate
usage
of scripts in
a code
form (without
having any script
file), however
samples can be
easyly modified to
use script files
instead.
Make sure you have
referenced CSScriptLibrary.dll in
you host application project and
have corresponding using statement:
using CSScriptLibrary;
--------------------------------------------------------------------------
Invoking
static
methods of a
script class.
static int Sum(int a, int b)
{
string scriptCode = "using System;\n " +
"public class Calc " +
"{ " +
" static public int Sum(int a, int b) " +
" { " +
" return a + b; " +
" } " +
"}";
AsmHelper helper = new AsmHelper(CSScript.LoadCode(scriptCode, null, false));
return (int)helper.Invoke("Calc.Sum",
a, b);
}
--------------------------------------------------------------------------
Invoking static
methods
of a script
class with
unloading the
script
assembly.
Note
that the
compiled
script (script
assembly) is
unloaded after
the Subtract()
returns.
static int Sum(int a, int b)
{
//this will also unload and delete the compiled assembly file after the execution
string scriptCode = "using System;\n " +
"public class Calc
:
MarshalByRefObject " +
"{ " +
" static public int Sum(int a, int b) " +
" { " +
" return a + b; " +
" } " +
"}";
string asmFile = CSScript.CompileCode(scriptCode, null, false);
using (AsmHelper helper = new AsmHelper(asmFile, "tempDomain", true))
{
return (int)helper.Invoke("Calc.Sum", a, b);
}
}
--------------------------------------------------------------------------
Invoking
instance
methods of a
script class..
static int Subtract(int a, int b)
{
string scriptCode = "using System;\n " +
"public class Calc " +
"{ " +
" public int Subtract(int a, int b) " +
" { " +
" return a - b; " +
" } " +
"}";
AsmHelper helper = new AsmHelper(CSScript.LoadCode(scriptCode, null, false));
object calc = helper.CreateObject("Calc");
return (int)helper.InvokeInst(calc, "Subtract", a, b);
}
--------------------------------------------------------------------------
Invoking instance
methods
of a script class
with unloading the
script assembly.
static int Subtract(int a, int b)
{
//this will also unload and delete the compiled assembly file after the execution
string scriptCode = "using System;\n " +
"public class Calc : MarshalByRefObject " +
"{ " +
" public int Subtract(int a, int b) " +
" { " +
" return a - b; " +
" } " +
"}";
string asmFile = CSScript.CompileCode(scriptCode, null, false);
using (AsmHelper helper = new AsmHelper(asmFile, "tempDomain", true))
{
object calc = helper.CreateObject("Calc");
return (int)helper.InvokeInst(calc, "Subtract", a, b);
}
}
--------------------------------------------------------------------------
Invoking
static
method
of a classless
script containing
method definition
only.
static int PrintSum(int a, int b)
{
var printSum
= CSScript.LoadMethod(
@"public static void PrintSum(int a, int b)
{
Console.WriteLine((a+b));
}")
.GetStaticMethod();
printSum(1, 2);
}
--------------------------------------------------------------------------
Back
to top
- Integration
with the host
application
Q.
...So the CSS
script must be
able to get
and set
variables,
call functions
in the class
and so on.
My
questions is
how this class
should be
designed...How
to
fix "Access to
the path
...\*.csc' is
denied"
problem.
Traditionally
when dealing
with the
application
extensibility
scenarios the
following
three major
integration
patterns are
used:
Dispatch,
Adapter,
Interface.PrintSum
Dispatch
You may be
familiar with
this approach
if you hosted
COM object
through
IDispatch
interface. In
case of
CS-Script it
may be
illustrated by
the following
code:
//script code
class Script
{
static public void Calculate(){ /*do something*/ }
static public void Refresh(){ /*do something*/ }
static public int Sum(int a, int b){ /*do something*/ }
}
//host code
AsmHelper script = //compile(load) script code
script.Invoke("Script.Calculate");
script.Invoke("Script.Refresh");
int i = script.Invoke("Script.Sum", 1, 2);
Prons:
Simple, no
extra classes
are required
(just host and
script).
Cons:
Some errors
(e.g. method
name, argument
type) cannot
be caught at
compile time.
Harder to read
code.
Adapter
(Wrapper)
You may be
familiar with
this approach
if you hosted
COM object
through MFC
wrappers. In
case of
CS-Script it
may be
illustrated by
the following
code:
//script code
class Script
{
static public void Calculate(){ /*do something*/ }
static public void Refresh(){ /*do something*/ }
static public int Sum(int a, int b){ /*do something*/ }
}
//host code
class Script
{
private AsmHelper script = //compile(load) script code
public void Calculate(){ script.Invoke("Script.Calculate"); }
public void Refresh(){ script.Invoke("Script.Refresh"); }
public int Sum(int a, int b){ return script.Invoke("Script.Sum", a, b); }
}
Script script = new Script();
script.Calculate();
script.Refresh();
int i = script.Sum(1, 2);
Prons:
Easy to read
code (you may
not even know
that class
Script has
dynamic code
behind).
Cons:
Extra class
declaration is
required.
Some errors
(e.g. method
name, argument
type) cannot
be caught at
compile time.
Changes in the
script
interface are
not handled in
the wrapper
class
automatically.
Interface
WCF uses this
technique as
one of a few
possible
configuration
options. In
case of
CS-Script it
may be
illustrated by
the following
code:
//defined
in host,
shared or GAC assembly
interface IScript
{
void Calculate();
void Refresh();
int Sum(int a, int b);
}
//script code
class Script : IScript
{
public void Calculate(){ /*do something*/ }
public void Refresh(){ /*do something*/ }
public int Sum(int a, int b){ /*do something*/ }
public static IScript Create()
{
return new Script();
}
}
//host code
AsmHelper asm = //compile(load) script code
IScript script = (IScript )asm.Invoke("Script.Create");
script.Calculate();
script.Refresh();
int i = script.Sum(1, 2);
Prons:
Easy to read
code.
Type safe (all
method
signatures are
checked at
compile time).
The
relationship
between host
and script is
a clear "can
do" contract
represented by
the interface
definition.
Cons:
Extra
deployment
component
(interface
assembly) may
be required
required.
Using
interfaces
arguably is
the best
approach,
though it
requires a bit
more initial
coding. But of
course in any
particular
hosting
scenario
another
approaches can
be preferable.
Interface
Alignment
(DuckTyping)
This model
allows
execution the
script by
"aligning" it
to the
appropriate
interface
(DuckTyping).
Important
aspect of such
hosting model
that the
script
execution is
completely
type safe and
such editors
as Visual
Studio can
provide full
Intellisense
support.
However even
more
importantly
the script
does not have
to implement
the interface
being used by
the host
application.
The technique
can
be illustrated
by the
following
code:
//script code
using System;
public class Script
{
public void Hello(string
greeting)
{
Console.WriteLine(greeting);
}
void SomeOtherMethod()
{
...
}
}
//host code
using CSScriptLibrary;
public interface IScript
{
void Hello(string greeting);
}
class Host
{
static void Main()
{
var script = CSScript.Load("HelloScript.cs")
.CreateInstance("Script")
.AlignToInterface<IScript>();
script.Hello("Hi there...");
}
}
Prons:
Easy to read
code.
Type safe (all
method
signatures are
checked at
compile time).
The
relationship
between host
and script is
a clear "can
do" contract
represented by
the interface
definition.
No deployment
penalties
(script and
host are
loosely
coupled).
Cons:
Current
implementation
of AlignToInterface
not support
remote
execution
scenarios.
Using
interfaces
this way is
has all
benefits of
using plain
interfaces
(previous
section). But
also it offers
architectural
benefits of
loosely
coupled
components
(script and
host).
Back
to
top
- Hosting
the
CS-Script in
IE.
Q.
Can CS-Script be used in scripts
in IE?
A. No C# script engine cannot be
used in IE. This is because IE uses
different engine (VBScript and JavaScript)
to compile scripting content of a WEB
page. As far as I know you cannot
reconfigure it to use something else. If
you really want to have it in IE you would
need to change the implementation of IE or
write your own Browser. Both options are
not practically possible.
Back
to
top
- How
to
fix
"Access to the
path
...\*.csc' is
denied"
problem.
Q.
I
noticed that
an
UnauthorizedAccessException
occurs after
CSScript.Load()
has been
invoked and
the script is
changed on the
filesystem.
Unhandled
Exception:
System.UnauthorizedAccessException:
Access to the
path '....<sctipt>.csc'
is
denied.
A. This
sort of
problems
usually
indicates that
hoste
application is
trying to
change and
recompile the
script which
is already
loaded and
still in the
memory.
The samples in
the "<cs-script>\Samples\Hosting\Modifying
script
without
restart"
folder
demonstrate
how to handle
the problem.
Back to top
- Classless
scripts.
Q.
...I know
many other
scripting
languages (JS,
python, etc) don't
require class and
function
declarations, yet
still allow them
if so
desired.
Could your engine
do the same?...
A. Yes it is
possible. CS-Script
offers automatic
generation of the
wrapper class if the
script does not have
any class
definition.
Classless support
comes in a few
different flavors,
which should not be
confused:
- Execution
of
classless
script files
with command
line switch
/autoclass.
This feature
is implemented
in the
CS-Script
engine itself.
It is to be
used with
stand alone
scripts:
- User cannot
control the
way auto-class
is generated.
- Script must
have entry
point Main().
Script
example:
using System;
void Main()
{
SayHello("Hello World!");
}
static void SayHello(string greeting)
{
Console.WriteLine(greeting);
}
The
details
can be found here.
- Execution
of
classless C#
code in
script hosting
scenarios.
This feature
is implemented
in the
CS-Script
engine class
library (CSScriptLibrary.dll).
It
is to be used
with C# code
dynamically
executed from
the host
application:
- User cannot
control the
way auto-class
is generated.
- Script is a
C# code in
memory (not a
file).
Example:
var code =
@"public static void Hello(string greeting)
{
SayHello(greeting);
}
static void SayHello(string greeting)
{
Console.WriteLine(greeting);
}";
var SayHello = new AsmHelper(CSScript.LoadMethod(code))
.GetStaticMethod("*.Hello", ""); //or you
can use full name
"Scripting.DynamicClass.Hello"
SayHello("Hello World!");
The
details
can be found here.
- Execution
of
classless script
files
with
Alternative
compiler.
This feature
is implemented
in the
CS-Script
engine
extension library
(CSSCodeProvider.dll).
It
is to be used
with both
hosted and
standalone
scripts:
- Scripts
must have .ccs
extension to
be handled as
classless.
-
User can
control the
way auto-class
is generated
(namespace and
class name) by
using //css_classless
directive.
Script
(script.cs):
//css_classless MyNamespace.MyClass;
static public void SayHello(string greeting)
{
Console.WriteLine(greeting);
}
Host:
CSScript.GlobalSettings.UseAlternativeCompiler =
Environment.ExpandEnvironmentVariables(@"%CSSCRIPT_DIR%\Lib\CSSCodeProvider.dll");
var helper = new AsmHelper(CSScript.Load("script.ccs"));
helper.Invoke("MyNamespace.MyClass.SayHello", "Hello World!");
The
details
can be found here.
For
the
wast majority
of scripting
scenarios
the built-in
support for
classless
scripting is
sufficient. If
you need more
control on
auto-class
being
generated you
can use CSSCodeProvider.dll
distributed
along with the
CS-Script
binaries.
However if you
want ultimate
flexibility
you can
implement Alternative
Compiler,
which can be
considered as
a
"precompiler"
doing some
source code
manipulations/injections
prior the
actual
compilation
with the
normal C#
compiler. You
can find the
tutorial for
implementing Alternative
Compilers
here.
Standard
CS-Script
installation
contains
examples of
classless
scripts in the [cs-script]\Samples\Classless
directory.
Back
to top
- Run script by
double-clicking
Q.
Is there a way to associate
.cs files with cs-script so I can
just type the name of the .cs file
to run it in the command prompt?
A. Yes there is a way. But you
will have to break existing .cs
association if any.
Have a look at how .txt associated
with notepad.exe in registry.
HKEY_CLASSES_ROOT\txtfile\shell\open\command
But I have to discourage you from
doing this. Making script execution
automatic on "opening" file is a
security breach regardless of what
scripting language you are using.
Back
to top
- Interactive
Environment
for CS-Script
Q. Does C# Script have an
Interactive Environment (like
IronPhyton.NET has)?
A. No, full scale interactive
environment is not possible for CS-Script.
Such style of environment is possible only
for truly interpreted languages (eg.
Python).
CS-Script is a statically typed
non-interpretive scripting environment and
as such it has to be compiled fully before
the execution. This is the reason why
CS-Script demonstrates such good
performance. Generally, the CS-Script
script application will take a bit longer
to start but after that it
will perform with the same speed as
it's standalone executable equivalent.
However CS-Script supports classless C#
syntax (it was requested by the users at a
stage). Such syntax does not require any
class declaration and it can be used to
execute short code fragments on fly. This
feature can be used to implement an
environment similar to the Python
Interactive Environment. Similar but not
the same...
"Script
Execution
Interactive Environment emulator" (ScriptEE.cs)
sample from "Online
Samples" librari is an
attempt to emulate script
interpretation. This simple WinForm
application allows execution of the
C# code snippets.
Back
to top
- Runing
the
script without
a script file.
Q. If I
have an application has the script as a
resource embedded in the application, is
there a way to pass the memory block
that contains the script to
cswscript.exe directly? I don't want to
save it to a physical file and the pass
it for security reason.
A. You can use
CSScript.CompileCode(string scriptText,
...) and than execute the assembly.
However the CSScript will create the
script file (in the user Temp folder) and
delete it immediately after the
compilation. That is why it is almost what
you want.
Back
to top
- MS
powershell
vs.CS-Script
Q. What
is
the advantage compared to the new
shell of microsoft? MS powershell
code name : monad.
A. Yes, it is a very
promising powerful scripting
environment. However the only thing,
which relates PS and CS-Script is the
scripting nature and usage of CLR.
Though CS-Script can use any CLR (eg.
MONO).
The purpose of the CS-Script is
to provide developers with ability to
interact with CLR (in a scripting
manner) through language, which was
designed specifically for CLR.
PowerShell is an environment for
performing variety of administrative
tasks. And it is just happened that
it's implementation is a CLR-based. In
other words PowerShell is a
task-oriented and CS-Script is
a CLR-oriented system. I cannot even
say that they actually compete with
each other, but of course there are a
lot of intersections.
The key difference is that CS-Script is
a runtime environment which is
independent from the language it is
using. Basically it is an adapter
between CLR and the language, which
allows scripting execution.
These are some other differences (you
can find more here: Why
CS-Script?):
Back
to top
- How
to host script engine
in .NET3.5 application
Q.
I am currently trying to
evaluate the use of
C#Script for my company
to replace vbs. This has
led me to the ...
example that I want to
execute, however this
fails. I am referencing
cs-script\Lib\Bin\NET
3.5\CSScriptLibrary.v3.5.dll
since I am developing a
3.5 Application with
VS2008.
A. It is
recommended to initialize
the TargetFramework
to
the
value of the desired CLR
version if the host
application is compiled
agains eariier CLR version
(comparing to the latest
CS-Script release). Thus
for hosting CS-Sript
engine (released for .NET
4.0) in the v3.5.
application you will need
to reference corresponding
scriprt engine librare
(CSSCript.v3.5.dll) and
set is TargetFramework
to "v3.5" string value
before the first use of
the engine:
CSScript.GlobalSettings.TargetFramework
= "v3.5";
Back
to top
- App.exe.config
equivalent
for C# script
Q.
Currently I'm building
an .exe and rename my app.config to
app.exe.config in order to have xml
configuration support. That works.
There is unfortunately no chance
(besides the debug sln) to integrate
an app.config at script level.
A.
Starting from
v1.7.0
CS-Script
includes
support for
the
application
configuration files.
Practically it
means that if
you need to
execute the
script (client.cs),
you
need to create
corresponding
config file (client.cs.config)
and use
command-line
switch
/sconfig.
This switch
will force the
script engine
to execute the
script
application in
a separate
AppDomain with
correct config
file. Note:
you casn also
create client.exe.config
instead of
client.cs.config
but in
the same
location,
CS-Script will
still treat it
as a script
app.config
file.
Back
to top
- Passing
the
object between scripts
Q.
Can your C# script engine
return an object in the memory after
running the script. It might be
useful when the returned object from
the first script needs to be passed
o another C# script?
A. Yes it can. Detailed
information on how to do this can be
found in the "'Type sharing' pattern"
article of the CS-Script
documentation or in online help.
The corresponding tutorials are:
Passing
well-known
type between script and host
Passing
custom
type between script and host
Back
to top
- Referencing
assembly
which has non-standard namespace
Q.
...I'm trying to use an
external dll taglib-sharp.dll within
a script.
However once, i load the script it
ends up in a CS0246, that the
namespace TagLib couldn't be
found....
...i've noticed the following: the
dll is named taglib-sharp.dll, but
the namespace is called TagLib.
A. The problem is quite usual
for the development scenarios
involving the assembly with the name,
which does not match it's root
namespace. This is usually the result
of using non-standard naming
convention by the developer(s) of that
assembly.
The common practice is that for a
given assembly all three
naming identities should match. For
example the System.Windows.Forms.dll
has the following naming identities:
1. assembly name - System.Windows.Forms
2. assembly namespace -
System.Windows.Forms
3. assembly file name -
System.Windows.Forms.dll
After the assembly compiled none of
it's identity can be changed. Even if
you are able to rename the file such
an action will interfere with .NET
assembly probing.
The proper solution is to
reference the assembly by it's file
name directly from the script code.
//css_ref taglib-sharp.dll;
using System;
using TagLib;
.....
Back
to top
- CS-Script
and
other languages
Q.
It
would be
really cool if
your script
engine could
be used for
VB.NET as well
as C#, sorta
at the flick
of a switch.
A. From
the name of the
product
(CS-Script) you
may guess that
it is all about
C#. The full
potential of
scripting with
the CS-Script
engine can be
utilised only
when using the
C# code.
However, it is
impossible not
to recognize
that some
developers may
need to work
with other
programming
languages.
Therefore
CS-Script also
provides some
support for
other CLR
languages
(currently
JScript, VB.NET,
C++/CLI).
This support is
provided by the
means of
plug-n-play
compilers (code
providers)
implemented as
an external
assembly. It
means that you
can implement
your own
compiler for
non-C# syntax or
use any existing
one.
See Alternative
compilers
for details.
Back
to top
- How
to uninstall CS-Script manually
Q.
...I try to run/compile
it...and get this as result: "Cannot
use alternative compiler"
What I do wrong ?
A. This particular error means
that the alternative compiler cannot
be found by the script engine, what
indicates that the CS-Script
installation has been altered/broken.
The solution for the problem is to
reinstall CS-Script. Uninstalling can
be done either from configuration
console or by executing css_config.exe
with command line argument /u.
Alternatively, it is possible to
remove CS-Script manually (if the
configuration console or/and css_config.exe
are not available). Manual
uninstallation is simple as CS-Script,
during the installation, does
very few changes to the OS. Do
the following steps if you need to
uninstall CS-Script manually.
- Remove system environment
variable CSSCRIPT_DIR.
- Remove
%CSSCRIPT_DIR% component from the
system environment
variable
PATH.
- If
advanced
shell extensions enabled unregister
shell extensions with regsvr32.exe
e.g. regsvr32 /u
C:\cs-script\Lib\ShellExtensions\CS-Script\ShellExt.cs.{...}.dll
You will need to repeat unregistration
for ShellExt64.cs.{...}.dll if
you use x64 edition of Windows.
Note: you may not be
able to delete ShellExt.cs.{...}.dll
because
Explorer locks
the shell
extension
files until it
restarted.
If for
whatever
reason you do
not want to
uninstall
CS-Script but
want to
execute it
from the
alternative
location (e.g.
source control
repository)
you can just
set CSSCRIPT_DIR
to the
alternative
location of the
CS-Script
binaries.
Back
to top
- How
to install
CS-Script
manually
Q.
...When I try
to install
this I get an
error...
How can I
install it
manually?
A. In
some rare
case it
may be desirable
to perform
manual
installation.
It is rather
ease as CS-Script,
during
the installation,
does very
few changes
to the OS.
Thus the
"installation"
can be
conducted
manually if
required. This
is what is
involved into
CS-Script
"installation":
- Create
system
environment
variable
CSSCRIPT_DIR
with the value
containing
path to the
CS-Script
folder.
- Add
%CSSCRIPT_DIR%
component to
the system
environment
variable PATH.
Optional
shell-extensions
can also be
configured
manually if
required:
regsvr32 /i
C:\cs-script\Lib\ShellExtensions\CS-Script\ShellExt.cs.{...}.dll
Back
to top
- Installation
on "Windows 7
/ Wondows
Server 2008
problem:
"Could not
load file or
assembly
'CSScriptLibrary'"
Q.
...I have
Frameworks
3.5SP1 and 4
installed but
I receive the
following
error message:
"System.IO.FileNotFoundException:
Could not load
file or
assembly
'CSScriptLibrary'"
A. This
can be due to
the new Windows
security
measures. You
may want to
"Unblock" all
CS-Script files
you downloaded.
This can be done
either manually
or with Sysinternals
Streams.exe
utility.
Alternatively
you can
"Unblock" the
zip file first
(from file
properties
dialog), then
everything you
unzip out of it
will also be
unblocked. This
is arguably the
most practical
solution.
Back
to top
- Licencing
and Commercial Support
Q.
I'm thinking to use your
CS-script execution engine as a
plug-in for one of our commercial
product. ... Could we use your
product in commercial environment?
A. The all details of the
CS-Script licencing can be found here.
But to put it simple, the CS-Script is
an open-source initiative and as such
it is free for commercial and
non-commercial use.
You can use it free of charge provided
no modifications are made to the
original CS-Script engine. Any script
engine modifications, if required,
must be conducted through CS-Script
consulting as part of the commercial
support, which attracts some fees .
We already have a successful
experience in customising the
CS-Script engine according to the
customers needs.
Please contact CS-Script
support
for more
details.
Back
to top
|