Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# as a scripting language [duplicate]

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.

like image 874
Sanctus2099 Avatar asked Oct 09 '12 13:10

Sanctus2099


2 Answers

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.

like image 139
PaulPerry Avatar answered Oct 07 '22 03:10

PaulPerry


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?

like image 43
cirrus Avatar answered Oct 07 '22 03:10

cirrus