Skip to content

.net dynamic objects #4

Closed
Closed

Description

@bradodarb

I just found your fantastic library today. It seems to perform well and was easy to get setup.

In my application, I am passing some global variables to the script machine that are dynamic objects in.net.

It seems like they are coming in as null.

Am I correct in assuming there is no support for .net dynamic objects at this time?

Best,
Brad Murry

Activity

  1. jingwood commented on Jun 14, 2013

    @jingwood
    Member

    Hi Brad,

    Thank you for reporting this issue. I think dynamic type should also be supported by current ReoScript. But when I did the test about your issue I found another bug in ReoScript's DirectAccess mode. If you using DirectAccess to access a field of type (whether it is dynamic or not) from your script, you may always get a null value.

    This bug is that current ReoScript always capitalizes the first letter of field's name, so there is no field will be found. For example:

    in .NET:

    class T {
        public int a = 10;
    }
    srm["obj"] = new T();
    

    in script:

    console.log(obj.a);       // get null since ReoScript tries to find the field called 'A'.
    

    This bug will be fixed in next release.

    Please try to use a .NET type in script by the following ways:

    1. If your type inherits from ObjectValue, put property into ObjectValue like:

      in .NET:

      class T : ObjectValue { }
      dynamic obj = new T();
      obj["a"] = 10;
      
      srm["obj"] = obj;
      

      in script:

      console.log(obj.a);    // 10
      
    2. If your type does not inherit from ObjectValue, you need use DirectAccess mode.

      in .NET:

      class T { 
          public int A { get; set; }
      }
      
      dynamic obj = new T();
      obj.A = 10;
      
      srm.WorkMode |= MachineWorkMode.AllowDirectAccess;          // enable DirectAccess
      srm["obj"] = obj;
      

      in script:

      console.log(obj.a);    // 10
      

    Hope it is useful to you!
    Jing Lu

  2. bradodarb commented on Jun 14, 2013

    @bradodarb
    Author

    Thank you for the prompt reply, Jing!

    I am using code similar to your 2nd example.

    The problem is using a property added to the dynamic object:

    This works accessing A from a script

    dynamic obj = new T();
    obj.A = 10;

    This does not work

    public class Obj
    {
      public dynamic Property {get;set;}
    
    }
    var obj = new Obj();
    obj.Property  = new ExpandoObject();
    obj.Property.NewDynamicStuff = "red alert";

    When I debug the script execution,

    obj.Property

    comes back as

    system.dynamic.ExpandoObject

    (as does obj.property).

    *however

     obj.Property.NewDynamicStuff 

    ,

    obj.Property.newDynamicStuff 

    and

     obj.property.newDynamicStuff 

    All return null.

    I already have c#, python and ruby scripting capabilities built into my application and I would love to get this working with reoscript!

  3. jingwood commented on Jun 15, 2013

    @jingwood
    Member

    Brad you are right, ExpandoObject class is not be supported by current ReoScript. I found that ExpandoObject implements the IDictionary<string, object> interface so I have an idea to resolve this issue. Let me fix this in next version!

  4. bradodarb commented on Jun 15, 2013

    @bradodarb
    Author

    Jing, my apologies for not mentioning the ExpandoObject explicitly in my original post.

    You are correct, it does implement IDictionary<string, object>.

                dynamic model = new System.Dynamic.ExpandoObject();
    
                model.red = "alert";
                model.white = "knight";
                model.blue = "sky";
    
                var hash = model as IDictionary<String, object>;
    
                foreach (var chunk in hash)
                {
                    Console.WriteLine("Key = {0} | Value = {1}", chunk.Key, chunk.Value);
                }

    I am really looking forward to the next version!

  5. added a commit that references this issue on Jun 17, 2013
  6. jingwood commented on Jun 17, 2013

    @jingwood
    Member

    Brad you're welcome! I just released the version 1.3 that is supported to access properties from IDictionary<string, object> interface, and the enumeration by using for...in syntax is also supported. I've test it in VisualStudio 2012 and .NET Framwork 4.5.

    If you have other problems, please let me know. Thanks for a good suggestion again!

  7. bradodarb commented on Jun 26, 2013

    @bradodarb
    Author

    Verified the new release does indeed fix the ExpandoObject issue.

    Thanks again Mr. Lu!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions