IDynamicMetaObjectProvider动态对象和JScript.Net

时间:2010-08-22 12:23:42

标签: dynamic c#-4.0 javascript

我有一个名为EcmaEval的类,它允许我的应用程序的用户执行任意javascript。该课程的实施是在这个问题的最后。我允许用户访问“环境”对象,该对象提供了对脚本编写有用的方法和属性。

问题是我需要将C#动态对象暴露给JScript,但它不起作用。有没有人以前这样做过 - 它应该有效吗?

所以,如果我有一个带有名为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对象传递给动态对象,那么它就不会(属性Name为null):

        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的类,它将JSObject强制转换为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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

基本上看起来我不能使用JScript.Net来做到这一点。我想我需要使用DLR上构建的东西,如:

IronJS

http://github.com/fholm/IronJS

托管JScript(不再由MS开发。)

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