4

I want to use DynamicObject class under Jint and I have built a sample to do it. First assert is correctly passes but fails at second assert.

Is there any way to do it or do you know any other javascript engine that makes it possible ?

public void Jtest()
{
    Jint.JintEngine engine = new JintEngine();

    dynamic subject = new MyDynamicObject();

    dynamic x = subject.myProp.otherProp;

    Assert.AreEqual(subject, x);

    engine.SetParameter("myClass", subject);

    object result = engine.Run(@"return myClass.myProp.otherProp;");

    // result is null here
    Assert.AreEqual(subject, result);
}

public class MyDynamicObject : System.Dynamic.DynamicObject
{
    public override bool TryGetMember(System.Dynamic.GetMemberBinder binder, out object result)
    {
        result = this;
        return true;
    }
}
ertan
  • 645
  • 1
  • 7
  • 14
  • Why do you expect the result of `@"return myClass.myProp.otherProp;"` to be the string `"otherProp"`? (And not the object `subject`?) – dtb Sep 13 '10 at 17:43
  • So, what does `result` actually contain after the call to `engine.Run`? – dtb Sep 13 '10 at 18:08
  • Does it work if you do, for example, `engine.SetParameter("myClass", 42);` and `engine.Run(@"return myClass;");`? – dtb Sep 13 '10 at 19:23
  • That's is an interesting question. You should ask it on the jint forum directly, you would have more help from the developpers : http://jint.codeplex.com/discussions – Nicolas Penin Sep 14 '10 at 05:40
  • @dtb; yes it's perfectly working with primitive types and standard classes – ertan Sep 14 '10 at 15:11

1 Answers1

0

I think the answer is in jint code. To find properties, it is based on reflection. I don't think reflection handles dynamic objects. Maybe the code should be revamped to use lambda expression. But in that case, it would not work on 2.0 anymore.

Firstly, you should try to override the method GetDynamicMemberNames. Maybe this would help reflection to find the properties, thus Jint.

Nicolas Penin
  • 371
  • 1
  • 19