1

I have a class

public class ApplicationClass
{
    public  ApplicationClass() 
    {
        // Some code here                   

        public bool Check() 
        {
            // some code here

        }
    }
}

I am calling the Check function in a click event

protected void CheckFunction_Click(object sender, EventArgs e)
{
    new ApplicationClass().Check();
}

Now in the ApplicationClass constructor I want to get the info about the name of the function which is called and parameters if any i.e the Check function.

How can I get these details ?

I want to save all the functions that are being called. Instead of placing the call to function at every function can I have it at one place? It need not be inside a constructor necessarily.

Vignesh Subramanian
  • 7,161
  • 14
  • 87
  • 150
  • 4
    Why do you want to know? It's a bad design. – John Saunders Jul 08 '14 at 12:42
  • 1
    How would you expect that to work? During the constructor the class is being created therefore it doesn't know about you calling `Check` at that point. – James Jul 08 '14 at 12:42
  • @JohnSaunders I want to save all the functions that are being called. Instead of placing the call to function at every function can I have it at one place? – Vignesh Subramanian Jul 08 '14 at 12:54
  • @James is there any other way to get it? I dont want to have the code to save function name inside each and every function – Vignesh Subramanian Jul 08 '14 at 12:56
  • What do you plan to do with the function name? – John Saunders Jul 08 '14 at 12:59
  • I am trying to mine the data about the usage of the application and understand about user behavior by getting these function names which will imply the usage – Vignesh Subramanian Jul 08 '14 at 13:02
  • @vignesh without knowing what you plan to do with the name then I can't really suggest anything. – James Jul 08 '14 at 13:13
  • @James I want to use it for getting to know the most and least used functionalities in the application. – Vignesh Subramanian Jul 08 '14 at 13:15
  • @vignesh yeah, I get that but you still haven't explained what you intend on doing with the information i.e. is it going to be inserted into a DB, logged to a file etc. – James Jul 08 '14 at 13:29
  • Your intentions - data mining for analysing user behavior - rather seem to be something which should be logged to a file. I do not understand why you actually need the information you are looking for; you could read the methods calls out of a detailed logfile though (if you create one). – Alex Jul 08 '14 at 13:48
  • @James No i am planning to create a webservice which will store all the function names into the DB. So when i consume the webservice i should call the method only once which will keep track of all function calls and save them – Vignesh Subramanian Jul 08 '14 at 15:44

2 Answers2

3

This is not possible. Maybe it looks like it is because of the notation. This is what your code expands to:

protected void CheckFunction_Click(object sender, EventArgs e)
{
    var temp = new ApplicationClass();
    temp.Check();
}

If you just want to call a method of class without needing an instance, you might want to use a static method - at least your example looks like it. Of course initialization must be done elsewhere or in a static constructor. A static constructor is invoked only once though!

public static bool Check() 
{
    // some code here
}

ApplicationClass.Check();

If you want information about the methods invoking the current method you may use.

System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace();

Please note that fetching the stack trace can be considered slow, it should not be used in a loop or performance critical code.

Alex
  • 5,240
  • 1
  • 31
  • 38
2

That is not possible. If it was the other way around, you could have used the StackTrace class to check what was called, but as said, that is not the case now. (If you had called Check first, it would have appeared on the stack then)

You could opt to use Expression or LamdbaExpression class to retrieve information of the call.

See the question and answers from Get the name of a method using an expression to get an idea how Expressions might be handy.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325