Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1028881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:27:31+00:00 2026-05-16T12:27:31+00:00

I’ve got a class called EcmaEval which allows users of my application execute arbitary

  • 0

I’ve got a class called EcmaEval which allows users of my application execute arbitary javascript. The implementation of the class is at the end of this question. I allow users to access an “environment” object which provides methods and properties which are useful for them to script with.

The problem is that I need to expose a C# dynamic object to JScript, but it doesn’t work. Has anyone done this before – should it work?

So, if I’ve got a plain old object with a string property called name it works:

        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);

BUT if I pass my EcmaEval object a dynamic object then it doesn’t (the property Name is 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);

(result is null.)

Here’s the implementation of EcmaEval. There’s another class involved called JSObjectToDotNetConversion which coerces JSObjects to C# Objects (it uses reflection to new up a C# object and set the fields and/or properties) but the implementation of that class isn’t relevent.

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T12:27:32+00:00Added an answer on May 16, 2026 at 12:27 pm

    Bascially it looks like I can’t do this using JScript.Net. I think I need to use something built ontop of the DLR like:

    IronJS

    http://github.com/fholm/IronJS

    or

    Managed JScript (which is no longer being developed by MS.)

    http://www.microsoft.com/downloads/details.aspx?familyid=A5189BCB-EF81-4C12-9733-E294D13A58E6&displaylang=en

    There’s some more information about Managed JScript and it’s demise here: http://pietschsoft.com/post/2009/06/12/Managed-JScript-on-the-DLR-from-Microsoft-is-DEAD-Why.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.