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. How can i effectively write the code for calling a function before and after execution of every function in a class.

How can i effectively write the code for calling a function before and after execution of every function in a class.

Scheduled Pinned Locked Moved C#
question
7 Posts 5 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.
  • Y Offline
    Y Offline
    yeswanthkumar
    wrote on last edited by
    #1

    I already have very large amount of code . Now I want to integrate one more functionality for this code for which i have to execute some code before and after every function call .How can i do this effectively.

    P L J B 4 Replies Last reply
    0
    • Y yeswanthkumar

      I already have very large amount of code . Now I want to integrate one more functionality for this code for which i have to execute some code before and after every function call .How can i do this effectively.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      This is a complicated topic, and one you really should have designed for before you reached this point. Normally, you would apply something called Aspect Oriented Programming (AOP) to allow you to automatically add code to your application at specified points. This gives you fine grained control of what you want to do. As you haven't designed your application for this, you're probably going to have to fall back to a technique called IL weaving - this means that you are going to weave in IL code to your application (it's what most AOP systems do, but they give you control over the process - you're going to end up having to manually do this yourself). You will probably find this[^] article to be some help in getting started.

      This space for rent

      1 Reply Last reply
      0
      • Y yeswanthkumar

        I already have very large amount of code . Now I want to integrate one more functionality for this code for which i have to execute some code before and after every function call .How can i do this effectively.

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

        [logging - How can I add a Trace() to every method call in C#? - Stack Overflow](https://stackoverflow.com/questions/559148/how-can-i-add-a-trace-to-every-method-call-in-c) (See reference to "PostSharp")

        "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

        1 Reply Last reply
        0
        • Y yeswanthkumar

          I already have very large amount of code . Now I want to integrate one more functionality for this code for which i have to execute some code before and after every function call .How can i do this effectively.

          J Offline
          J Offline
          John C Rayan
          wrote on last edited by
          #4

          There are many ways to do this. However it depends how good your design and coding technique.

          1 Reply Last reply
          0
          • Y yeswanthkumar

            I already have very large amount of code . Now I want to integrate one more functionality for this code for which i have to execute some code before and after every function call .How can i do this effectively.

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

            In addition to the CP article that Pete cited, also see: [^], [^].

            «When I consider my brief span of life, swallowed up in an eternity before and after, the little space I fill, and even can see, engulfed in the infinite immensity of spaces of which I am ignorant, and which know me not, I am frightened, and am astonished at being here rather than there; for there is no reason why here rather than there, now rather than then.» Blaise Pascal

            Y 1 Reply Last reply
            0
            • B BillWoodruff

              In addition to the CP article that Pete cited, also see: [^], [^].

              «When I consider my brief span of life, swallowed up in an eternity before and after, the little space I fill, and even can see, engulfed in the infinite immensity of spaces of which I am ignorant, and which know me not, I am frightened, and am astonished at being here rather than there; for there is no reason why here rather than there, now rather than then.» Blaise Pascal

              Y Offline
              Y Offline
              yeswanthkumar
              wrote on last edited by
              #6

              Thank you very much BillWoodruff .Can you please tell em how to pass our own custom attributes .

              B 1 Reply Last reply
              0
              • Y yeswanthkumar

                Thank you very much BillWoodruff .Can you please tell em how to pass our own custom attributes .

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

                Question: what is the scale/scope of the code base you wish to monitor/log ? Multi-threading involved ? Team project with multiple code repositories ? Also see this recent article: [^].

                yeswanthkumar wrote:

                Can you please tell em how to pass our own custom attributes

                While I am a registered owner of PostSharp, I do not have the expertise to comment on any other method/tool, and I have never tried to explore creating custom attributes in PostSharp: however their documentation/support are very good, and I expect if the need arose, it could be done: [^]. imho, the task of IL-weaving/AOP is an extremely technically deep subject, and experimenting with "rolling your own" potentially risky in terms of technical "cost." Have you looked at Spring.NET: [^] ? cheers, Bill

                «When I consider my brief span of life, swallowed up in an eternity before and after, the little space I fill, and even can see, engulfed in the infinite immensity of spaces of which I am ignorant, and which know me not, I am frightened, and am astonished at being here rather than there; for there is no reason why here rather than there, now rather than then.» Blaise Pascal

                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