开发者

C# scripting from C++?

开发者 https://www.devze.com 2023-01-30 07:54 出处:网络
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 s开发者_高级运维ure how to proceed with that.You can write a si

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 s开发者_高级运维ure how to proceed with that.


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.


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.


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.


YOu can have .net dlls and use them in your C++ code.


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.


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...


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消