I have the following piece of code:
string actionString = "(p1,p2)=>p1.HP-=20";
var options = ScriptOptions.Default.AddReferences(typeof(SimplePlayer).Assembly).AddImports("Player");
var script = CSharpScript.Create<Action<SimplePlayer, SimplePlayer>>(actionString, options);
SimpleDeck d = new SimpleDeck(Game.GameController.Cards, 100, 100);
SimplePlayer p1 = new SimplePlayer(4000, 100, 100, d);
SimplePlayer p2 = new SimplePlayer(4000, 100, 100, d);
var del = script.CreateDelegate();
del.DynamicInvoke(p1, p2);
System.Console.WriteLine(p1.HP);
I want to achieve the same thing, but instead of having to specify the type of the expected delegate I want something like this:
string actionString = "(p1,p2)=>p1.HP-=20";
var options = ScriptOptions.Default.AddReferences(typeof(SimplePlayer).Assembly).AddImports("Player");
var script = CSharpScript.Create(actionString, options);
SimpleDeck d = new SimpleDeck(Game.GameController.Cards, 100, 100);
SimplePlayer p1 = new SimplePlayer(4000, 100, 100, d);
SimplePlayer p2 = new SimplePlayer(4000, 100, 100, d);
var del = script.CreateDelegate();
del.DynamicInvoke(p1, p2);
That throws me:
error CS8917: The delegate type could not be inferred.
because i don't specify the type I want to be created. If I change the code to:
string actionString = "(SimplePlayer p1,SimplePlayer p2)=>p1.HP-=20";
Is there a way that I can infer the SimplePlayer type without having to explicitly pass it as an argument? And not having to do the following:
var script = CSharpScript.Create<Action<SimplePlayer, SimplePlayer>>(actionString, options);