How to execute this kind of script?

41 views
Skip to first unread message

its Win32nipuh

unread,
May 17, 2012, 1:31:39 PM5/17/12
to CS-Script
Hey Oleg,
Could you please explain me why this script gives an error? and is it
possible to pass array to script function?

1. script

using System;
using System.Windows.Forms;
public class Script
{
static public void Main(string[] args)
{
// for( int i = 0; i<args.Length; i++)
// {
// //MessageBox.Show(args[i]);//args[i]
// }
}
}

2. How I execute it in code:

var Process = CSScript.LoadMethod(textBox1.Text)
.GetStaticMethod();

string[] args = new string[] { "Hey", "World" };

for( int i = 0; i<args.Length; i++)
{
MessageBox.Show(args[i]); // I see all parameters
here
}
Process(args); // Exception: Unable to cast
System.String to System.String[]

3. If I replace in the script

static public void Main(string[] args)

with

static public void Main(string args)
and pass simple string to
Process(str);

it works fine.

Thanx!

Regards,
Oleg.

Oleg Shilo

unread,
May 17, 2012, 7:27:04 PM5/17/12
to cs-s...@googlegroups.com
Because GetStaticMethod returns dynamic method it will always have some limitations with respect to the signature of the emitted method. In your case the FastInvoker builder treats the compile time array argument as a two separate runtime string arguments. The solution is simple. Pass the input parameters as an Object nut not an array:

instead "string[] args = new string[] { "Hey", "World" };"
use "object args = new string[] { "Hey", "World" };"  

This will work. But keep in mind that GetStaticMetghod (based on the FastInvoker) in some cases may not be the best choice of the script invocation model. Also consider using interfaces. 

Cheers,
Oleg Shilo
Reply all
Reply to author
Forward
0 new messages