Unable to access .NET "class within class" from Jint

Feb 5, 2010 at 9:02 AM

Hi all,

And sorry for the bad wording on the subject. I'm looking into different scripting technologies for my .NET system, and is currently examining Jint. I need to be able to pass objects back/forth from C#/Jint. One issue that I'm currently struggling with is accessing methods in class which is defined within class. Here is an example to illustrate :

using System;
using System.Collections.Generic;
using System.Text;
using Jint;
namespace test_jint
{
     public class Inner
     {
         public int integ { get { return 2; } }
     }   
     public class Test
     {
        public Inner _inner = new Inner();
        public void Execute()
        {
            Console.WriteLine("test.exec "+_inner.integ);
        }

        public Inner getinner { get { return _inner; } }
    }
    public class Program
    {
        static void Main(string[] args)
        {
            Test t = new Test();
            Console.WriteLine(">{0}", t._inner.integ);

            JintEngine engine = new JintEngine();

             engine = engine.SetParameter("TestIt", t);
             var result = engine.Run(@"TestIt.Execute();");
             result = engine.Run(@"return TestIt.getinner.integ();");

             //This line fails, as Jint assumes _inner must be a method.
             result = engine.Run(@"return TestIt._inner.integ();");
             System.Console.WriteLine(">>{0}", result);

        }
    }
}
The line at which I'm trying to access the integ method from the _inner object fails, and tracing into Jint shows that Jint believes 
this to be a method where it probably should have recursed over the object to get the method. As a workaround, using a property to get the class object works,
but it would have been better to access the class-object itself. Ideas or solutions anyone, or will I have to use properties for this ?
Regards,
Kjetil
Coordinator
Feb 5, 2010 at 9:45 AM
Hi,

Indeed, in Jint, we have not dealt with Fields in classes. This is intended only because it does not respect programming best practices : a field shoud not be public.

2010/2/5 knaess <notifications@codeplex.com>

From: knaess

Hi all,

And sorry for the bad wording on the subject. I'm looking into different scripting technologies for my .NET system, and is currently examining Jint. I need to be able to pass objects back/forth from C#/Jint. One issue that I'm currently struggling with is accessing methods in class which is defined within class. Here is an example to illustrate :

using System;
using System.Collections.Generic;
using System.Text;
using Jint;
namespace test_jint
{
     public class Inner
     {
         public int integ { get { return 2; } }
     }   
     public class Test
     {
        public Inner _inner = new Inner();
        public void Execute()
        {
            Console.WriteLine("test.exec "+_inner.integ);
        }

        public Inner getinner { get { return _inner; } }
    }
    public class Program
    {
        static void Main(string[] args)
        {
            Test t = new Test();
            Console.WriteLine(">{0}", t._inner.integ);

            JintEngine engine = new JintEngine();

             engine = engine.SetParameter("TestIt", t);
             var result = engine.Run(@"TestIt.Execute();");
             result = engine.Run(@"return TestIt.getinner.integ();");

             //This line fails, as Jint assumes _inner must be a method.
             result = engine.Run(@"return TestIt._inner.integ();");
             System.Console.WriteLine(">>{0}", result);

        }
    }
}
The line at which I'm trying to access the integ method from the _inner object fails, and tracing into Jint shows that Jint believes 
this to be a method where it probably should have recursed over the object to get the method. As a workaround, using a property to get the class object works,
but it would have been better to access the class-object itself. Ideas or solutions anyone, or will I have to use properties for this ?
Regards,
Kjetil

Read the full discussion online.

To add a post to this discussion, reply to this email (jint@discussions.codeplex.com)

To start a new discussion for this project, email jint@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com




--
Cordialement,
Nicolas Penin