Clearscript: Expose a Dictionary as an object

Created on 21 Mar 2021  路  2Comments  路  Source: microsoft/ClearScript

Hey guys!
This code gives me the error: "ArgumentException: Invalid property assignment"
What happens??

`using (var engine = new V8ScriptEngine())
{
engine.AddHostObject("texto", txtFormula);

Dictionary<string, string> parametros = new Dictionary<string, string>();

parametros.Add("cantidad", "1");
parametros.Add("anchoLona", "1.60");

engine.AddHostObject("param", parametros);

engine.Execute("texto.Text = param['cantidad'];");
}`
question

All 2 comments

Hello @sebastianbrunobautes,

The JavaScript expression param['cantidad'] is equivalent to param.cantidad, and since .NET dictionaries have no property named "cantidad", the expression evaluates to undefined, which apparently isn't a suitable value for texto.Text.

Instead of a dictionary, consider using something like ClearScript's PropertyBag. This class behaves like a dictionary on the .NET side, but projects its contents as properties for script code.

Good luck!

Thanks for the reply, I'll give it a try.

Was this page helpful?
0 / 5 - 0 ratings