Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Execute User written Code

Execute User written Code

Scheduled Pinned Locked Moved C#
csharpvisual-studiotoolsperformancequestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Ketty Avashia
    wrote on last edited by
    #1

    I need to write a performance driven utility that allows user to write C# code in a text area. When they click on execute to see what is the final out, utility should complie and run the code written by user as if it is a function body. e.g. I am trying to achieve: private void ExecuteUser()//Function in Utility { /* -------Body of this function will be filled by User Code from text area at click of button. */ } I am not a C# coder for past few years:((, so any code snippet would be awesome. We are using .Net 2.0. VS 2005.

    Ketty

    G P C 3 Replies Last reply
    0
    • K Ketty Avashia

      I need to write a performance driven utility that allows user to write C# code in a text area. When they click on execute to see what is the final out, utility should complie and run the code written by user as if it is a function body. e.g. I am trying to achieve: private void ExecuteUser()//Function in Utility { /* -------Body of this function will be filled by User Code from text area at click of button. */ } I am not a C# coder for past few years:((, so any code snippet would be awesome. We are using .Net 2.0. VS 2005.

      Ketty

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      You can't put code into an existing method. You have to compile the code into a new assembly. Take a look at the Microsoft.CSharp.CSharpCodeProvider class.

      --- single minded; short sighted; long gone;

      K 1 Reply Last reply
      0
      • G Guffa

        You can't put code into an existing method. You have to compile the code into a new assembly. Take a look at the Microsoft.CSharp.CSharpCodeProvider class.

        --- single minded; short sighted; long gone;

        K Offline
        K Offline
        Ketty Avashia
        wrote on last edited by
        #3

        Thanks, I did see an example of "Microsoft.CSharp.CSharpCodeProvider class" on CodeProject, if you or any body has used it. Is it performance promising?

        Ketty

        1 Reply Last reply
        0
        • K Ketty Avashia

          I need to write a performance driven utility that allows user to write C# code in a text area. When they click on execute to see what is the final out, utility should complie and run the code written by user as if it is a function body. e.g. I am trying to achieve: private void ExecuteUser()//Function in Utility { /* -------Body of this function will be filled by User Code from text area at click of button. */ } I am not a C# coder for past few years:((, so any code snippet would be awesome. We are using .Net 2.0. VS 2005.

          Ketty

          P Offline
          P Offline
          PhilDanger
          wrote on last edited by
          #4
          private void ExecuteUser()//Function in Utility
          {
                if(txtUserCode.Text != "while(true){}"){
                  //run!
                }
          }
          

          Feel free to use this as a safeguard after you get the rest of it figured out :)

          K 1 Reply Last reply
          0
          • P PhilDanger
            private void ExecuteUser()//Function in Utility
            {
                  if(txtUserCode.Text != "while(true){}"){
                    //run!
                  }
            }
            

            Feel free to use this as a safeguard after you get the rest of it figured out :)

            K Offline
            K Offline
            Ketty Avashia
            wrote on last edited by
            #5

            I am not quite sure if I understand the idea completely. Please advice.

            Ketty

            P 1 Reply Last reply
            0
            • K Ketty Avashia

              I am not quite sure if I understand the idea completely. Please advice.

              Ketty

              P Offline
              P Offline
              PhilDanger
              wrote on last edited by
              #6

              Just a joke about the dangers of letting users execute their own code -- the code I wrote "checks" for an infinite loop.

              K 1 Reply Last reply
              0
              • P PhilDanger

                Just a joke about the dangers of letting users execute their own code -- the code I wrote "checks" for an infinite loop.

                K Offline
                K Offline
                Ketty Avashia
                wrote on last edited by
                #7

                :-DJokes are fine but I am looking for serious comments on this issue

                Ketty

                1 Reply Last reply
                0
                • K Ketty Avashia

                  I need to write a performance driven utility that allows user to write C# code in a text area. When they click on execute to see what is the final out, utility should complie and run the code written by user as if it is a function body. e.g. I am trying to achieve: private void ExecuteUser()//Function in Utility { /* -------Body of this function will be filled by User Code from text area at click of button. */ } I am not a C# coder for past few years:((, so any code snippet would be awesome. We are using .Net 2.0. VS 2005.

                  Ketty

                  C Offline
                  C Offline
                  Cyrilix
                  wrote on last edited by
                  #8

                  If you just want to compile code and execute it, you can always invoke the compiler with the correct command line options when the user would like to execute the code, and then start / wait on the process that you just compiled. That's probably the most general way to do things. I wouldn't advise it though, as I don't see any practical application. If, on the other hand, you want to do some kind of scripting, you could always encapsulate existing functions and dynamically call them by parsing a script, using a virtual address space within your actual program for the storage of stack variables and dynamically allocated memory.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups