tags:

views:

1553

answers:

10

Hi,

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.

+8  A: 

Have you looked at paxScript.NET?

Mitch Wheat
Thanks for your reply, my office proxy is currently blocking this url (as spam), don't know why, I'll check it at home, tonight.
Manitra Andriamitondra
I used paxScript.NET. It works great.
Nestor
I just tested it, it looks great but I forgot to mention that I needed an open source tool.+1 for this answer anyway.
Manitra Andriamitondra
I hate office proxies! :-<
Danny Varod
A: 

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

Marc Wittke
Hello Marc, CsScript uses runtime compilation so it wont work on medium trust environnments.
Manitra Andriamitondra
Ah, sorry, probably I was rushing over this constraint in your question. Now I get the point.
Marc Wittke
It's ok. Anyway, Thanks for your quick reply :)
Manitra Andriamitondra
+6  A: 

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
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
@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
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
+2  A: 

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
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
A: 

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
Hmm, it looks like a quite dead project ? And is it working on Windows ?
Manitra Andriamitondra
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
-1 mint was the IL interpreter. It was obsoleted loooong time ago. Now there's only an IL JIT.
Gonzalo
A: 

Is Snippet Compiler something you looking for?

rockacola
I can't afford the compile time of a script, and further more, It won't work on a medium trust environnement.
Manitra Andriamitondra
A: 

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 M
Thanks for your answer. Indeed, Linqpad was not what I'm looking for but it's still a great tool :)
Manitra Andriamitondra
A: 

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
+1  A: 

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
A: 

How about DOES.NET http://does.dataodyssey.com ?

Andrey