重放的功能和参数的列表 [英] replay a list of functions and parameters

查看:144
本文介绍了重放的功能和参数的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列的我想有以下功能的功能。

I have a series of functions that I want to have the following functionality.


  • 当函数被调用时,自身添加到功能记忆的参数和值

  • 名单
  • 允许的功能列表,在以后的日子

不同功能有多种不同的参数,我挣扎着叫想一个优雅的方式来做到这一点。任何帮助,将不胜感激。

The different functions have a variety of different parameters and I'm struggling to think of an elegant way to do this. Any help would be appreciated.

推荐答案

我认为这将满足您的需求,但功能没有,并称自己。

I think this would meet your needs, however the functions are not "adding themselves".

public class Recorder
{
    private IList<Action> _recording;
    public Recorder()
    {
        _recording = new List<Action>();
    }
    public void CallAndRecord(Action action)
    {
        _recording.Add(action);
        action();
    }
    public void Playback()
    {
        foreach(var action in _recording)
        {
            action();
        }
    }
}

//usage
var recorder = new Recorder();
//calls the functions the first time, and records the order, function, and args
recorder.CallAndRecord(()=>Foo(1,2,4));
recorder.CallAndRecord(()=>Bar(6));
recorder.CallAndRecord(()=>Foo2("hello"));
recorder.CallAndRecord(()=>Bar2(0,11,true));
//plays everything back
recorder.Playback();



,使功能添加自己将使用AOP LIB如postsharp单程或李林甫动态代理,并添加一个拦截它增加了功能和args到阵列。要做到这一点可能会比这将是值得国际海事组织,作为上述要简单得多并且仍然达到期望的功能更多的工作。

One way to make the functions "add themselves" would be to use an AOP lib such as postsharp or linfu dynamic proxy, and add an interceptor which adds the function and args to the array. To do this would probably be more work than it would be worth IMO, as the above is much simpler and still achieves the desired functionality.

这篇关于重放的功能和参数的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆