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.
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With