1
_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 );
Dirk Boer
  • 8,522
  • 13
  • 63
  • 111

1 Answers1

0

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" } );
BitCortex
  • 3,328
  • 1
  • 15
  • 19
  • The problem is that my data is coming from other ExpandoObjects from different parts of the code, where they are being used for other means - this means I still need to write a converter for these objects. – Dirk Boer Aug 29 '17 at 14:25
  • How are you examining the object in JavaScript? Since you're seeing the methods as properties, I'm guessing that you're using [`for..in`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in)? – BitCortex Aug 29 '17 at 14:48
  • I'm using JSON.stringify(obj) – Dirk Boer Aug 29 '17 at 15:01
  • 2
    Hmm, `JSON.stringify` doesn't seem to work with .NET objects at all (at least in the latest ClearScript), so yes, you'll probably have to create native JavaScript objects. – BitCortex Aug 29 '17 at 15:31