如何实现任务字典-泛型代理列表? [英] How to implement a dictionary of tasks -- list of generic-delegates?

查看:63
本文介绍了如何实现任务字典-泛型代理列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

请原谅我相对较长的描述!

我正在尝试实现以下内容:
具有键作为类型对象的任务以及代表的类型列表的值的字典.

要求是,对于给定的任务对象,我可能要执行一组操作(为什么要执行一组操作?因为根据任务的不同,我可能会执行多种类型的操作(由其返回类型和参数的类型决定),因此一组动作).另外,我希望该词典的用户能够在运行时指定针对特定任务要采取的措施.因此,我想实现一个以任务为键,并以委托人列表为值的字典.在给给定任务的字典中添加一个任务-动作对时,如果该任务已经存在于字典中,那么我想先搜索该任务的委托人列表,以查找已经添加的动作以及这个新动作类型(委托)已经存在,那么我将使用+=运算符将该新动作简单地添加到该委托中,否则我将在字典中的任务列表中添加一个新动作.如果字典中不存在该任务,那么我只需将任务-动作对添加到字典中.怎么做呢?我有这个未编译的小示例代码:

Hi All,

Pardon me for the relatively longer description!

I''m trying to implement the following:
A dictionary with keys as tasks of type objects, and values of type list of delegates.

The requirement is that for a given task object I might have a set of actions to be taken (Why a set? Because depending upon the task I might take multiple types of actions (dictated by their return type and parameters'' types), hence a set of actions). Also, I want the user of this dictionary to be able to specify at run-time what are the actions to be taken for a specific task. Hence, I want to implement a dictionary which has task as the key and a list of delegates as the value. While adding a task-actions pair to this dictionary for a given task, if the task already exists in the dictionary then I want to first search the list of delegates for this task for the already added actions and if this new action type (delegate) already exists then I''d simply add this new action using the += operator to that delegate else I''d add a new action to the list for the task in the dictionary. If the task doesn''t already exist in the dictionary then I''d simply add the task-actions pair to the dictionary. How to go about it? I have this non-compiling small sample code:

class TaskActionMapping
{
    static Dictionary<object, List<Delegate>> TaskActionMapping = new Dictionary<object, List<Delegate>>();

    public static bool AddTaskAction(object task, Delegate action)
    {
        if (TaskActionMapping.ContainsKey(task))
        {
            if (TaskActionMapping[task].Contains(action))
            {
                Delegate action_set = TaskActionMapping[task][TaskActionMapping[task].IndexOf(action)];
                action_set += action;
            }
            else
            {
                TaskActionMapping[task].Add(action);
            }
        }
        else
        {
            TaskActionMapping.Add(task, new List<Delegate>();
            TaskActionMapping[task].Add(action);
        }
    }
}

推荐答案

几件事.

1.您不能使用与所属类相同的名称来命名类型.
2.为什么不将以下行中的Delegate更改为MulticaseDelegate:
A couple of things.

1. You can''t have a type name with the same name as the class it belongs to.
2. Why not change Delegate into MulticaseDelegate in the line:
static Dictionary<object, List<Delegate>> TaskActionMapping = new Dictionary<object, List<Delegate>>();


而不是使用对象任务,让您的课程通用:

Instead of using object task, make your class generic:

class TaskActionMapping<task> where TASK : ??? /**/ {
   public void Add(TASK task, System.Func<TASK, bool> action) { /*...*/}
   // System.Func instead of Delegate.
   // bool is return value, but you can use another generic parameter 
}</task>



对于多播,它可以像这样工作:如果此键的操作不在字典中,则添加新的委托列表(最好是Queue),如果已经在字典中,则将另一个委托排队到现有队列中.

请在对问题的评论中看到我的问题.



For multi-casting, it could work like this: if an action for this key is not in dictionary, new list of delegates is added (better be Queue), if one is already in dictionary, enqueue another delegate to existing queue.

Please see my questions in my comment to the question.


这篇关于如何实现任务字典-泛型代理列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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