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.
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).
Related
I need my superusers to write some basic expressions like (getting today, or getting first day of last month, or just returning a default int value like 40), that I can later execute and get the result. It will be used for basic scripting that will provide an optional default value for a report parameter. It would be nice if it did not require any additional installation
So does c-sharp support any scripting languages that it can happily execute and evaluate?
thanks in advance,
It's an ASP.Net application and .Net Framework 4.5
LUA is a scripting language that is able to be used from C#, take a look at LuaInterface
You can use NLua (http://nlua.org/ https://www.nuget.org/packages/NLua/)
Super easy to integrate, and work on any platform.
I recently used IronPython to provide "scripting" to a program. You can embed the IronPython runtime in your program and let it execute scripts passed as a string. Look here for some examples.
You can also execute C# code compiled on the fly. See here.
Short answer, yes.
There are a lot of different possible script engines for pretty much any possible scripting language.
Personally, i like to use C# for scripts too.
Then make the program load the script file and compile it runtime.
Is there a way to run F# code in C#? I have written an app in C# - I'd like to provide it the ability to execute F#, read Record objects, enumerate lists from F#. What is the current solution for this? I know in the future there will be probably be a way to do this via an update to Roslyn. Also, curious how to run F# code in F#, currently. Is there a way to do that easily?
Currently, you have to make the F# a library, and then call it from C#. Since F# is just .NET, using the F# library is (mostly) just like using any other C# library. You might want to look into FSharpX for this portion, however, as it provides some utilities to ease calling F# from C# and vice versa.
If you want to compile the F# dynamically from C#, you'll need to use the F# CodeDom implementation from the F# Power Pack. This will let you use CodeDom to compile F# on the fly, and then execute it from C#.
Note that Roslyn will not help here, even when it's released, as it won't support F# as a code model, only C# and VB.Net. It would potentially be possible to use Roslyn to manipulate and compile C# from within F#, but not the other way around.
In addition to the options that Reed already mentioned, there are a two other alternatives based on using the F# Interactive - you can either run it as a separate process or you could use the open-source release to reference it and call it directly (which is quite challenging, though).
So in summary, you have three options:
You can start the fsi.exe process and evaluate F# code in the F# Interactive environment (by sending F# code to the process by standard input). The F# binding for MonoDevelop is a good example how to do this (see InteractiveSession.fs).
The only difficulty is communication between the two processes, but I think this should be doable using a .NET remoting (or WCF) channel between the two. You can also just read the standard output from F# Interactive, but that might be a bit too simple.
You could use the open-source release of F# compiler & tools to reference relevant parts of the F# compiler and call them (as a library) to evaluate F# code. This would be the best option and it would give you pretty much the same options you get with interactive C# from Roslyn.
Sadly, this is not quite an easy task - I was playing with the idea of doing this and I made some progress (I can share that), but it is not done yet. The idea is to take the source code of fsi.exe (here), remove all UI and turn it into a library.
Using CodeDOM, you can invoke the F# compiler (fsc.exe) to compile F# code into a stand-alone application (or library), load it dynamically and run it. A disadvantage is that this will start a new instance of the compiler each time you use it and so it may be a bit slow (depends on your scenario)
Since the question has been asked, there has been a lot of exciting development :-)
You can now do this really easily using the F# Compiler Services project:
The F# compiler services package is a component derived from the F# compiler source code that exposes additional functionality for implementing F# language bindings, additional tools based on the compiler or refactoring tools. The package also includes F# interactive service that can be used for embedding F# scripting into your applications.
I know that I can use Lua Script files to manipulate Java Objects by using libraries like LuaJava. I had this idea of using C# scripts instead~
Is it possible to run C# scripts inside Java?
In theory, yes - you can certainly do this in .Net applications and there are Java / .Net interops.
Typically however Java / C# interops are performed through either P/Invoke or COM - both are pretty cumbersome for this sort of thing and so in reality this probably won't work as neatly as you might have imagined.
All the same if you did want to do this I'd probably recommend that you write the "scripting engine" (i.e. wrapper around the C# compiler) in C#, and then have that expose it to Java land via interops, for example:
public ScriptResult(string Script)
{
// Implemented in .Net
// Script is a string containing the C# code to execute
}
You then need to think carefully about how your C# scripts are going to be able to access any Java-land functionality, again I imagine the best way would be to implement a .Net wrapper class that calls Java objects through interops.
Using C# as a scripting language from within a .Net application is surprisingly straightforward - for information see:
Why You Should Use C# For Your Scripting Language
C# As a Scripting Language in your .NET Applications
Are C# programs "scripts"? Regardless, you could run most all outside programs from via Runtime.exec(...), but be sure to watch for traps: When Runtime.exec() won't.
Things get a bit more tricky if you wish to have two-way communication between C# and Java, which can be done via simple sockets/streams or all the way up to COM interfaces.
You can do it the other way around. Have a look at http://www.ikvm.net/ - it allows object/library reuse from one language in the other.
I'm just looking for a best way to re-use code written in c#, in my c++ projects. Creating a com\service doesn't look like a best option for my needs. How difficult it is to export c# code into a dll and use it in c++? can i get some suggestion or example? is this usual requirement or ? Please help me.
i use win7, VS2008, win7sdk
Thanks & Rgds, ~calvin
Executing managed code from an unnamaged executable is possible, though not quite easy. You can look into this article for an introduction and this book to go further.
I personally would avoid this kind of things in most cases and, if possible, switch the C++ project to C++/CLI to obtain an immediate compatibility with .Net assemblies for a minimal cost.
You can't export C# code into a native dll afaik. At least without very much pain in your buttocks. You should have thought beforehand and write the reusable part in C, thus creating a native DLL which could be used from all languages.
Also, you could try managed C++ - I personally hate it... but there you go
In case you need to use code written in C# C++, then you need to first see what all data types you would be passing from your C++ code to C# code.
1. Basic data types like int, enum etc can be passed from unmanaged to managed code.
2. in case you want to pass on class object, than you need to use marshalling.
If you can't use COM (if the .NET code is already written for example), then you can host the CLR, but this is a long road...
See these other articles
How to load CLR into process and Create a C# DLL That Can Be Imported in a Delphi App Using stdcall - Possible?
I know CodeGear made BabelCode that uses the Code DOM to convert C# to Delphi for .NET. I am curious if there are any other similar tools to convert C# to Delphi Prism? If not, what is involved in using the Code DOM to create one (yeah, that is open ended!)
Update: This is now built into Delphi Prism. Just paste or import your C# and you have Oxygene aka Delphi Prism Code.
It's in its early stages but Carlo just published a first revision of his open source "C# to Oxygene" tool:
http://code.remobjects.com/p/csharptoxy/
One option I saw was to use .NET Reflector on the C# compiled assembly. It has an Oxygene syntax. That is kind of the long way around and not exactly optimal.
Use BabelCode to convert your C# to Delphi, and then use Oxidizer to convert Delphi to Prism. Not a stellar idea, I realize, but it might at least be a little more automatable than going through Reflector. Good luck.