ClearScript: Stop converting methods to JavaScript properties

559 views Asked by At
_Engine.Script.test( new { test = 123, cat = "lolcat" } );

This outputs the following to JavaScript:

{"Equals":{},"GetHashCode":{},"ToString":{},"GetType":{},"test":123,"cat":"lolcat"}

As you can see also the methods are being converted to Json properties. Is it possible (with as little boilerplate syntax as possible) to only send the properties? Same goes for Expando-like objects.

Below here works, and I can replace a bit of boilerplate with some extension methods, but it would be nice if I can completely get rid of all boilerplate.

var js = _Engine.Evaluate( "eval(" + JSONSerializer.Serialize(new { test = 123, cat = "lolcat" }) + ")" );

_Engine.Script.test( js );
1

There are 1 answers

4
BitCortex On

How about using ClearScript's PropertyBag? For example,

_Engine.Script.test( new PropertyBag { { "test", 123 }, { "cat", "lolcat" } } );

Or, if you're using C# 6 or later,

_Engine.Script.test( new PropertyBag { ["test"] = 123, ["cat"] = "lolcat" } );