泛化委托代码 (C#)

发布于 2024-11-07 10:46:06 字数 1160 浏览 2 评论 0原文

我有两个非常相似的类,它们基本上做同样的事情。唯一的区别在于提供给每个类的实例的回调处理程序。回调处理程序不同,并且接受不同的参数。我想将这些类中的大部分代码概括为基类。关于如何智能地概括委托代码有什么想法吗?我使用 .NET 2.0

注意: 我阅读了 这个关于委托继承的非常有用的博客以及关于委托的协变和逆变的文章,但我仍然不知道如何在这里应用这些知识。


public class A
{
    public delegate void AHandler(string param1, string param2);
    public void AcceptHandler(string param3, AHandler handler);
    public void InvokeHandler(string forParam1, string forParam2);

    // the rest is same
}

public class B
{
    public delegate void BHandler(int param1);
    public void AcceptHandler(int param2, int param3, int param4, BHandler handler);
    public void InvokeHandler(int forParam1);

    // the rest is same
}

编辑:代码的“其余部分”完全相同,除了对具有不同签名的委托方法的调用之外。像这样的东西:


public void StartListening()
{
   Timer timer = new Timer(CheckForChanges, null, 0, 1000);            
}

private void CheckForChanges()
{
    // pull changes, and pass different params to InvokeHandler()
}

I have two very similar classes that do essentially the same thing. The only difference is in a callback handler provided to an instance of each class. The callback handlers are different and they are accepted with different parameters. I would like to generalize most of the code from these classes into a base class. Any ideas on how to generalize the delegate code intelligently? I'm on .NET 2.0

Note: I read this very useful blog on inheritance with delegates and articles on covariance and contravariance with delegates, but I still don't see how that knowledge can be applied here.


public class A
{
    public delegate void AHandler(string param1, string param2);
    public void AcceptHandler(string param3, AHandler handler);
    public void InvokeHandler(string forParam1, string forParam2);

    // the rest is same
}

public class B
{
    public delegate void BHandler(int param1);
    public void AcceptHandler(int param2, int param3, int param4, BHandler handler);
    public void InvokeHandler(int forParam1);

    // the rest is same
}

EDIT: "the rest" of the code is exact same, except calls to the delegate methods that have different signatures. Something like this:


public void StartListening()
{
   Timer timer = new Timer(CheckForChanges, null, 0, 1000);            
}

private void CheckForChanges()
{
    // pull changes, and pass different params to InvokeHandler()
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞笑我太脆弱 2024-11-14 10:46:06

为什么不这样设置:
编辑:我已更新以包含您编辑的方法。

public abstract class AbstractBase {
    // "the rest"
    public void StartListening() {
        Timer timer = new Timer(CheckForChanges, null, 0, 1000);            
    }
    protected abstract void CheckForChanges();
}

public class A : AbstractBase {
    public delegate void AHandler(string param1, string param2);
    public void AcceptHandler(string param3, AHandler handler);
    public void InvokeHandler(string forParam1, string forParam2);
    protected override void CheckForChanges() {
        //Do stuff for this version of the class
    }
}

public class B : AbstractBase {
    public delegate void BHandler(int param1);
    public void AcceptHandler(int param2, int param3, int param4, BHandler handler);
    public void InvokeHandler(int forParam1);
    protected override void CheckForChanges() {
        //Do stuff for this version of the class
    }
}

这样,您将在单个类中拥有相同的所有代码,然后各个类 AB 可以具有您需要的任何形式的方法。

或者您是否正在寻找一种通用调用委托的方法,而不管哪个类?

IE。像这样的东西:

AbstractBase ab = new A();
ab.InvokeDelegate();

Why not set it up like this:
Edit: I've updated to include the methods from your edit.

public abstract class AbstractBase {
    // "the rest"
    public void StartListening() {
        Timer timer = new Timer(CheckForChanges, null, 0, 1000);            
    }
    protected abstract void CheckForChanges();
}

public class A : AbstractBase {
    public delegate void AHandler(string param1, string param2);
    public void AcceptHandler(string param3, AHandler handler);
    public void InvokeHandler(string forParam1, string forParam2);
    protected override void CheckForChanges() {
        //Do stuff for this version of the class
    }
}

public class B : AbstractBase {
    public delegate void BHandler(int param1);
    public void AcceptHandler(int param2, int param3, int param4, BHandler handler);
    public void InvokeHandler(int forParam1);
    protected override void CheckForChanges() {
        //Do stuff for this version of the class
    }
}

This way, you'll have all your code that is the same in a single class, and then the individual classes A and B can have whatever form of the methods you need.

Or are you looking for a way to invoke the delegates generically irrespective of which class?

ie. Something like:

AbstractBase ab = new A();
ab.InvokeDelegate();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文