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 = subj开发者_如何学Cect.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;
}
}
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.
精彩评论