1

I have this Js functions :

function Add (a, b)
{  return a+b;}

function Substract (a, b)
{  return a-b;}

I know (heard) that I can activate those functions on c# code using the dynamic keyword.

Can I get a help ( or beginning of help) to the solution by simple sample ?

edit

If I have a webBrowser ( winform) - which can help me. ( sorry to add this now).

9
  • 1
    What do you mean by activating? You need a javascript interpreter if you want to execute javascript code. The C#s dynamic keyword is pretty useless in this aspect. Commented Dec 25, 2011 at 14:08
  • C# can't call javascript directly (assuming your C# is server side, not in silverlight and even then I don't think you can). Commented Dec 25, 2011 at 14:08
  • @oded edited. thanks. and sorry. Commented Dec 25, 2011 at 14:11
  • @oded I saw this , but didnt understand completely what he has done there blogs.microsoft.co.il/blogs/shlomo/archive/2009/06/19/… Commented Dec 25, 2011 at 14:13
  • He is using the WebBrowser instance to return a javascript Calculator object and uses dynamic to call it or a winforms implementation interchangeably. Actually rather cool. Commented Dec 25, 2011 at 14:22

1 Answer 1

2

You need a javascript interpreter if you want to execute javascript code. The C#s dynamic keyword is pretty useless in this aspect. You may take a look at Javascript .NET or Jint if you are trying to execute those functions in .NET code.

Here's an example with Jint:

using System;
using Jint;

class Program
{
    static void Main()
    {
        var script = @"
function Add(a, b) {  
    return a + b;
}

function Substract(a, b) {  
    return a - b;
}

return Add(a, b);
";
        var result = new JintEngine()
            .SetParameter("a", 3)
            .SetParameter("b", 5)
            .Run(script);

        Console.WriteLine("result: {0}", result);
    }
}

Remark: what you have is not valid javascript. The var keyword cannot be used before function parameters. So you probably should start by fixing your code.

Sign up to request clarification or add additional context in comments.

7 Comments

Does this libarary can help me to activate "add" in c# ?
fixed ( that is what heppens when you're in hurry :) )
@RoyiNamir, yes, the library can help you call the Add javascript function. But first you will have to fix it as what you have shown is not valid javascript as I stated in my answer.
youre the man! thanks ! ( although I wanted to use the Dynamic keyword...) but thanks a lot.
@RoyiNamir, the dynamic keyword cannot parse javascript. It's purpose is far from that.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.