System.Linq.Dynamic.Core icon indicating copy to clipboard operation
System.Linq.Dynamic.Core copied to clipboard

Accessible types in ParseLambda?

Open MariaCobretti opened this issue 4 years ago • 3 comments

Hi,

I would like to do some mapping with the linq.dynamic package.

var expression = "new System.Collections.Generic.Dictionary<string, string>() { { \"First\", \"A\" }, { \"Second\", \"B\" } }[it]";

var lambda = DynamicExpressionParser.ParseLambda<string, string>(ParsingConfig.Default, true, expression);

var compiled = lambda.Compile();
var input = "First";
var result = compiled(input);

in the parseLambda call I am getting this exception

System.Linq.Dynamic.Core.Exceptions.ParseException: 'Type 'System.Collections.Generic.Dictionary' not found'

any way to make this accessible or is there a better way to achieve this?

MariaCobretti avatar Nov 12 '21 09:11 MariaCobretti

You could try something like

var config = new ParsingConfig
{
    CustomTypeProvider = new CustomTypeProvider(new[] { typeof(System.Collections.Generic.Dictionary<,>) })
};

And try to use this config instead of the default one.

alexeyshockov avatar Nov 16 '21 06:11 alexeyshockov

Adding typeof(System.Collections.Generic.Dictionary<,> will probably not work, because the parser cannot understand the <> in the expression.

I'm taking a look if this can be added easily.

StefH avatar Nov 18 '21 21:11 StefH

yes thats right, it translates to smth like Dictionary`2 as a string which will then cause a syntax error when you call new on it

MariaCobretti avatar Nov 19 '21 08:11 MariaCobretti