How do i go about counting how many times a messag

2019-09-19 09:19发布

问题:

I am trying to get my program to Count how many times a certain messagebox in my program appears while it is running.

I have looked up how to do this and it seems that I will need to use int count but I'm not sure how to code this for Messageboxes as I can only find count code for strings and arrays.

回答1:

It is better idea to make some static wrapper class for MessageBox class and count it there in some kind of static variable.



回答2:

There are two solutions for this

1- create a class Adapter that wraps MessageBox class, add a counter field to the Adapter and increment it in every call

 class MyMessageBox
 {
 static int counter;

 static void Show(string msg)
 {
   counter++;
   MessageBox.Show(msg);
 }
 }

using this approach you assume that users are using your MessageBox

2- The second solution is AOP
use an Aspect Oriented framework like PostSharp to count Show calls



标签: c# counting