Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unknown identifier IDictionary #264

Closed
rob53twenty opened this issue Nov 22, 2022 · 4 comments · Fixed by #268
Closed

Unknown identifier IDictionary #264

rob53twenty opened this issue Nov 22, 2022 · 4 comments · Fixed by #268

Comments

@rob53twenty
Copy link

rob53twenty commented Nov 22, 2022

I am trying to get dynamic expresso to interpret this line of code:

(((IDictionary<string, object>)o)["Diesel % in oil"].ToString().StartsWith("<") ? ((IDictionary<string, object>)o)["Diesel % in oil"].ToString().Replace("<", String.Empty) : ((IDictionary<string, object>)o)["Diesel % in oil"].ToString())

When it executes I get the following exception Unknown identifier 'IDictionary' (at index 3).

I am testing for a "<" at the start and then replacing all instances of the "<" (excuse the terrible key name - out of my control that one!).

I have tried adding typeof(System.Collections.Generic.IDictionary<string, object>) as a reference to the interpreter object, but no joy.

The full code:

    var interpreter = new Interpreter();
    interpreter.Reference(typeof(System.Collections.Generic.IDictionary<string, object>));
    interpreter.SetVariable("o", o);
    return (T)interpreter.Eval(code);

I'm sure I'm missing something simple.

@davideicardi
Copy link
Member

Can you try instead with this code

o["Diesel % in oil"].ToString().StartsWith("<") ? o["Diesel % in oil"].ToString().Replace("<", String.Empty) : o["Diesel % in oil"].ToString()

?

And configuring interpreter with:

    var interpreter = new Interpreter();
    interpreter.SetVariable("o", o, , typeof(IDictionary<string, object>));
    return (T)interpreter.Eval(code);

If still doesn't work can you try to reproduce the bug with an unit test similar to the one that we already have in the source code?

@metoule
Copy link
Contributor

metoule commented Nov 27, 2022

For generics, the reference type must be the generic type definition:

interpreter.Reference(typeof(System.Collections.Generic.IDictionary<,>));

@metoule
Copy link
Contributor

metoule commented Nov 27, 2022

I've opened a PR to throw an exception when trying to register a generic type that's not a type definition.

@rob53twenty
Copy link
Author

rob53twenty commented Nov 29, 2022

Thank you @davideicardi & @metoule, a combination of them both has worked.

For reference, the working solution was:

string code = "o[\"Diesel % in oil\"].ToString().StartsWith(\"<\") ? o[\"Diesel % in oil\"].ToString().Replace(\"<\", String.Empty) : o[\"Diesel % in oil\"].ToString()";
var interpreter = new Interpreter();
interpreter.Reference(typeof(System.Collections.Generic.IDictionary<,>));
interpreter.SetVariable("o", o, typeof(IDictionary<string, object>));
return (T)interpreter.Eval(code);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants