43

Is there a ready-to-use C# interpreter out there, that is does not rely on runtime compilation?

My requirements are :

  • A scripting engine
  • Must Handle C# syntax
  • Must work on medium-trust environments
  • Must not use runtime compilation (CodeDomProvider ...)
  • Open source (or at least free of charge both for personal and professional use)

If this is not clear, I need something like Jint (http://jint.codeplex.com/), but which allows me to write C# scripts instead of JavaScript ones.

Thanks for your help.

Danny Varod
  • 17,324
  • 5
  • 69
  • 111
Manitra Andriamitondra
  • 1,249
  • 1
  • 15
  • 21
  • 4
    Can you elaborate on why you need C# syntax without runtime compilation? Due to the way the C# language is specified, and the way in which the CLR primitive types are implemented, I don't believe what you're asking for is even possible. But my guess is that you don't truly need the C# language for what you're trying to do. – Daniel Pryden Nov 02 '09 at 23:00
  • 6
    1) runtime compilation involve assembly generation wich requires full trust mode, I'm working on a medium trust asp.net environment. 2) In some particular case, I need to evaluate 10000+ small scripts that are all differents, compiling all of them would be just dramatically slow and using assembly cache would be useless and would quickly kill my AppPool. – Manitra Andriamitondra Nov 02 '09 at 23:20
  • 8
    ... 3) I don't want to write anything else than C# :p – Manitra Andriamitondra Nov 02 '09 at 23:23
  • 2
    A C# running in the DLR would be intresting. – AndreasN Nov 07 '09 at 16:05
  • 1
    Does reflection work on medium-trust environments? – Danny Varod Nov 07 '09 at 16:07
  • Would it work to use ScriptSharp to compile to JavaScript for Jint? Where are these 10000 scripts coming from? I'm still a bit unclear about what you're doing. – Nosredna Nov 07 '09 at 16:21
  • The 10000 scripts could come from a template engine that would generate csharp code but I have to handle thousands of templates. – Manitra Andriamitondra Nov 09 '09 at 09:30
  • Dynamic methods work in non-full-trust environments, you might get away with them unless you need to create new types. – Anton Tykhyy Nov 10 '09 at 01:03
  • wow, Dynamic methods is something still mysterious :p and unfortunatly doesn't work on our platform (.NET 3.5) – Manitra Andriamitondra Nov 10 '09 at 10:24
  • You can easily do this with Mono.CSharp as shown here: https://stackoverflow.com/questions/11569177/mono-csharp-how-do-i-inject-a-value-entity-into-a-script/67819345#67819345 – henon Jun 03 '21 at 10:09

12 Answers12

20

Have you looked at paxScript.NET?

Sagie
  • 243
  • 1
  • 10
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
14

Check out the Mono project. They recently demoed CsharpRepl which sounds like what you're after. The PDC 2008 video here.


Update:
On a close look it seems like using Mono.CSharp service to evaluate scripts won't be possible. Currently it is linked to the Mono runtime and they don't expect it to run in a medium trust environment. See this discussion for more info.

On alternative possibility is to include the Mono C# compiler (sources here) in your project and use it to generate assemblies that you load from the file system. It you are worried about the resources required to load all those assemblies you might have to load them in a separate AppDomain.

Luke Quinane
  • 16,447
  • 13
  • 69
  • 88
  • Hi, is it possible to 1) allow scripts to interact with my compiled aseemblies (wich are targeted to .NET 3.5 and not Mono) 2) use it in an asp.net application (and not in shell mode) ? – Manitra Andriamitondra Nov 03 '09 at 09:22
  • @manitra, 1) Mono assemblies interact just fine with .NET assemblies. I have many apps that use some assemblies from Mono and some from .NET. 2) It doesn't matter what kind of app you call them from. – Frank Krueger Nov 12 '09 at 16:10
  • The problem about this solution is that it's a compiler. I can't afford the warmup latency because I have a lot of different small scripts. – Manitra Andriamitondra Nov 12 '09 at 20:55
  • [C# Interpreter in mono has been removed](https://github.com/mono/mono/pull/1248). – zwcloud Apr 08 '17 at 16:34
5

I need to evaluate 10000+ small scripts that are all differents, compiling all of them would be just dramatically slow

Interpretting these would be even more painfully slow. We have a similar issue that we address as follows:

We use the Gold Parser project to parse source code and convert it to an XML based 'generic language'. We run this through a transform that generates VB.Net source code (simply because it's case insensitive). We then compile these using the .Net runtime into a standalone DLL, and call this using heavily restricted access.

It sounds as though you are creating something like a dynamic website where people can create custom modules or snippets of functionality, but using C# to do this introduces a couple of main problems; C# has to be compiled, and the only way around this is to interpet it at runtime, and this is unfeasible, and even if you do compile each snippet then you end up with 10,000 DLLs, which is impractical and unusable.

If your snippets are rarely changing, then I would consider programatically wrapping them into a single set of source, with each having a unique name, then compile them in a single shot (or as a timed process every 10mins?). This is what we do, as it also allows 'versioning' of peoples sessions so they continue using the version of DLL they had at the start of their session, but when every session stops using an old version then it's removed.

If your snippets change regularly throughout the day then I would suggest you look at an interpretted scripting language instead, even PHP, and mix your languages depending on the functionality you require. Products such as CScript and LinqPad all use the CodeDomProvider, because you have to have IMSL somewhere if you want to program compiled logic.

The only other option is to write your own interpretter and use reflection to access all the other libraries you need to access, but this is extremely complex and horrible.

As your requirements are effectively unachievable, I would suggest you take a step back and figure out a way of removing one or more restrictions. Whether you find a FullTrust environment to compile your snippets in, remove the need for full code support (i.e. move to interpretted code snippet support), or even change the whole framework to something non .Net.

Jason
  • 31
  • 2
4

I have written an open source project, Dynamic Expresso, that can convert text expression written using a C# syntax into delegates (or expression tree). Expressions are parsed and transformed into Expression Trees without using compilation or reflection.

You can write something like:

var interpreter = new Interpreter();
var result = interpreter.Eval("8 / 2 + 2");

or

var interpreter = new Interpreter()
                .SetVariable("service", new ServiceExample());

string expression = "x > 4 ? service.SomeMethod() : service.AnotherMethod()";

Lambda parsedExpression = interpreter.Parse(expression, 
                        new Parameter("x", typeof(int)));

parsedExpression.Invoke(5);

My work is based on Scott Gu article http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx .

Davide Icardi
  • 11,919
  • 8
  • 56
  • 77
4

LINQPad can work as a code snippet IDE. The application is very small and lightweight. It is free (as in beer) but not open-source. Autocompletion costs extra but not much ($19).

Edit: after reading over the comments in this post a little more carefully, I don't think LINQPad is what you want. You need something that can programmatically evaluate thousands of little scripts dynamically, right? I did this at work using Iron Ruby very easily. If you're willing to use a DLR language, this would probably be more feasible. I also did some similar work with some code that could evaluate a C# lambda expression passed in as a string but that was extremely limited.

Jason Marcell
  • 2,785
  • 5
  • 28
  • 41
3

or http://www.csscript.net/ Oleg was writing a good intro at code project

Marc Wittke
  • 2,991
  • 2
  • 30
  • 45
2

It doesn't handle exact C# syntax, but PowerShell is so well enmeshed with the .NET framework and is such a mature product, I think you would be unwise to ignore it as at least a possible solution. Most server products being put out by Microsoft are now supporting PowerShell for their scripting interface including Microsoft Exchange and Microsoft SQL Server.

Lee
  • 18,529
  • 6
  • 58
  • 60
  • 2
    Thanks Lee but, C# syntax is really the essential part of my question. There are plenty of existing interpreters out there (Jint is prolly the most close of my requirements) but, I'm tired of learning multiple languages and just need my stuffs with c#. Thanks for the answer anyway :) – Manitra Andriamitondra Nov 09 '09 at 09:38
1

I believe Mono has mint, an interpreter they use before implementing the JIT for a given platform. While the docs in the official site (e.g. Runtime) say it's just an intermediate state before consolidating the jitting VM, I'm pretty sure it was there the last time I compiled it on Linux. I can't quite check it right now, unfortunately, but maybe it's in the direction you want.

dodecaphonic
  • 449
  • 3
  • 9
  • Hmm, it looks like a quite dead project ? And is it working on Windows ? – Manitra Andriamitondra Nov 10 '09 at 10:22
  • It isn't dead -- it just loses relevance after the VM has been ported. I've read some more, and it wouldn't do what you want in the end: _mint_ interprets CLI bytecodes, not C# files the way paxScript does. Sorry I couldn't help out further. – dodecaphonic Nov 10 '09 at 11:43
  • -1 mint was the IL interpreter. It was obsoleted loooong time ago. Now there's only an IL JIT. – Gonzalo Nov 11 '09 at 21:45
1

bungee# is the thing that you want, in a short time, bungee sharp will be an open source project in

http://www.crssoft.com/Services/Bungee

. you can create scripts with the same c# syntaxt. there is no assembly creation when you run the script, interpretation is done on the fly, so the performance is high. all the keywords are available like c#. I hope u will like it very much..

Burcu Co
  • 83
  • 2
  • 6
0

I faced the same problem. In one project I was looking to provide a generic way to specify conditions controlling when a certain letter has to be generated. In another project the conditions were controlling how cases were assigned to queues. In both of them The following solution worked perfectly:

  1. The Language for the snippets - I chose JScript so that I do not have to worry about variable types.
  2. The Compilation - yes it requires full trust, but you can place your code in a separate assembly and give it full trust. Do not forget to mark it with AllowPartiallyTrustedCaller attribute.
  3. Number of code snippets - I treated every snippet as a method, not a class. This way multiple methods can be combined into a single assembly
  4. Disk usage - I did all compilation in memory without saving the assembly to disk. It also helps if you need to reload it.

All of this works in production without any problems

Edit

Just to clarify 'snippet' - The conditions I am talking about are just boolean expressions. I programatically add additional text to turn it to methods and methods to compilable classes.

Also I can do the same with C# although I still think JScript is better for code snippets

And BTW my code is open source feel free to browse. Just keep in mind there is a lot of code there unrelated to this discussion. Let me know if you need help to locate the pieces concerning the topic

mfeingold
  • 7,094
  • 4
  • 37
  • 43
0

This one works really well c# repl and interactive interpreter

Barton
  • 547
  • 5
  • 11
  • Let me qualify that - It works well for most basic needs. It lacks good error reporting. It may be time for me to grab the source and revive this effort. – Barton Sep 05 '14 at 20:29
-1

Is Snippet Compiler something you looking for?

Trav L
  • 14,732
  • 6
  • 30
  • 39