csharp-eval-unity3d icon indicating copy to clipboard operation
csharp-eval-unity3d copied to clipboard

Allow for more than 4 arguments?

Open rotolonico opened this issue 5 years ago • 1 comments

Hello, I was wondering if there are any plans/if it is possible to parse functions with more than 4 arguments or, even better, for a generic number of arguments.

rotolonico avatar Sep 14 '20 10:09 rotolonico

Hi, @rotolonico. Actually there is no Func<T1,T2,T3,T4,T5,TResult> in .NET 3.5 which is target framework for this library.

You can use custom parsing/binding function which is support up to 16 arguments if runtime is .NET 4.8:

			var tokens = Tokenizer.Tokenize(expression);
			var parseTree = Parser.Parse(tokens);
			var expressionTree = parseTree.ToSyntaxTree(cSharpExpression: expression);
			var expressionBinder = new Binder(new[]
			{
				Expression.Parameter(typeof(Arg1T), "arg1"),
				Expression.Parameter(typeof(Arg2T), "arg2"),
				Expression.Parameter(typeof(Arg3T), "arg3"),
				Expression.Parameter(typeof(Arg4T), "arg4"),
				Expression.Parameter(typeof(Arg5T), "arg5"),
			}, resultType: typeof(ResultT), typeResolver: typeResolver);

			return (Expression<Func<Arg1T, Arg2T, Arg3T, Arg4T, Arg5T, ResultT>>)expressionBinder.Bind(expressionTree);

or just pass structure/class in argument:

var args = new MyArgs
{
    number1 = 1,
    number2= 2
};
CSharpExpression.Evaluate<MyArgs,bool>("args.number1+ args.number2", args, "args");

deniszykov avatar Sep 14 '20 11:09 deniszykov