Click to See Complete Forum and Search --> : Live parsing of C# code.
dahwan
September 10th, 2008, 05:49 AM
I was thinking of starting my career as a game programmer a bit early by making a few very simple arcade games, but i really need it to accept scripting. In fact i was thinking of scripting the entire game!
My plan so far is to have the program start with a reference to a file with C# code in it. I want the program to "copy&paste" the code in the file to a method and run it. I heard from a friend that this could be done but i have no idea how.
I checked out the assembly class but only managed to compile code to a new program, which is not what i need.
So to sum up:
I need to make a program that is able to run C# code directly from an uncompiled cs file.
Is this possible?
Thanks in advance
MMH
September 10th, 2008, 10:15 AM
So to sum up:
I need to make a program that is able to run C# code directly from an uncompiled cs file.
Is this possible?
You mean to ask, you have a code kept in .cs file that you want to compile it from your application and make an exe. ?
If what i assumed is correct, then it can be done.
Have a look
http://code-in-action.blogspot.com/2008/09/how-to-create-exe-file-from-c-code.html
Hope it helps !!
MMH
sylvainboisse@hotmail.com
September 10th, 2008, 02:46 PM
I think what our pal wants is actually to avoid having to compile in a separate executable file.
I am no expert, but I think you need to generate an assembly from your .cs file in order to execute it, the sample on the link MMH provided builds a .exe, but it could surely be adapted to genrate a dll instead.
Once your dll is compiled, it would be easy for you to load it and retrieve the class and methods you would want to access, and execute them.
public static Type LoadFromFile(string fileName, string className)
{
//Load assembly from file
Assembly myAssembly = Assembly.LoadFrom(fileName);
// Get the Type object for this class
Type ClassType = myAssembly.GetType(className);
//return the type
return ClassType;
}
boudino
September 11th, 2008, 02:32 AM
1. Yes, it is possible.
2. C# is not scripting language, look at DLR (http://en.wikipedia.org/wiki/Dynamic_Language_Runtime).
3. Check this thread (http://www.codeguru.com/forum/showthread.php?t=460469).
dahwan
September 11th, 2008, 06:49 AM
I want to do the equal of the javascript function that takes a string and executes it as javascript code. But i want it in C#
mmetzger
September 11th, 2008, 09:22 AM
The Mono project just released some early stuff for a C# REPL (ie, a C# Shell) - which basically requires an Eval() method for C#...
In other words - http://tirania.org/blog/archive/2008/Sep-10.html
crackersixx
September 11th, 2008, 02:52 PM
You can do this, but you will run into problems when the "external" compiled method needs to access data from your parent application.
You can tell the compiler to generate a dll in memory that you can then invoke methods from.
Rough example:
CodeDomProvider provider = null;
CompilerResults cr = null;
provider = new Microsoft.CSharp.CSharpCodeProvider();
CompilerParameters cp = new CompilerParameters();
//Add referenced assemblies
cp.ReferencedAssemblies.Add("system.dll");
cp.ReferencedAssemblies.Add("system.windows.forms.dll");
// Compile in memory
cp.GenerateInMemory = true;
//Compile the code
cr = provider.CompileAssemblyFromSource(cp, sourceCodeFile);
//Load the new assembly - object
Assembly loAssembly = cr.CompiledAssembly;
object loObject = loAssembly.CreateInstance("SomeObject");
//Call method
object loResult = loObject.GetType().InvokeMember("SomeMethod",BindingFlags.InvokeMethod,null,loObject,loCodeParms);
I pulled this code from a project where I attempted to do the same thing you are suggesting (scripting a game).
It wasn't long before I realized this was the wrong way to go about this.
Your mileage may vary.
-C6
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.