Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: Passing javascript objects to c# native code

$
0
0
I found a solution. The V8ScriptItem is a DynamicObject - so this works:
    public class Konsoll
    {
        private void logg(int indent, dynamic o)
        {
            foreach (string egenskap in o.GetDynamicMemberNames())
            {
                Console.Write(new String(' ', indent));
                Console.Write(egenskap);
                Console.Write(':');
                
                if (o[egenskap] is System.Dynamic.DynamicObject)
                {
                    Console.WriteLine();
                    logg(indent + 1, o[egenskap]);
                }
                else
                {
                    Console.WriteLine(o[egenskap].ToString());
                }
            }
        }
        public void log(dynamic o)
        {
            logg(0, o);
        }

    }
so
engine.Execute("console.log({v1:4,v2:['a','bc', 'de', {f:'g'}],h:1,i:2,j:3,k:4});");
will output
v1:4
v2:
 0:a
 1:bc
 2:de
 3:
  f:g
h:1
i:2
j:3
k:4
Clearscript is really cool stuff.

Viewing all articles
Browse latest Browse all 2297

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>