[.net] Easy Scripting in .NET

Started by
72 comments, last by ppp_vcf 16 years, 5 months ago
Yes, I presume I will have to mix 'n match managed and unmanaged code, but for the ease of using C++ as scripting, I think it is well worth it. Now, I've found documentation on mixing managed code, but I haven't been able to find any C++ .Net compiler nor have I been able to figure out how to utilize a cross language (C++, MC++, and C#).
Advertisement
If you can run managed code from c++, why not use the method outlined in this thread and make the scripts c#? One of the common reasons to use scripts is that it's simpler. Scripting in c++/clr wouldn't be what I'd call simple :P
Well, my original idea was to have my program (C++) use scripts (C#). The question is how do I access the C# System.Compiler? In C++ (native) I only have the namespace System which doesn't have any compiler... I am unsure where I can get the System.Compiler (and it's friends), I guess this would fall under multi-language issues?
The namespaces you want are System.CodeDom and System.CodeDome.Compiler, not System.Compiler. But otherwise, I'm afraid this is totally outside my experience.
Quote:Original post by luke2
Well, my original idea was to have my program (C++) use scripts (C#). The question is how do I access the C# System.Compiler? In C++ (native) I only have the namespace System which doesn't have any compiler... I am unsure where I can get the System.Compiler (and it's friends), I guess this would fall under multi-language issues?


You could possibly have an interpreter for C#->C++ then execute it....
Quote:Original post by gharen2
I doubt you could compile a single line. I'm pretty sure that the script has to be a complete source file.

You could get around this by having most of the script already stored in a string, and insert the line the user entered at the appropriate place.

So have the following string in memory:

*** Source Snippet Removed ***

So when the user enters the line of code into the console, the string containing that line of code is inserted into this one. Compile the script, and use reflection to invoke ScriptContainer's Execute method.


I believe you can execute 'one-liners' if you manually parse the command line and emit IL to execute. Someone had an example of this at some point when we were trying to find a work-around for not being able to unload the dynamically compiled assemblies. It might be earlier in this thread or one of the similar .Net Scripting threads...
DynamicMethod is what you are looking for.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

That has to be the single coolest thing I've ever seen.

respect for the .net framework++
Is there any way to make this safe?
Using a runtime compiler to compile .cs-files and have them act as scripts will allow the script-writer the same level of access to the host system as the application executing the script.

So is there any way to limit the usage of namespaces and/or ImportDLL-stuff. Just to make sure that file/network I/O is limited to the methods exposed by the host application.
//Geron ? ,,,__oOo_(O.O)_oOo__| | | | | | | | || | | | | | | | |
You should look at the sample provided in the latest DirectX SDK called Scriping. They provide an excellent methode for restricting access of a script.

GBS
GBS

This topic is closed to new replies.

Advertisement