How to add a callback to a web project

I want to add a generic callback function to all methods of an object. I found this SO question , but in this question I would have to modify every function of the class. I would like to define some beforeFilter method that gets a trigger before every method call. I know that I could use Action delegates , but I do not know how to do this.

UPDATE: As an example, what I want will be something like this

 Class MyClass Sub MyCallback 'callback code... End Sub 'Rest of tyhe code End Class 

As you can see, it would be ideal to add a simple (or more than one) method to get a trigger before all other methods. Maybe with some base class or interface, I don’t know (why I ask).

Some other option that I think might be useful is to clone an object and define a new class instead of adding code to an existing one. So, in this Class use Object to access the trhough Type methods

 Class NewClass Sub AddCallback(Obj As Object, Callback As Action) 'Add the callback here End Sub End Class 

I just guess, maybe some of my options are even not viable, so please help me with this.

Closest approach

What I have is this method

 Sub RunWithCallback(beforFilter As Action, f As Action) beforeFilter() f() End Sub 

(This means a lot of overloading for RunWithCallback to control Sub and functions with Action and Func delegates with different number of parameters) To use this, I would have to run this Sub instead of calling any of the object's methods (passing the ByRef parameter to catch the return value in the functions). Something like that

 Public Sub DoNothing() Debug.WriteLine("Callback working!") End Sub Public Sub BeforeFilter() Debug.WriteLine("Testing callback...") End Sub 'To run the above function RunWithCallback(AddressOf BeforeFilter, AddressOf DoNothing) 

I think there should be a better solution for this, which will avoid calling the same sub and avoid overloads, a kind of dynamic extension method for a class class

+7
c # callback delegates
source share
2 answers

Here is an example of how delegates work and how you can use them:

 public class MyClass { // MyDelegate callback holder private MyDelegate _myDelegate; // Singleton holder private static volatile MyClass _instance; ///<summary> /// Instance property ///</summary> public static MyClass Instance { get { if (_instance == null) { _instance = new MyClass(); } _instance.MyCallbacks(_instance.MyDelegateCallback, _instance.MyDelegateCallback1); return _instance; } } ///<summary> /// Instance multi-callback caller ///</summary> ///<param name="callbacks">calbacks to call</param> ///<returns>MyClass</returns> public static MyClass Instance(params MyDelegate[] callbacks) { if (_instance == null) { _instance = new MyClass(); } _instance.MyCallbacks(callbacks); return _instance; } ///<summary> /// MyClass constructor ///</summary> private MyClass() { // Define the callback MyDelegate = MyDelegateCallback; } ///<summary> /// MyDelegate property, here u can change the callback to any function which matches the structure of MyDelegate ///</summary> public MyDelegate MyDelegate { get { return _myDelegate; } set { _myDelegate = value; } } ///<summary> /// Call all given callbacks ///</summary> ///<param name="callbacks">callbacks u want to call</param> public void MyCallbacks(params MyDelegate[] callbacks) { if (callbacks != null) { foreach (MyDelegate callback in callbacks) { if (callback != null) { callback.Invoke(null); } } } } ///<summary> /// RunTest, if u call this method the defined callback will be raised ///</summary> public void RunTest() { if (_myDelegate != null) { _myDelegate.Invoke("test"); } } ///<summary> /// MyDelegateCallback, the callback we want to raise ///</summary> ///<param name="obj"></param> private void MyDelegateCallback(object obj) { System.Windows.MessageBox.Show("Delegate callback called!"); } ///<summary> /// MyDelegateCallback1, the callback we want to raise ///</summary> ///<param name="obj"></param> private void MyDelegateCallback1(object obj) { System.Windows.MessageBox.Show("Delegate callback (1) called!"); } } ///<summary> /// MyDelegate, the delegate function ///</summary> ///<param name="obj"></param> public delegate void MyDelegate(object obj); 

I updated my answer, see MyCallbacks(params MyDelegate[] callbacks) . This will trigger various callbacks that match the MyDelegate structure.
Edit:
Added MyClass.Instance and MyClass.Instance(params MyDelegate[] callbacks) .

+1
source share

Have you ever considered using the Aspect library? To your question, it seems that you can really solve your problem with this approach.

For example, here you can use the PostSharp library , simply output the OnMethodBoundaryAspect class and handle the four main events available:

or two additional:

  • Onresume
  • Onield

or any of them. A simple diagram is as follows:

 int MyMethod(object arg0, int arg1) { OnEntry(); try { // YOUR CODE HERE OnSuccess(); return returnValue; } catch ( Exception e ) { OnException(); } finally { OnExit(); } } 

Thus, you can easily call the Callback function (and you can extract the parameters that passed at run time into the OnSuccess method:

 public override void OnSuccess(MethodExecutionArgs args) { CallBack(GetValueYouNeed(args)); } 

The simple version of PostSharp is a free license, distributed and well-documented, so I highly recommend that you explore this approach.

+1
source share

All Articles