Groups | Blog | Home
all groups > dotnet jscript > november 2004 >

dotnet jscript : Adding JScript Eval to an ASPX Parser


Kolo San
11/27/2004 8:46:19 PM
Hi,

I am trying to add a JScript Evaluation to a Page Template parser in C# for
a CMS port from ASP that I am doing. VBscript (for ASP) has an Eval
function which we have used to allow some measure of scripting within page
templates (allowing some of our other developers to extend our pages without
playing with the base scripts).

The Evaluation works in C# for actual JScript.Net classes (like Math, etc)
but I am having problems loading (or referencing) Net Managed assemblies
(like System, or classes which we have created) into the script engine.

The somewhat prototypical code I am using for this is (in a Parse.dll
assembly which the aspx page 'uses'):

<code>

Microsoft.JScript.Vsa.VsaEngine scriptEngine =
Microsoft.JScript.Vsa.VsaEngine.CreateEngine();
try{
// Ok, now evaluate the expression using the script file where necessary.
result = Microsoft.JScript.Eval.JScriptEvaluate(fullscriptfile,
scriptEngine).ToString();
}catch(Exception ex){
result = ex.ToString();
}
return result;

</code>

where the script file is loaded into the string 'fullscriptfile'.

Is there any way I can add C# assemblies into the scriptEngine before
Evaluating.

I tried the Microsoft.JScript.Import(name, Engine) method but I cant find
any information on what string it requires for the 'name' parameter...is it
the assembly name, path to the dll or something else (I have tried both
these options without success)? Could it be a security issue?

Hoping someone can help as there is not a lot on the Net or MSDN about this.

Mark

bruce barker
11/29/2004 9:12:27 AM
the vsa engine has an Items (IVsaItems) collections, to which you can add
references.

-- bruce (sqlwork.com)


[quoted text, click to view]
| Hi,
|
| I am trying to add a JScript Evaluation to a Page Template parser in C#
for
| a CMS port from ASP that I am doing. VBscript (for ASP) has an Eval
| function which we have used to allow some measure of scripting within page
| templates (allowing some of our other developers to extend our pages
without
| playing with the base scripts).
|
| The Evaluation works in C# for actual JScript.Net classes (like Math, etc)
| but I am having problems loading (or referencing) Net Managed assemblies
| (like System, or classes which we have created) into the script engine.
|
| The somewhat prototypical code I am using for this is (in a Parse.dll
| assembly which the aspx page 'uses'):
|
| <code>
|
| Microsoft.JScript.Vsa.VsaEngine scriptEngine =
| Microsoft.JScript.Vsa.VsaEngine.CreateEngine();
| try{
| // Ok, now evaluate the expression using the script file where necessary.
| result = Microsoft.JScript.Eval.JScriptEvaluate(fullscriptfile,
| scriptEngine).ToString();
| }catch(Exception ex){
| result = ex.ToString();
| }
| return result;
|
| </code>
|
| where the script file is loaded into the string 'fullscriptfile'.
|
| Is there any way I can add C# assemblies into the scriptEngine before
| Evaluating.
|
| I tried the Microsoft.JScript.Import(name, Engine) method but I cant find
| any information on what string it requires for the 'name' parameter...is
it
| the assembly name, path to the dll or something else (I have tried both
| these options without success)? Could it be a security issue?
|
| Hoping someone can help as there is not a lot on the Net or MSDN about
this.
|
| Mark
|
|

Simone Massaro
12/2/2004 11:19:14 AM
This is what I have been doing:

string[] commonNamespaces =

{

"System",

"System.Collections",

"System.ComponentModel",

"System.Data",

"System.Drawing",

"System.Windows.Forms",

"System.Xml",

"Ico.Gwx"

//"System.Drawing.Drawing2D",

//"System.Drawing.Imaging",

//"System.Globalization",

//"System.IO",

//"System.Reflection",

//"System.Text",

//"System.Text.RegularExpressions",

//"System.Threading"

};

string[] commonAssemblies =

{

"mscorlib",

"System",

"System.Data",

"System.Drawing",

"System.Windows.Forms",

"System.Xml",

"GwxRuntimeCore",

"GwxRuntimeCoreCommands",

"GwxRuntimeViewControl",

"GwxConfigCore"

};

private Microsoft.JScript.GlobalScope jscriptGlobalScope = null;

private void UninitializeJScriptDotNetEngine()

{

if (jscriptGlobalScope != null)

jscriptGlobalScope.engine.Close();

jscriptGlobalScope = null;

}

private void InitializeJScriptDotNetEngine()

{

if (jscriptGlobalScope == null)

{

try

{

jscriptGlobalScope =
Microsoft.JScript.Vsa.VsaEngine.CreateEngineAndGetGlobalScope(false,
commonAssemblies);

}

catch {}

foreach (string ns in commonNamespaces)

{

try

{

if ((ns != null) && (ns != String.Empty))

Microsoft.JScript.Import.JScriptImport(ns, jscriptGlobalScope.engine);

}

catch {}

}

}

}




After initializing the engine you can call

Object retVal=Microsoft.JScript.Eval.JScriptEvaluate(jscriptCode, "unsafe",
jscriptGlobalScope.engine);

it will evaluate your code using the assembly and namespaces that you
declared



[quoted text, click to view]

Kolo San
12/8/2004 4:46:53 PM
Thanks Simone...I will try this out.

Mark


[quoted text, click to view]

AddThis Social Bookmark Button