How to pass a class instance to jint script?

Feb 7, 2010 at 6:28 PM
Edited Feb 8, 2010 at 3:29 AM

I want to pass a class instance to jint script like this:

 

class A
{
    public int B;
    public string C;
}
var a = new A(); 
var script = "a.B=18;a.C='hello';"; 
var engine = new JintEngine(); 
engine.SetParameter("a",a); 
engine.Run(script);
Console.WriteLine("a.B={0};a.C='{1}'",a.B,a.C);

 

 

Coordinator
Feb 7, 2010 at 8:55 PM
Hi,

It should work. Do you have any exception when trying to execute it ?

Le 7 févr. 2010 à 20:28, "niijyeni" <notifications@codeplex.com> a écrit :

From: niijyeni

I want to pass a class instance to jint script like this: class A{public int B;public string C;} var a = new A(); var script = "a.B=18;a.C='hello';"; var engine = new JintEngine(); engine.SetParameter("a",a); engine.Run(script); Console.WriteLine("a.B={0};a.C='{1}'",a.B,a.C);

Feb 8, 2010 at 3:39 AM

Yes, I got this message when execute it:

 

Object reference not set to an instance of an object.
Line: 1 Char: 0
No source code available.

Object reference not set to an instance of an object.

Line: 1 Char: 0

No source code available.

Coordinator
Feb 8, 2010 at 5:05 AM
Hi,

I've just found why it is not working : we do actually allow access only to properties. If you want to access a field on an instance, you have to make an accessor for it.

Le 8 févr. 2010 à 05:39, "niijyeni" <notifications@codeplex.com> a écrit :

From: niijyeni

Yes, I got this message when execute it:

 

Object reference not set to an instance of an object.
Line: 1 Char: 0
No source code available.

Object reference not set to an instance of an object.

Line: 1 Char: 0

No source code available.

Feb 8, 2010 at 1:18 PM

Thanks, I got it. but i have a new problem. I'm try like this

        {
            public int B { get; set; }
            public string C { get; set; }
            public A()
            {
                this.C = string.Empty;
                this.B = 0;
            }
        }

 

class A
{
    public int B { get; set; }
    public string C { get; set; }

    public A()
    {
        this.C = string.Empty;
        this.B = 0;
    }
}

var a = new A();
var script = "a.B=18;a.C='hello';";
var engine = new JintEngine();
engine.SetParameter("a", a);
engine.Run(script);
Console.WriteLine("a.B={0};a.C='{1}'", a.B, a.C);


Object of type 'System.Double' cannot be converted to type 'System.Int32'.

Line: 1 Char: 0

No source code available.

Why Jint can't auto convert 'System.Double' to 'System.Int32'. And in source code I means to asign 18(System.Int32) to a.B not 18.0(System.Double). so if Jint can auto identify the data type then we can fix the problem.

:)

 

 

 

Coordinator
Feb 8, 2010 at 1:24 PM

I have added a unit test and it's working with the current version under SVN.

Feb 8, 2010 at 1:32 PM
sebastienros wrote:

I have added a unit test and it's working with the current version under SVN.

sorry, i didn't have account to access SVN