Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6557901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:07:57+00:00 2026-05-25T13:07:57+00:00

how can I implement analog of bind / unbind like in jQuery? class MyDataType<T>

  • 0

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");
...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:07:57+00:00Added an answer on May 25, 2026 at 1:07 pm

    You could do this using Castle DynamicProxy. First, create abstract base class that contains the Bind() and Unbind() methods and a dictionary to hold the invocations:

    public abstract class BindableBase
    {
        private readonly Dictionary<string, Action> m_boundMethods =
            new Dictionary<string, Action>();
    
        protected Dictionary<string, Action> BoundMethods
        {
            get { return m_boundMethods; }
        }
    
        public void Bind(string method, Action action)
        {
            if (!m_boundMethods.ContainsKey(method))
                m_boundMethods.Add(method, null);
    
            m_boundMethods[method] += action;
    
        }
    
        public void Unbind(string method, Action action)
        {
            if (m_boundMethods.ContainsKey(method))
            {
                m_boundMethods[method] -= action;
    
                if (m_boundMethods[method] == null)
                    m_boundMethods.Remove(method);
            }
        }
    }
    

    Then create concrete class that implements BindableBase. You need to make any methods you want to intercept virtual:

    public class Foo<T> : BindableBase
    {
        private readonly List<T> m_data = new List<T>();
    
        public virtual void Add(T value)
        {
            m_data.Add(value);
        }
    }
    

    Then create the interceptor that intercepts calls to any virtual methods and call the appropriate delegate from the dictionary:

    class Interceptor : IInterceptor
    {
        private static readonly PropertyInfo BoundMethodsProperty =
            typeof(BindableBase).GetProperty(
                "BoundMethods", BindingFlags.Instance | BindingFlags.NonPublic);
    
        public void Intercept(IInvocation invocation)
        {
            var boudMethods =
                (Dictionary<string, Action>)BoundMethodsProperty.GetValue(
                    invocation.InvocationTarget, null);
    
            invocation.Proceed();
    
            string method = invocation.Method.Name;
    
            Action action;
            if (boudMethods.TryGetValue(method, out action))
                action();
        }
    }
    

    Finally, it’s just a matter of generating the proxy and using it:

    var proxyGenerator = new ProxyGenerator();
    Foo<int> foo = proxyGenerator.CreateClassProxy<Foo<int>>(new Interceptor());
    foo.Bind("Add", LogAdd);
    foo.Add(42);
    foo.Unbind("Add", LogAdd);
    foo.Add(43);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does ASP.NET MVC provide any way to implement UpdateProgress WebForms control analog? Can anyone
In java a class can implement Iterable which lets you use the foreach() statement
Is there any way I can implement functionality which will clear all my data/files/shared_preferences/database
It seems like both can be used to define functions that you can implement
I know you can implement using Object class but is there any in Apple's
In C#, I can implement a generic interface twice on one class, using two
We can extend a class but we cannot implement a class. We can implement
Class A can implement multiple interfaces. What I do not understand is what happens
In java I can implement the composite design pattern as follows: interface Component{ void
I am wondering how I can implement something like the following: test(){ cat>file<<'EOF' abc

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.