我想为对象的所有方法添加一个通用的回调函数.我找到了
this SO question,但在那个方法中,我将不得不修改该类的每个功能.我想定义一些在每个方法调用之前得到trigered的beforeFilter方法.我知道我可以使用
Action delegates
,但我不知道如何.
更新:作为一个例子,我想要的是这样的
Class MyClass Sub MyCallback 'callback code... End Sub 'Rest of tyhe code End Class
正如您所看到的,理想情况是在所有其他方法之前添加一个简单(或多个)方法来进行三重处理.也许有一些基类或接口,我不知道(这就是我要求的原因).
我认为可能有的其他选项是克隆对象并定义一个新类,而不是将代码添加到现有类中.因此,在该类中使用Object来访问类型的方法
Class NewClass Sub AddCallback(Obj As Object,Callback As Action) 'Add the callback here End Sub End Class
我只是猜测,也许我的一些选择甚至是不可行的,所以请帮助我
最近的方法
我现在拥有的是这种方法
Sub RunWithCallback(beforFilter As Action,f As Action) beforeFilter() f() End Sub
(这意味着RunWithCallback使用Actions和Funcs代理管理Sub和函数的大量重载,具有不同数量的参数)要使用它,我必须运行此Sub而不是调用任何Object方法(传递ByRef参数)捕捉函数中的返回值).像这样的东西
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)
解决方法
以下是代理如何工作以及如何使用它们的示例:
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);
我更新了我的答案,看看MyCallbacks(params MyDelegate []回调).它将调用与MyDelegate.Edit结构相匹配的不同回调:添加了MyClass.Instance& MyClass.Instance(params MyDelegate []回调).