method with method name as parameter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • firepol
    New Member
    • Mar 2009
    • 7

    #1

    method with method name as parameter

    Hello,

    as "exercise" (I needed it) I've built a simple logging/tracing classLibrary and I'm using it to write in a txt what my application is doing, and save the exceptions.

    I'm not an expert programmer and I want to improve my library. Actually to trace something I have to do like this:

    Code:
    Logger log = new Logger();
    SomeMehod();
    log.Trace("I usually write a comment here, such as: SomeMehod()");
    Now, I'd like to add time measurement in my log. I've found an interesting class to do that, the Stopwatch one (using System.Diagnost ics).

    I can do that as follows:
    Code:
    Logger log = new Logger();
    Stopwatch watch = new Stopwatch();
    watch.Start();
    SomeMethod();
    watch.Stop();
    log.Trace("SomeMehod() time: " + watch.Elapsed.ToString() );
    Now, I'd like to add a new method in my classLibrary, and call it let's say MeasureAndTrace (string methodToCall, string logMessage);

    The tricky thing is the "methodToCa ll". I'm not even sure it's correct to use it as string. My idea is to use the method to launch a method and measure the time, as done before with the Stopwatch, as follows:

    Code:
    MeasureAndTrace("SomeMethod()", "SomeMethod launched");
    And this is how the MeasureAndTrace would look like, in my library.

    Code:
    MeasureAndTrace(string methodToCall, string logMessage)
    {
    Logger log = new Logger();
    Stopwatch watch = new Stopwatch();
    watch.Start();
    // PLEASE LOOK HERE
    //Here I need the code to launch the methodToCall defined as parameter
    watch.Stop();
    log.Trace("SomeMehod() time: " + watch.Elapsed.ToString() );
    }
    My question is, can you help me to correctly adapt my MeasureAndTrace method in my logging classLibrary and tell me what do add in the line "//Here I need the code to launch the methodToCall defined as parameter"?

    Note: methodToCall could be something more complex, e.g. a method containing some parameters...

    Another note: what happens if the method that I want to pass as methodToCall would return a value, would it be possible to still call it, e.g. like this (in case it returns a string)?

    Code:
    string resultString = MeasureAndTrace(string methodToCall, string logMessage);
    I've already googled for: c# function name parameter and read some suggestions by people to use delegates, or reflection. I'm reading some tutorials about delegates but actually I can't understand how to use them in my practical example above mentioned. If you can give me some code, I would really appreciate.

    Thanks a lot,

    cheers,

    --firepol
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    My two cents worth...
    Don't do it with methods: Do it with events.
    With a method you are re-directing program flow. Your working method does part of its job, then gets redirected to the log to write it down, then comes back to do more work.
    If you raise an event for things such as "StartStopWatch " "StopStopWa tch" "RecordLogEntry " you won't sidetrack your main working method. All it knows is to raise its hand and say "Go!" "Stop!" "Write".

    Now you can have any number of classes subscribe to the events and react.
    For example if you raise a "MethodStar ted" event you can have a stopwatch class start timing, and a LogClass write it in a text file, and a ListBox add it as an on-screen notification. Later when you update the application you take out the listbox, but you don't have to re-write all the other methods because its gone.

    Comment

    • firepol
      New Member
      • Mar 2009
      • 7

      #3
      tlhintoq: thanks for your 2 cents... Well I spent a few hours reading about delegates and events... but I don't see, in practice, how I can use them in my example explained in the post :-( or... it seems very complicated.

      If I've understood your post you suggest me to create a MethodStarted event. Seems a good idea, but then... should I create also a MethodStopped event?

      I will check again some events tutorials and see if I can build something and get back here with some fresh code. In the meanwhile if you have more szggestions you are welcome,

      cheers

      Comment

      Working...