7

Possible Duplicate:
Best way to call Managed .NET code from Unmanaged code

I'm curious to know if I could possibly use C# as a scripting language hosted in a c++ native application. I've been working on my own game engine for 5 years but I've never touched the scripting aspect of it. I like how in Unity I can write C# scripts for the different objects in the game.

I did some searching but I couldn't find anything of actual use.

Note that my question is not related to .Net but to C# as a general language.

Just to make sure, I'll state this: I don't want to call native c++ from C#. I want it the other way around.

If anyone could point me to some resources I'd appreciate it.

Community
  • 1
  • 1
Sanctus2099
  • 1,669
  • 5
  • 22
  • 40
  • sounds feasible, as long as there is a managed interface for what you want to call. – Jodrell Oct 09 '12 at 13:22
  • I think perhaps, [this](http://stackoverflow.com/questions/1832293/c-sharp-scripting-language) is a better fit than the suggested duplicate. Although I wouldn't say it's an exact duplicate – James Oct 09 '12 at 13:36
  • 2
    C# is not really designed to be a scripting language. Have you considered (if possible) using JavaScript? Look into Google's V8 engine (which I believe is C++ already), and you can hook native C++ objects from JavaScript...Admittedly JavaScript is designed for the web, but it can be adapted to other applications. – Matthew Layton Oct 09 '12 at 13:40
  • This is simple enough. Just compile the code into a dll then load the compiled file as a reference. – Security Hound Oct 09 '12 at 14:08
  • IronPython, IronRuby or V8 (ECMAScript) would be a better way to go than C# if you want scripting as in dynamic. – Tony Hopkinson Oct 09 '12 at 21:06

3 Answers3

6

Yes, what you want to do is host the CLR in your own App. Here is an MSDN article which goes over it.

http://msdn.microsoft.com/en-us/magazine/cc301479.aspx

The author of that article also has a book out which covers all aspects of it. Once you get the CLR up and running in your App, if you want true scripting, you'll want to dynamically compile code, and cirrus has provided some links on how to do that.

You'll inevitably also need to expose parts of your main App into the CLR namespace. Otherwise, you won't be able to access any of the specific features of the main hosting App.

EDIT: When I say "true scripting" I mean you are using the text files with the extension .cs for your scripts at runtime. It would be more efficient to compile those beforehand and then include the .DLL with the hosted App, however you can do it both ways.

PaulPerry
  • 906
  • 5
  • 14
1

Have a look at CodeDOM. There's also now Reflection.Emit and Linq.Expressions - there are similar questions here;

Is it possible to dynamically compile and execute C# code fragments?

Compiling code dynamically using C#

Compile and run dynamic code, without generating EXE?

Community
  • 1
  • 1
cirrus
  • 5,624
  • 8
  • 44
  • 62
0

You might want to look at the sources of CSRepl by Rickard Nilsson. It's a C# interactive interpreter. It does not host CLR, since it's managed, but creates C# scripting environment.