我有一个名为EcmaEval的类,它允许我的应用程序的用户执行任意javascript。类的实现在这个问题的末尾。我允许用户访问一个“环境”对象,该对象提供了对他们编写脚本有用的方法和属性。
问题是我需要向JScript公开一个C#动态对象,但它不起作用。以前有没有人这样做过--它应该起作用吗?
因此,如果我有一个普通的旧对象,它有一个名为name的字符串属性,它可以工作:
test test = new test();
test.Name = "Daniel Bryars";
EcmaEval ecmaEval = new EcmaEval(new List<String>
{
Assembly.GetExecutingAssembly().Location
}, test);
String ecma = "environment.Name";
String result = ecmaEval.Eval<String>(ecma);
Assert.AreEqual("Daniel Bryars", result);但是如果我向EcmaEval对象传递一个动态对象,那么它就不会(属性名称为空):
dynamic expandoObject = new ExpandoObject();
expandoObject.Name = "Daniel Bryars";
EcmaEval ecmaEval = new EcmaEval(new List<String>
{
Assembly.GetExecutingAssembly().Location
}, expandoObject);
String ecma = "environment.Name";
String result = ecmaEval.Eval<String>(ecma);
Assert.AreEqual("Daniel Bryars", result);(结果为空。)
下面是EcmaEval的实现。还涉及到另一个名为JSObjectToDotNetConversion的类,它将JSObjects强制转换为C#对象(它使用反射来新建C#对象并设置字段和/或属性),但该类的实现并不相关。
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.JScript;
namespace Aeriandi.ApplicationBlocks.BusinessBaseObjects.Ecma
{
/// <summary>
/// Exposes the JScrip eval function as a .net method.
/// This uses the "safe" JScript.Eval so no disk, or network access is allowed.
/// </summary>
public class EcmaEval
{
private readonly object _evaluator;
private readonly Type _evaluatorType;
private readonly Object _environment;
public EcmaEval() : this (new List<string>(), null )
{
}
public EcmaEval(List<String> referencedAssemblies, Object environment)
{
if (null == referencedAssemblies)
{
throw new ArgumentNullException("referencedAssemblies", "The argument referencedAssemblies must not be null");
}
_environment = environment;
JScriptCodeProvider compiler = new JScriptCodeProvider();
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateInMemory = true;
foreach (String referencedAssembly in referencedAssemblies)
{
parameters.ReferencedAssemblies.Add(referencedAssembly);
}
string _jscriptSource =
@"package Evaluator
{
class Evaluator
{
public function Eval(expr : String, environment : Object)
{
return eval(expr);
}
}
}";
CompilerResults results = compiler.CompileAssemblyFromSource(parameters, _jscriptSource);
Assembly assembly = results.CompiledAssembly;
_evaluatorType = assembly.GetType("Evaluator.Evaluator");
_evaluator = Activator.CreateInstance(_evaluatorType);
}
public Object Eval(Type returnType, String ecmaScript)
{
ecmaScript = WrapInBrackets(ecmaScript);
Object result = _evaluatorType.InvokeMember(
"Eval",
BindingFlags.InvokeMethod,
null,
_evaluator,
new object[] { ecmaScript, _environment }
);
return JSObjectToDotNetConversion.Coerce(returnType, result);
}
public T Eval<T>(String ecmaScript)
{
return (T) Eval(typeof (T), ecmaScript);
}
private static String WrapInBrackets(String ecmaScript)
{
//You can't start a block of js with a { because it's ambiguous (according to the spec)
//so we wrap everything in brackets.
return String.Format("({0})", ecmaScript);
}
}
}发布于 2010-08-23 03:11:49
基本上,看起来我不能使用JScript.Net做到这一点。我想我需要使用一些构建在DLR之上的东西,比如:
IronJS
http://github.com/fholm/IronJS
或
托管JScript (不再由微软开发)
http://www.microsoft.com/downloads/details.aspx?familyid=A5189BCB-EF81-4C12-9733-E294D13A58E6&displaylang=en
这里有更多关于Managed JScript的信息,它将在这里消亡:http://pietschsoft.com/post/2009/06/12/Managed-JScript-on-the-DLR-from-Microsoft-is-DEAD-Why.aspx
https://stackoverflow.com/questions/3541459
复制相似问题