Using C# as a scripting language

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Currently I am writing a game using C# and OpenTK, and I would like to implement scripting. Ive tried LuaInterface, but whenever I passed parameters to arguments there was some sort of memory leak, and the total memory usage increased by between 0.02 and
0.05 MB per second, which is unacceptable. I would really love if I could implement C# as a scripting language. So far I have compiling from a source file implemented, but whenever I go to load the main module so I can retrieve the methods I would need, no
methods are found. This is my entire Script class:
<pre class="prettyprint private Assembly asm;

public Script(string path) {
CodeDomProvider provider = CodeDomProvider.CreateProvider("C#");
ICodeCompiler compiler = provider.CreateCompiler();
CompilerParameters param = new CompilerParameters();
param.GenerateInMemory = true;
CompilerResults result = compiler.CompileAssemblyFromFile(param, path);
if (result.Errors.Count > 0) {
Console.WriteLine("Errors:");
foreach (CompilerError err in result.Errors) {
Console.WriteLine(string.Format("{0}:{1} - {2}", err.Line, err.Column, err.ErrorText));
}
}

asm = result.CompiledAssembly;
}

public void Init() {
foreach (var m in asm.GetLoadedModules()) {
Console.WriteLine(m.Name);
foreach (var me in m.GetMethods()) {
Console.WriteLine("m: " + me.Name);
}
}
// MethodInfo mi = asm.GetLoadedModules(false)[0].GetMethod("Init");
// mi.Invoke(null, null);
}[/code]
And this is my script I am trying to compile:
<pre class="prettyprint using System;

namespace FactionScripting {
public class Test {
public void Init() {
Console.WriteLine("Initialized.");
}
}
}[/code]

View the full article
 
Back
Top