locked
Using of JScript by C# application RRS feed

  • Question

  • Hello!

    How can I use JScript in C#?

    The aim is to use some scripts for internal process in C# application. For example, get some methods and call them.

    Tuesday, January 10, 2012 2:06 PM

Answers

  • O'k. Thanks!

    I don't want to execute js-file, I would like to use methods from js-files, because these methods are very configurable and it's presumed that the methods will be used very frequently therefore frequent executions of js-files aren't suitable.

    It's interesting thing with "translation". How can I do it?

    I suppose to begin from simple method:

    function simpleMethod(someInt)
    {
      return ++someInt;
    }
    

     


    Update1: Or do you mean manual "translation"?

    As I've already told you, no, you cannot run javascript code in the context of a C# application.

    He means manually translating the code, not using some sort of program or tool.

    • Marked as answer by Bob Shen Friday, January 20, 2012 3:12 PM
    Wednesday, January 11, 2012 3:19 PM
  • You can't take a method from a non-Framework based file (as far as I know).

    And I ment manual translation.

    You can "translate" that function in this way:

    int simpleMethod(int someInt)
    {
        return ++someInt;
    }
    

    Remeber, JScript is a dynamic language (the type of variables is decided at run-time), while C# is a static language (the type of variables is decided at compile-time). In JScript, you know that simpleMethod will return int before running, but in C# you must indicate the type of everything.


    João Miguel
    • Marked as answer by Bob Shen Friday, January 20, 2012 3:12 PM
    Thursday, January 12, 2012 6:56 PM

All replies

  • You cannot execute javascript within the context of a C# app.  You could run a file (use Process.Start();) that happens to be a .js file, but it will run in its own context.
    Tuesday, January 10, 2012 4:08 PM
  • You could "translate" the methods into C# and that would be a good training (I'm saying this because I did it before, but now I forgot JScript).

    Well, maybe you can also post the code and we can help you. Or like servy42 said, you could run the file in a different process.

    In C# there are no macros or scripts: "what you see is what you get". Everything is safer. I prefer it this way.


    João Miguel
    • Edited by JMCF125 Tuesday, January 10, 2012 4:26 PM
    Tuesday, January 10, 2012 4:26 PM
  • O'k. Thanks!

    I don't want to execute js-file, I would like to use methods from js-files, because these methods are very configurable and it's presumed that the methods will be used very frequently therefore frequent executions of js-files aren't suitable.

    It's interesting thing with "translation". How can I do it?

    I suppose to begin from simple method:

    function simpleMethod(someInt)
    {
      return ++someInt;
    }
    

     


    Update1: Or do you mean manual "translation"?
    • Edited by Andrej Bra Wednesday, January 11, 2012 7:26 AM Update1
    Wednesday, January 11, 2012 6:10 AM
  • O'k. Thanks!

    I don't want to execute js-file, I would like to use methods from js-files, because these methods are very configurable and it's presumed that the methods will be used very frequently therefore frequent executions of js-files aren't suitable.

    It's interesting thing with "translation". How can I do it?

    I suppose to begin from simple method:

    function simpleMethod(someInt)
    {
      return ++someInt;
    }
    

     


    Update1: Or do you mean manual "translation"?

    As I've already told you, no, you cannot run javascript code in the context of a C# application.

    He means manually translating the code, not using some sort of program or tool.

    • Marked as answer by Bob Shen Friday, January 20, 2012 3:12 PM
    Wednesday, January 11, 2012 3:19 PM
  • You can't take a method from a non-Framework based file (as far as I know).

    And I ment manual translation.

    You can "translate" that function in this way:

    int simpleMethod(int someInt)
    {
        return ++someInt;
    }
    

    Remeber, JScript is a dynamic language (the type of variables is decided at run-time), while C# is a static language (the type of variables is decided at compile-time). In JScript, you know that simpleMethod will return int before running, but in C# you must indicate the type of everything.


    João Miguel
    • Marked as answer by Bob Shen Friday, January 20, 2012 3:12 PM
    Thursday, January 12, 2012 6:56 PM
  • Try to translate the code with DIY(do it yourself).

    You will learn more about C#, and if you have any doughts in the manual translation paste the code in this forum, and we're going to help you.


    João Miguel
    Thursday, January 12, 2012 6:58 PM
  • So, how is it going on?

    Any problems in the "translation"? If there's any, we'll try to solve it.


    João Miguel
    Friday, January 13, 2012 5:19 PM