0

I have a module (Fody.MethodTimer) that I can extend my classes with attributes. When my class'es method gets executed it, invokes a static method (in another class)

public  class CommandBase
{
    [Time]
    public bool test()
    {
        return true;
    }
}

public static class MethodTimeLogger
{
    public static void Log(MethodBase methodBase, long milliseconds)
    {
        //Do some logging here
    }
}

Basically, after the method is call of test is complete, the Log method gets executed. As you can see, it get a MethodBase argument and has all that is needed do decribe the method that invoked this method call.

my Question is, If it is possible to get the object that invoked the Log method Call, out of The .NET MethodBase class instance.

user853710
  • 1,715
  • 1
  • 13
  • 27

1 Answers1

4

No. MethodBase is extracted from a type, not an instance. You will need to pass the instance in as a parameter if you want to invoke methods on it.

Even if you could, how would you know what parameter values to use when calling the method?

This is why all built-in event handlers have an object sender parameter - so you know which object triggered the event.

D Stanley
  • 149,601
  • 11
  • 178
  • 240