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. Tool to add code in my existing class

Tool to add code in my existing class

Scheduled Pinned Locked Moved C#
debuggingtutorialquestion
4 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.
  • B Offline
    B Offline
    Bastien Vandamme
    wrote on last edited by
    #1

    I would like to know if a toll exist to "inject" code in existing class. I'm thinking about a system working with attribute. These attributes are not used at compile or runtime but just to specify part of code I want to inject during coding. For example attribute [LogThis]

        \[LogThis\]
        private void ConsoleTextBox\_TextChanged(object sender, TextChangedEventArgs e)
        {
            consoleTextBox.ScrollToEnd();
        }
    

    Then I run a tool that will add log to all [LogThis] methods

        \[LogThis\]
        private void ConsoleTextBox\_TextChanged(object sender, TextChangedEventArgs e)
        {
            log.Trace("starting ConsoleTextBox\_TextChanged");
            consoleTextBox.ScrollToEnd();
        }
    

    Does somthin similar exists?

    L Richard DeemingR B 3 Replies Last reply
    0
    • B Bastien Vandamme

      I would like to know if a toll exist to "inject" code in existing class. I'm thinking about a system working with attribute. These attributes are not used at compile or runtime but just to specify part of code I want to inject during coding. For example attribute [LogThis]

          \[LogThis\]
          private void ConsoleTextBox\_TextChanged(object sender, TextChangedEventArgs e)
          {
              consoleTextBox.ScrollToEnd();
          }
      

      Then I run a tool that will add log to all [LogThis] methods

          \[LogThis\]
          private void ConsoleTextBox\_TextChanged(object sender, TextChangedEventArgs e)
          {
              log.Trace("starting ConsoleTextBox\_TextChanged");
              consoleTextBox.ScrollToEnd();
          }
      

      Does somthin similar exists?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Google knows: C# code injection - Google Search[^]

      1 Reply Last reply
      0
      • B Bastien Vandamme

        I would like to know if a toll exist to "inject" code in existing class. I'm thinking about a system working with attribute. These attributes are not used at compile or runtime but just to specify part of code I want to inject during coding. For example attribute [LogThis]

            \[LogThis\]
            private void ConsoleTextBox\_TextChanged(object sender, TextChangedEventArgs e)
            {
                consoleTextBox.ScrollToEnd();
            }
        

        Then I run a tool that will add log to all [LogThis] methods

            \[LogThis\]
            private void ConsoleTextBox\_TextChanged(object sender, TextChangedEventArgs e)
            {
                log.Trace("starting ConsoleTextBox\_TextChanged");
                consoleTextBox.ScrollToEnd();
            }
        

        Does somthin similar exists?

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        The term you're looking for is "Aspect-Oriented Programming[^]", or "AOP". There are a variety of tools which can do this, both free and commercial. There are also some articles here on CodeProject on the topic. For example: Aspect Oriented Programming in C# with RealProxy[^]


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        1 Reply Last reply
        0
        • B Bastien Vandamme

          I would like to know if a toll exist to "inject" code in existing class. I'm thinking about a system working with attribute. These attributes are not used at compile or runtime but just to specify part of code I want to inject during coding. For example attribute [LogThis]

              \[LogThis\]
              private void ConsoleTextBox\_TextChanged(object sender, TextChangedEventArgs e)
              {
                  consoleTextBox.ScrollToEnd();
              }
          

          Then I run a tool that will add log to all [LogThis] methods

              \[LogThis\]
              private void ConsoleTextBox\_TextChanged(object sender, TextChangedEventArgs e)
              {
                  log.Trace("starting ConsoleTextBox\_TextChanged");
                  consoleTextBox.ScrollToEnd();
              }
          

          Does somthin similar exists?

          B Offline
          B Offline
          BillWoodruff
          wrote on last edited by
          #4

          While you have the information you need to explore AOP tools: very full-featured commercial, like PostSharp [^] ; open-source, like Fody [^]; and, others mentioned on this thread ... I wonder if there are other, simpler, techniques ... not based on Attributes and behind-the-scenes modification of Assemblies/IL code ... that could meet your needs ? These could include: use of #define compiler directives; run-time attaching/detaching EventHandlers. If you want to see examples of these methods, just ask. fyi: I use PostSharp, and it is an amazing tool-set.

          «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

          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