Trapping invalid property on .NET object

Jan 21, 2010 at 2:15 PM

I'm passing a .NET object into Jint, and trying to call a property that does not exist. I want to be able to trap this condition and redirect the call to return the proper value.* However, I'm getting back the name of the property returned to the caller (sample follows). What am I missing here?

--Jim

*Long-term, I'm trying to implement something like the HTML DOM, where it's valid syntax to do "document.item1" where item1 is an element on the page. I don't think I can really build this sort of dynamic typing into a .NET class unless I have some way to catch when the interpreter tries to evaluate an invalid property.

    public class TestObject
    {
        Dictionary<string, string> internalMap = new Dictionary<string, string>();

        public TestObject()
        {
        }

        public void AddItem(string key, string value)
        {
            internalMap.Add(key, value);
        }

        public string FindItem(string name)
        {
            string itemValue = null;
            if (internalMap.ContainsKey(name))
            {
                itemValue = internalMap[name];
            }
            return itemValue;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            JintEngine engine = new JintEngine();
            TestObject test = new TestObject();
            engine.SetParameter("tester", test);
            engine.Run("tester.AddItem('item1', 'Hello');");
            object x = engine.Run("return tester.item1;");
        }
    }