how can I implement analog of bind / unbind like in jQuery?
class MyDataType<T>
{
private List<T> data;
...
public void Add(T value)
{
data.Add(value);
}
...
}
...
MyDataTypeObject.Bind("Add",()=>Console.WriteLine("OnAdd"));
...
MyDataTypeObject.UnBind("Add");
...
You could do this using Castle DynamicProxy. First, create abstract base class that contains the
Bind()andUnbind()methods and a dictionary to hold the invocations:Then create concrete class that implements
BindableBase. You need to make any methods you want to intercept virtual:Then create the interceptor that intercepts calls to any virtual methods and call the appropriate delegate from the dictionary:
Finally, it’s just a matter of generating the proxy and using it: