c# - 我如何设计一个类来接收具有未知数量参数的委托(delegate)?

标签 c# .net c#-3.0

我不断地发现自己不得不编写定时窗口服务来轮询外部队列或运行定时进程。因此,我有一个相当强大的模板来执行此操作,但我发现每次编写服务来执行此操作时,我都是从相同的模板开始,然后在其中编写流程。

今天早上,我发现自己想知道我是否真的可以将这个模板变成一个框架,我可以将我的流程注入(inject)其中。我的基本模板只有 122 行代码。由于每个过程的要求不同——即不同数量的参数、不同的参数类型和不同的依赖关系(有些依赖于 Web 服务,有些依赖于数据库等),我不知道如何设置我的基本模板来接收一个注入(inject)过程。

模板的核心只是一个计时器,它在初始化和启动进程时停止,然后在进程完成后重新启动计时器。然后,我将我的流程方法和任何依赖项直接添加到模板中。

有没有人知道如何做到这一点?我看过依赖注入(inject)并经常使用它来注入(inject)诸如数据存储连接之类的东西?有没有办法将具有未知数量/类型参数的委托(delegate)注入(inject)到类中?我看错了吗?

这是我的模板:

TimedProcess.Template.cs

using System;
using System.Timers;

public partial class TimedProcess : IDisposable
{
    private Timer timer;

    public bool InProcess { get; protected set; }

    public bool Running
    {
        get
        {
            if (timer == null)
                return false;

            return timer.Enabled;
        }
    }

    private void InitTimer(int interval)
    {
        if (timer == null)
        {
            timer = new Timer();
            timer.Elapsed += TimerElapsed;
        }
        Interval = interval;
    }

    public void InitExecuteProcess()
    {
        timer.Stop();
        InProcess = true;
        RunProcess();
        InProcess = false;
        timer.Start();   
    }

    public void TimerElapsed(object sender, ElapsedEventArgs e)
    {
        InitExecuteProcess();
    }

    public void Start()
    {
        if (timer != null && timer.Interval > 0)
            timer.Start();
    }

    public void Start(int interval)
    {
        InitTimer(interval);
        Start();
    }

    public void Stop()
    {
        timer.Stop();
    }

    public TimedProcess()
        : this(0)
    {
    }

    public TimedProcess(int interval)
    {
        if (interval > 0)
            InitTimer(interval);
    }

    private disposed = false;
    public Dispose(bool disposing)
    {
        if (disposed || !disposing)
            return;

        timer.Dispose();

        disposed = true;
    }

    public Dispose()
    {
        Dispose(true);
    }

    ~TimedProcess()
    {
        Dispose(false);
    }
}

TimedProcess.cs

using System;

public partial class TimedProcess
{
    public void RunProcess()
    {
        //Hook process to run in here
    }
}

所以我希望对其进行修改,以便我的 Windows 服务生成一个新的 TimedProcess 并将其注入(inject)需要运行的进程,从而从我的 Windows 服务中完全删除 TimedProcess 代码并使其引用 DLL。

编辑:感谢大家的帮助。我意识到,如果我将我的 RunProcess() 方法推到我的 TimedProcess 库之外,并将 that 作为构造函数中的一个 Action 传递进来,那么这会按照我希望的方式简化一切:

[为了简洁而简化]

public class TimedProcess
{
    Action RunProcess;
    Timer timer = new Timer();

    private void TimerElapsed(object sender, ElapsedEventArgs e)
    {
        if (RunProcess != null)
            RunProcess();
    }

    public TimedProcess(Action action, int interval)
    {
        timer.Interval = interval;
        RunProcess = action;
        timer.Start();
    }
}

最佳答案

这里的一种方法是使用捕获的变量,以便所有委托(delegate)本质上成为Action或者也许 Func<T> - 并通过匿名方法、捕获变量等的魔力将其余部分留给调用者 - 即

DoStuff( () => DoSomethingInteresting("abc", 123) );

(警告:注意异步/捕获——通常是一个糟糕的组合)

哪里DoStuff接受 Action .然后当你调用 Action参数会自动添加等。在某些 RPC 库代码中,我采用了另一种方法,使用 Expression - 所以我表达了一个服务接口(interface)(像往常一样),然后有如下方法:

Invoke(Expression<Action<TService>> action) {...}
Invoke<TValue>(Expression<Func<TService,TValue>> func) {...}

调用,例如:

proxy.Invoke(svc => svc.CancelOrder(orderNumber));

然后调用者说如果我们有那个接口(interface)的实现——除了我们从来没有真正做过;相反,我们拉 Expression分开查看被调用的方法、args 等——并将它们传递给 RPC 层。有兴趣的可以多讨论here , 或者代码可用 here .

关于c# - 我如何设计一个类来接收具有未知数量参数的委托(delegate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1601510/

相关文章:

c# - 检查字符数组是否包含不同于允许的字符

c# - 如何使用 lambda 表达式从字典中删除项目

c# - 通过唯一 ID 从集合中获取项目

c# - 在 C# 中创建一个可以处理多种返回类型的函数

c# - 泛型与否泛型

c# - 如何以编程方式向 VSTS/TFS 测试用例中的每个测试步骤添加/更新单独结果

.net - 不支持文化1033(0x0409)

c# - axVLCPlugin(VideoLAN) 更快的状态到正常状态

c# - Entity Framework 4

c# - 无法解析 'MediatR.IMediator'类型的服务