Note that there are some explanatory texts on larger screens.

plurals
  1. POC# script accessing previously compiled methods
    primarykey
    data
    text
    <p>I'm looking to move a scripting solution that I currently have over to C# as I believe as this will solve some of the issues which I am currently facing when it comes to running on different platforms. I can call functions which are within the script and access their variables, however, one thing that I would like to be able to do is call a function from the class that the script resides in. Does anyone know how I would be able to do this?</p> <p>Here is my code at the minute which is working for calling and access objects within the script, but I would like to be able to call the method "Called" from within the script, but cannot:</p> <pre><code>using System; using System.CodeDom.Compiler; using System.Collections.Generic; using System.Reflection; using Microsoft.CSharp; namespace scriptingTest { class MainClass { public static void Main (string[] args) { var csc = new CSharpCodeProvider (); var res = csc.CompileAssemblyFromSource ( new CompilerParameters () { GenerateInMemory = true }, @"using System; public class TestClass { public int testvar = 5; public string Execute() { return ""Executed.""; } }" ); if (res.Errors.Count == 0) { var type = res.CompiledAssembly.GetType ("TestClass"); var obj = Activator.CreateInstance (type); var output = type.GetMethod ("Execute").Invoke (obj, new object[] { }); Console.WriteLine (output.ToString ()); FieldInfo test = type.GetField ("testvar"); Console.WriteLine (type.GetField ("testvar").GetValue (obj)); } else { foreach (var error in res.Errors) Console.WriteLine(error.ToString()); } Console.ReadLine (); } static void Called() // This is what I would like to be able to call { Console.WriteLine("Called from script."); } } } </code></pre> <p>I am attempting to do this in Mono, however, I don't believe this should affect how this would be resolved.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload