Is there any C# interpreter that can be used inside C++ and yet still allow .Net access?
I want to use C# scripts for games and I'm not sure how to proceed with that.

- 1,669
- 5
- 22
- 40
-
Use System.CodeDom. An AppDomain if you need to be able to unload the scripts. – Hans Passant Dec 09 '10 at 21:01
7 Answers
You can write a simple class in C#, let's call it ScriptRunner
that would take your C# code as input, compile it at runtime to produce a new assembly in memory, then it will use Reflection to load a specific Type from this new assembly, and will run some method with an expected name.
Then, use COM Interop (for example) to create a ScriptRunner
.NET object from your C++ native application, and you'll be able to use it to run scripts.
Start with:
var myProvider = Microsoft.CSharp.CSharpCodeProvider.CreateProvider();
var myCompiler = myProvider.CreateCompiler();
and it's really easy to continue on your own by using IntelliSense to see what's on the ICodeCompiler
interface.
If you've got some specific questions about this approach please ask.

- 5,989
- 1
- 24
- 26
-
Lovely! I was looking exactly for this! I didn't not want to mix other scripting language, and have one unified language! – Richard Lalancette Nov 26 '20 at 15:16
C# is not an interpreted language, it is a compiled language.
You can write C# scripts, but why not use Python or Ruby or Lua or some other true-blue scripting language?
I feel somewhat dirty mentioning this, but it looks like there is an ECMA compliant C# scripting engine.

- 57,289
- 29
- 176
- 237
-
These are thin lines. C# is interpreted on .NET Micro. Python, Ruby and Lua are compiled. There are jitters for Python and (I think) Ruby. – Hans Passant Dec 09 '10 at 21:00
-
2Some people just really like C#'s syntax. I much prefer languages that use curly brackets anyway, though I do get your point. :) – JC Leyba Dec 09 '10 at 21:08
I believe C# is a compiled language only. Your best bet would be to call the csharp compiler (csc.exe) and load the assembly dynamically.

- 12,268
- 3
- 40
- 51
Is it C++ or C++/CLI? If it's the "normal" C++ I don't think it will be possible to use C# as it is a compiled language and a managed one, your best bet would be to use the managed version of C++ and compile the C# code, then load it.

- 154
- 6
-
1You can always use a C++/CLI wrapper dll to access .NET from a straight native C++ app. – Eclipse Dec 09 '10 at 21:07
-
At the time of this comment, c++/CLI was on windows only. If you want a cross platform solution, you need something else. – Richard Lalancette Nov 26 '20 at 16:32
I've never tried to do that but I think C# is definitely not a suitable language for scripting, for your purpose Lua or Python (for example) are certainly better...

- 8,868
- 6
- 48
- 70
-
I've implemented lua in another game but it doesn't feel right. I'd be much more comfortable with C# – Sanctus2099 Dec 11 '10 at 09:15
I assume you want to use C++ for DirectX/OpenGL support and then want to load in the entire .Net Framework on top of that to support scripting? That would be a very heavy footprint if it were possible. Since C# is compiled into bytecode (just like Java), you would have to precompile your scripts.
Your best best is to to use an opensource scripting language (php, lua, etc).

- 15,777
- 1
- 26
- 42