Accessing .NET object properties in Jint

Dec 28, 2009 at 12:04 PM
Edited Dec 29, 2009 at 6:30 PM

I've been trying to access CLR object properties inside Jint, take this example program:

    class Program
    {
        class User
        {
            public string Name; 
        }

        static void Main(string[] args)
        {
            var user = new User { Name = "Fredrik Holmström" };
            var engine = new Jint.JintEngine();

            engine.SetDebugMode(true);
            engine.AllowClr = true;

            engine.SetParameter("user", user);
            engine.SetFunction("print", new Action<object>(Console.WriteLine));

            engine.Run(" print(user.Name); ");
        }
    }

But the only output I get in the console window is "Name", that is - the name of the property I'm accessing is printed, instead of the value of the property. I also tried to store the value of the property in a separate variable inside javascript, but no luck here either (prints the name of the property, instead of the value of it)

engine.Run(" var un = user.Name; print(un); ");

Dec 29, 2009 at 6:30 PM

Just wanted to response to my own thread and say that the fix was, obvious after using Jint a bit more, to use Properties instead of fields.

Dec 29, 2009 at 6:46 PM

I didn't see it at first. We didn't decide to handle field access yet, and I don't think it's a big lack for the moment.

So let's say the issue is closed ;)