Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Scripting inside Java

Tags:

java

c#

scripting

I know that I can use Lua Script files to manipulate Java Objects by using libraries like LuaJava. I had this idea of using C# scripts instead~

Is it possible to run C# scripts inside Java?

like image 334
GaiusSensei Avatar asked Jul 01 '11 14:07

GaiusSensei


2 Answers

In theory, yes - you can certainly do this in .Net applications and there are Java / .Net interops.

Typically however Java / C# interops are performed through either P/Invoke or COM - both are pretty cumbersome for this sort of thing and so in reality this probably won't work as neatly as you might have imagined.

All the same if you did want to do this I'd probably recommend that you write the "scripting engine" (i.e. wrapper around the C# compiler) in C#, and then have that expose it to Java land via interops, for example:

public ScriptResult(string Script)
{
    // Implemented in .Net
    // Script is a string containing the C# code to execute
}

You then need to think carefully about how your C# scripts are going to be able to access any Java-land functionality, again I imagine the best way would be to implement a .Net wrapper class that calls Java objects through interops.

Using C# as a scripting language from within a .Net application is surprisingly straightforward - for information see:

  • Why You Should Use C# For Your Scripting Language
  • C# As a Scripting Language in your .NET Applications
like image 87
Justin Avatar answered Sep 28 '22 08:09

Justin


Are C# programs "scripts"? Regardless, you could run most all outside programs from via Runtime.exec(...), but be sure to watch for traps: When Runtime.exec() won't.

Things get a bit more tricky if you wish to have two-way communication between C# and Java, which can be done via simple sockets/streams or all the way up to COM interfaces.

like image 30
Hovercraft Full Of Eels Avatar answered Sep 28 '22 08:09

Hovercraft Full Of Eels