How to execute a string as code using Roslyn

  • Thread starter Thread starter antidinosaur
  • Start date Start date
A

antidinosaur

Guest
Here is what I tried:

using System;
using System.Threading.Tasks;
using static HMSan.Game;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
using System.Reflection;

static void Trait_0(double[] Modifiers)
{
//Do something
}
static void Trait_1(double[] Modifiers)
{
//Do something
}

for (int i = 0; i < 2; i++)
{
string myscript = "HMSan.Game.TraitEffects[" + i + "] = HMSan.Trait.Trait_" + i + ";";

await CSharpScript.RunAsync(@myscript,
ScriptOptions.Default.WithReferences(Assembly.GetExecutingAssembly()));

}



"TraitEffects" is a list of delegates. I am trying to loop through the names of a series of methods and put them in "TraitEffects". The problem is that the compiler said I did not have the methods defined yet. What should I do to dynamically get references to these methods?

Continue reading...
 
Back
Top