Dynamic objects

Nov 23, 2010 at 8:09 PM

Is there any plans to support Dynamic objects getting passed into the engine?

 

Example:

static void Main(string[] args)        {            
dynamic obj = new ExpandoObject();
        JavascriptContext js = new JavascriptContext();
        js.SetParameter("document", obj);            
js.Run("document.DynamicProperty = '3';");
obj.DynamicProperty = "helloWorld";            
Console.WriteLine(obj.DynamicProperty);                        
js.Run("var x = document.DynamicProperty;");            
string x = js.GetParameter("x").ToString();            // x = 3 instead of helloWorld because obj get's cast into a generic object not a Dynamic one             

Console.WriteLine(x);        }

 

 

 

Output:

helloWorld
3

Nov 30, 2010 at 11:00 PM

Hi,

I see the only way of doing this is by using Dictionary<string, object> like this:



        Dictionary<string, object> contact = new Dictionary<string, object>();
        contact.Add("Surname", "Fred");
        contact.Add("Forename", "Smith");
        context.SetParameter("contact", contact);

The from within JavaScript you can use it as an object:

console.print(contact.Forename);
console.print(contact.Surname);

But I found a couple of concerns (I don't know if by design):

1) That if I specify contact.Age = 38 in JavaScript, the .NET Dictionary doesn't have the new item. I get an exception even with contact.Add('Age', 38);

2) Or event contact.Forename = 'Joe' doesn't change the existing value.

Nov 30, 2010 at 11:29 PM
Edited Nov 30, 2010 at 11:30 PM

I was looking to implement a document object for DOM interaction from javascript like the ExpandoObject in this link http://www.codeproject.com/KB/cs/dynamicincsharp.aspx.  The current issue is I can create a "document" object that contains the html elements within, but in javascript you can call "document.formName" to reference a form from within the document.  My purpose would be to create logic inside the TryGetMember function of the DynamicObject class to return a form object based off business logic.

 

also, javascript might call document.write code that inserts new forms that don't exist before run time.  

May 4, 2011 at 12:44 PM

Hi,

Did anyone attempt to implement the Dynamic approach with Javascript DotNet?

I'm working on it, and it would be great if anyone was interested in collaborating on this, e.i. via a new GitHub project that extends JavscriptDotNet.

Developer
Jun 20, 2011 at 5:14 AM
Edited Jun 20, 2011 at 5:20 AM

Here what I was able to do with Dynamic and Javascript DotNet.
I will be blogging about it on my blog this week: http://frederictorres.blogspot.com/ 

            

            dynamic csContext   = new DynamicJavascriptContext(new JavascriptContext());

            csContext.console   = new SystemConsole();
            csContext.message   = "Hello World !";
            csContext.number    = 1;
            csContext.instance  = new Dictionary<string, object>() { { "b" , 2 } };
            csContext.instance2 = DynamicSugar.DS.Dictionary( new { a = 1, b = 2 } );
           
            string script = @"
                number           = 123;
                instance['a']    = 1;
                instance['Date'] = new Date();                
                instance2['a']   = 123;
            ";
            
            csContext.Run(script);
            
            Console.WriteLine("number: "        + csContext.number);
            Console.WriteLine("instance: "      + csContext.instance);
            Console.WriteLine("instance.a: "    + csContext.instance.a);
            Console.WriteLine("instance.b: "    + csContext.instance.b);
            Console.WriteLine("instance.Date: " + csContext.instance.Date);
            Console.WriteLine("instance2.a: "   + csContext.instance2.a);


Developer
Jun 20, 2011 at 5:20 AM
Edited Jun 20, 2011 at 5:21 AM

.