Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • December 06, 2016, 12:03:52 AM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: C# as a Script  (Read 4058 times)

Ehtyar

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 1,237
    • View Profile
    • Donate to Member
C# as a Script
« on: October 20, 2008, 03:05:57 AM »
Hi all.
I'm posting this in relation to this thread. It's intended to be just a quick intro to using C# as a scripting language instead of a compiled language.
Due to the fact that CLR compiled applications must be accompanied by a compiler, .net languages provide a unique opportunity to compile code on-the-fly. In this case, we're using a compiled executable to call the compiler to compile code we provide at the moment we wish to execute it via Microsoft.CSharp.CSharpCodeProvider(), thus simulating the behavior of a scripting language. There are two engines I've found to accomplish this task:
  • C# Script for .NET 2.0 is the engine I eventually chose. It is extremely small and relatively simple, therefore is also a cinch to modify.
  • CS_Script is the bulkier, yet more feature full of the two, this one provides for caching of the compiled script, and for embedding the "interpreter" itself into any .net application.
I chose the former both because it was far easier to modify, and because it runs happier on Mono than the alternative. Attached is a console and GUI build of my slightly modified version (scripts written for the console misbehave if run from the GUI interpreter). The changes include:
  • The System, System.IO, System.Text, System.Collections.Generic, and System.Windows.Forms namespaces are included by default, and the necessary assemblies are referenced.
  • The full path of the executing script will be provided as the first argument on the command line.
  • The code generated from the scripts will be optimized and will not contain any debug information.
  • The interpreter will compile code that does not contain a Main(). However, if you provide a Main(), you must also provide an enclosing class { } , and optionally, a namespace { }. This provides for multiple classes or namespaces in a single script.
I hope this info will be of use to somebody. As always, feedback is much appreciated

Ehtyar.


mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 36,406
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: C# as a Script
« Reply #1 on: October 21, 2008, 04:35:58 AM »
Cool.  Looking forward to seeing how you use it.

Ehtyar

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 1,237
    • View Profile
    • Donate to Member
Re: C# as a Script
« Reply #2 on: October 21, 2008, 05:49:11 AM »
I used it for my binary parsing problem Mouse Man (first link). I won't show the script here because it's not appropriate for DC, but if anyone would like some info on how things went, feel free to ask any questions.

Ehtyar.