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. anonymous delegates with dynamic code

anonymous delegates with dynamic code

Scheduled Pinned Locked Moved C#
questiondiscussion
9 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.
  • G Offline
    G Offline
    Goncalo Oliveira
    wrote on last edited by
    #1

    let's assume this... bt is a Button; bt.Click += delegate( object sender, EventArgs e ) { MessageBox.Show( "something" ); }; I also know that we can do this: string s = "something"; bt.Click += delegate( object sender, EventArgs e ) { MessageBox.Show( s ); }; But.. what if I wanted to put inside the delegate some code that is stored in a string? like... string code ="MessageBox.Show( \"something\" );"; bt.Click += delegate( object sender, EventArgs e ) { ??? code ???? }; is there anyway one can do something similar to this? I thought of reflection, but... the context would be missing. Any thoughts? thanks...

    Gonçalo A.

    N 1 Reply Last reply
    0
    • G Goncalo Oliveira

      let's assume this... bt is a Button; bt.Click += delegate( object sender, EventArgs e ) { MessageBox.Show( "something" ); }; I also know that we can do this: string s = "something"; bt.Click += delegate( object sender, EventArgs e ) { MessageBox.Show( s ); }; But.. what if I wanted to put inside the delegate some code that is stored in a string? like... string code ="MessageBox.Show( \"something\" );"; bt.Click += delegate( object sender, EventArgs e ) { ??? code ???? }; is there anyway one can do something similar to this? I thought of reflection, but... the context would be missing. Any thoughts? thanks...

      Gonçalo A.

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      No you can't use a string for the body of the delegate. If you need to take different actions when the button is clicked, then create a handler and branch the code accordingly.


      only two letters away from being an asset

      G 1 Reply Last reply
      0
      • N Not Active

        No you can't use a string for the body of the delegate. If you need to take different actions when the button is clicked, then create a handler and branch the code accordingly.


        only two letters away from being an asset

        G Offline
        G Offline
        Goncalo Oliveira
        wrote on last edited by
        #3

        Even though... if I create an EventHandler, I'll still have the same problem. If the inner code is coming from another source, I still don't have a way to "reflect" that code. Am I right? Thanks for the reply, btw

        Gonçalo A.

        S 1 Reply Last reply
        0
        • G Goncalo Oliveira

          Even though... if I create an EventHandler, I'll still have the same problem. If the inner code is coming from another source, I still don't have a way to "reflect" that code. Am I right? Thanks for the reply, btw

          Gonçalo A.

          S Offline
          S Offline
          Skippums
          wrote on last edited by
          #4

          So, if I understand correctly, you have an object, call it B, who has the code (in string format) to run when some event in object A is fired, right? I don't understand why you can't have obj B simply add an event listener to the event of obj A. It sounds like you want to dynamically run some annonymous code from B that you won't know until runtime, but if at runtime you know the code, then why can't you just do A.Click += new ClickEventHandler(B.DoWhatever);? If this is not what you need (or want), then please explain a bit more on why exactly you need to have the php exec command in C#.

          Sounds like somebody's got a case of the Mondays -Jeff

          G 1 Reply Last reply
          0
          • S Skippums

            So, if I understand correctly, you have an object, call it B, who has the code (in string format) to run when some event in object A is fired, right? I don't understand why you can't have obj B simply add an event listener to the event of obj A. It sounds like you want to dynamically run some annonymous code from B that you won't know until runtime, but if at runtime you know the code, then why can't you just do A.Click += new ClickEventHandler(B.DoWhatever);? If this is not what you need (or want), then please explain a bit more on why exactly you need to have the php exec command in C#.

            Sounds like somebody's got a case of the Mondays -Jeff

            G Offline
            G Offline
            Goncalo Oliveira
            wrote on last edited by
            #5

            Well, the thing is, the code I do want to run, is not something I know. It's something I retrieve at runtime, let's say, from a file. I open a file, read the content, and then, have an event to run that content. That is what I want, so I can't have a B.DoWhatever function. I know this is something awkward to want... but this would be what I need. Any thoughts? :) Thank you for the reply

            Gonçalo A.

            T 1 Reply Last reply
            0
            • G Goncalo Oliveira

              Well, the thing is, the code I do want to run, is not something I know. It's something I retrieve at runtime, let's say, from a file. I open a file, read the content, and then, have an event to run that content. That is what I want, so I can't have a B.DoWhatever function. I know this is something awkward to want... but this would be what I need. Any thoughts? :) Thank you for the reply

              Gonçalo A.

              T Offline
              T Offline
              techieboi
              wrote on last edited by
              #6

              Although as a programmer this approach appears to make sense to you (you are loading source code into a dynamic method), to the computer the approach does not make sense as the source code you are loading is not compiled. In order to achieve this, you would have to load the source code then compile it at run-time and then execute it most likely using reflection ;->

              G 1 Reply Last reply
              0
              • T techieboi

                Although as a programmer this approach appears to make sense to you (you are loading source code into a dynamic method), to the computer the approach does not make sense as the source code you are loading is not compiled. In order to achieve this, you would have to load the source code then compile it at run-time and then execute it most likely using reflection ;->

                G Offline
                G Offline
                Goncalo Oliveira
                wrote on last edited by
                #7

                Those were my thoughts exactly, that I was stuck with reflection. Though, the context is a problem... I cannot fit the code in the context that I want. Anyways, thank you for the replies.

                Gonçalo A.

                S 1 Reply Last reply
                0
                • G Goncalo Oliveira

                  Those were my thoughts exactly, that I was stuck with reflection. Though, the context is a problem... I cannot fit the code in the context that I want. Anyways, thank you for the replies.

                  Gonçalo A.

                  S Offline
                  S Offline
                  Skippums
                  wrote on last edited by
                  #8

                  Ahhhh, now I understand. Do you have control over the code in the file, or is it a third party thing that you don't want to edit? If you have control, why not make the code a class with a method that does the work for you? For example, if you want to take in a string a return a boolean, make a static class "Container" with the method "public static bool DoWork(string input)". Does this help at all? I think the problem is you are trying to modify the running dll at run-time, which sounds like it is not possible (without hacked code and a lot of work). You need to branch to new code, execute it, then return, which is exactly what functions are for. Hope this helps,

                  Sounds like somebody's got a case of the Mondays -Jeff

                  G 1 Reply Last reply
                  0
                  • S Skippums

                    Ahhhh, now I understand. Do you have control over the code in the file, or is it a third party thing that you don't want to edit? If you have control, why not make the code a class with a method that does the work for you? For example, if you want to take in a string a return a boolean, make a static class "Container" with the method "public static bool DoWork(string input)". Does this help at all? I think the problem is you are trying to modify the running dll at run-time, which sounds like it is not possible (without hacked code and a lot of work). You need to branch to new code, execute it, then return, which is exactly what functions are for. Hope this helps,

                    Sounds like somebody's got a case of the Mondays -Jeff

                    G Offline
                    G Offline
                    Goncalo Oliveira
                    wrote on last edited by
                    #9

                    Yes, I kind of wanted something very weird, I said it. But it's only logical that it's not possible. That would mean to alter a dll in runtime... The decision I came up to go around this is using reflection. I send in a sort of a sandbox with variables that I might want to alter in the code, fetch the code from the file, encapsulate it inside a neat namespace with a class and a method, compile it, and run it. Seems to do the trick. I had to create a few "syntax hacks" to access the sandbox, like... $var as Label.Text = "something"; and in runtime replace the "pretty" line into something more ugly to access the sandbox. It works, and does the job very well. I also needed to create two more "syntax hacks" in case I need an extra reference, or to use an extra namespace that is not included by default in the assembly chunk. #assembly "System.Drawing.dll"; #using System.IO; Those three "hacks" are not very elegant, but... it allows me to have this done in a very generic way, which, in the current project, is the only way. Thank you all for the replies... and... if anyone as any thoughts on this, please let me know.

                    Gonçalo A.

                    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