Yes, this is a newbie question. I am with C# for only a few days so far,
but I want to convince my boss to migrate our main product to .NET. I
found out how to solve all the problems we might encounter while
migrating, except one...
As this post's title states, it is essencially for us to be able to
dynamically evaluate... well, let's say: "strings as code". I.e.
scripting. I found an example somewhere on the net that creates a very
simple JScript assembly that takes a string as an argument, eval()s it
and returns the result. I may
JSE.DoEval("1 + 1");
now, but I need to pass existing objects to the scripting engine so that
I'd be able to do stuff like
JSE.DoEval("Application.MainWindow.Close()");
I'm afraid the assembly I picked up isn't designed to do so. What's the
right way? My whole test app is designed in a way that an access via the
"Application" object offers access to all other important parts of the
app (like the MDI main window, it's children, their controls etc.). So,
my (hopefully not too stupid <g>) idea is to pass the Application object
to the JScript engine and then be able to access everything top-down.
But how to pass the App object so that it becomes "known" by the
scripting engine? What prerequisites do I have to take? What has to be
considered while designing the application itself? What assemblies do I
need? Etc. etc. ;-)
Please help a newbie! Any hints appreciated!
Regards from Germany,
Carsten
Carsten Posingies - 17 Jan 2004 17:07 GMT
Addendum:
I've found out some more that may clarify the problem to one of you (but
obfuscates me even more <g>)...
What I've done is to pass my "root" object to the script package's
constructor. I've added a simple public member to the root class. And
now I am able to access this member within the script. Well, I'm not
sure if this is understandable... Umh... <g>
Here is the JScript that makes the "scripting engine" assembly:
package JScript {
class MicroJSE {
private var Application;
public function Eval(expr : String) : String {
return this.Application.Version;
//return eval("this.Application.Version", "unsafe");
//return eval(expr);
}
public function MicroJSE(zApp) {
this.Application = zApp;
}
}
}
The class the zApp object is instanciated from is (in short):
namespace foo {
public class cApp {
public string Version = "alpha";
}
}
Carsten Posingies - 17 Jan 2004 17:17 GMT
[pardon, wrong button...]
Addendum:
I've found out some more that may clarify the problem to one of you (but
obfuscates me even more <g>)...
What I've done is to pass my "root" object to the script package's
constructor. I've added a simple public member to the root class. And
now I am able to access this member within the script. Well, I'm not
sure if this is understandable... Umh... <g>
Here is the JScript that makes the "scripting engine" assembly:
package JScript {
class MicroJSE {
private var Application;
public function Eval(expr : String) : String {
return this.Application.Version; //[1]
//return eval("this.Application.Version", "unsafe"); //[2]
//return eval(expr); //[3]
}
public function MicroJSE(zApp) {
this.Application = zApp;
}
}
}
The class the zApp object is instanciated from is (in short):
namespace foo {
public class cApp {
public string Version = "alpha";
}
}
Now I do:
using JScript;
using Microsoft.JScript;
(some more usings)
(some code)
protected cApp pApp;
(some code)
(assignment of the app object to the pApp pointer)
JScript.MicroJSE E = new JScript.MicroJSE(pApp);
string sResult;
try {
sResult = E.Eval("this.Application.Version");
}
catch(Microsoft.JScript.JScriptException jse) {
sResult = jse.Message;
}
The "real" version I've marked with [3] in the above snipplet. It
behaves like the debugging version [2], i.e.: sResult contains a message
from the cought exception that states: "object required". The thing is
now, that, if I'm running the version [1], everything goes fine!?!?!
So, the Application object is indeed passed to the JScript engine
assembly, and it is accessible -- but the eval() can't access it!? Can
anyone explain this behavior, or tell me where my thinking takes the
wrong curve?
Thanks in advance...
Carsten