c#事件的发布-订阅模型_C#订阅基于参数类型的事件?

I have a Commander class, which handles commands. All these commands implement the ICommand interface. Basically the command pattern...

Now I want to create something similar to an event for each specific type of command, without actually making an event for each specific type in the commander. The commander should not be coupled to each type of command.

So my command has a method void Subscribe(Action callback) where T: ICommand. If a subscriber calls this with the method void MyAttackCommandHandler(AttackCommand att) as the parameter, I expect the subscriber to get a callback only for AttackCommands. However another class can also subscribe for a different command.

I tried creating a dictionary, that maps the type of the parameter (the kind of command) to a list of subscribers: Dictionary>> _subscriptions, and then my subscribe method would look something like:

public void Subscribe(Action callback)

where T: ICommand

{

Type type = typeof(T);

if (_subscriptions.ContainsKey(type))

{

List> subscribtions = _subscriptions[type];

subscribtions.Add(callback);

}

else ... //create a new entry in _subscriptions

}

This however doesn't work because callback isn't of the type Action, but of Action for instance.

How would one implement this cleanly?

Thanks!

解决方案

Try this

subscribtions.Add(i => callback((T)i));

If the above doesn't work please provide a full example that shows your problem.

Something like this:

using System;

using System.Collections.Generic;

namespace Example

{

class Program

{

static void Main(string[] args)

{

Commander C = new Commander();

C.Subscribe((MyCommand i) => { Console.WriteLine(i.Value); });

C.Subscribe((SquareMyCommand i) => { Console.WriteLine(i.Value); });

C.Subscribe((SquareMyCommand i) => { Console.WriteLine("**" + i.Value + "**"); });

C.Do(new MyCommand(2));//1 callback , Prints 2

C.Do(new SquareMyCommand(3));//2 callbacks, Prints 9 , **9**

Console.ReadLine();

}

}

public class Commander

{

Dictionary>> dictionary = new Dictionary>>();

public void Subscribe(Action callback) where T : ICommand

{

Type type = typeof(T);

List> subscribtions = null;

dictionary.TryGetValue(type, out subscribtions);

if (subscribtions == null)

{

subscribtions = new List>();

dictionary.Add(type, subscribtions);

}

subscribtions.Add(i => callback((T)i));

}

public void Do(T t) where T : ICommand

{

foreach (var item in dictionary[t.GetType()])

item(t);

}

}

public class MyCommand : ICommand

{

public MyCommand(int x) { Value = x; }

public int Value { get; set; }

}

public class SquareMyCommand : ICommand

{

public SquareMyCommand(int x) { Value = x * x; }

public int Value { get; set; }

}

public interface ICommand

{

int Value { get; set; }

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值