Respondido Using C# as a scripting language

  • sexta-feira, 10 de agosto de 2012 21:32
     
      Contém Código

    Currently I am writing a game using C# and OpenTK, and I would like to implement scripting. I've 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:

    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);
    }

    And this is my script I am trying to compile:

    using System;
    
    namespace FactionScripting {
        public class Test {
            public void Init() {
                Console.WriteLine("Initialized.");
            }
        }
    }

Todas as Respostas

  • sexta-feira, 10 de agosto de 2012 22:08
     
     Respondido
    Never mind, I found where the methods I need are (and it makes a lot of sense now that I found them). I need to go into the types inside of the module and then I can get the methods.
    • Marcado como Resposta csdevrich sexta-feira, 10 de agosto de 2012 22:08
    •  
  • segunda-feira, 13 de agosto de 2012 09:13
    Moderador
     
     

    Hi blakpilar,

    Thanks for sharing your solutions here.


    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us