-
September 10th, 2008, 05:49 AM
#1
Live parsing of C# code.
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
Please vote the posts you find usefull.
---
I'm back
Bigger and badder
Better and bolder
Explaining stuff -
With an improved vocabulary!
-
September 10th, 2008, 10:15 AM
#2
Re: Live parsing of C# code.
Originally Posted by dahwan
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/2...om-c-code.html
Hope it helps !!
MMH
Last edited by MMH; September 10th, 2008 at 10:51 AM.
-
September 10th, 2008, 02:46 PM
#3
Re: Live parsing of C# code.
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.
Code:
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;
}
-Sylvain
-
September 11th, 2008, 02:32 AM
#4
Re: Live parsing of C# code.
1. Yes, it is possible.
2. C# is not scripting language, look at DLR.
3. Check this thread.
Last edited by boudino; September 11th, 2008 at 02:49 AM.
Reason: fixing url
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post.
-
September 11th, 2008, 06:49 AM
#5
Re: Live parsing of C# code.
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#
Please vote the posts you find usefull.
---
I'm back
Bigger and badder
Better and bolder
Explaining stuff -
With an improved vocabulary!
-
September 11th, 2008, 09:22 AM
#6
Re: Live parsing of C# code.
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
-
September 11th, 2008, 02:52 PM
#7
Re: Live parsing of C# code.
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:
Code:
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|