如何订阅多个事件
How to subscribe to multiple events
我有一个 UserGroup
对象,它有一个 属性: User
对象列表。每个 User
对象引发特定事件。我希望能够处理来自 UserGroup
中所有 Users
的事件。
例如:
public abstract class User : IUser
{
public event EventHandler<MatchCompletedEventArgs> MatchCompleted;
public bool Match(Job job)
{
bool result = UserMatch(job);
EventHandler<MatchCompletedEventArgs> handler = MatchCompleted;
handler?.Invoke(this, new MatchCompletedEventArgs(result));
return result;
}
protected abstract bool UserMatch(Job job);
}
public class UserGroup
{
public int Id { get; set; }
public string Name { get; set; }
public List<IUser> Users { get; set; }
public UserGroup(int id, string name)
{
Id = id;
Name = name;
Users = new List<IUser>();
}
public void AddUser(IUser user)
{
// TODO:
// In here I want to subscribe to user.MatchCompleted event
// Eventually I want to be able to handle MatchCompleted event in all users in the list
Users.Add(user);
}
public void OnMatchCompleted()
{
// TODO:
// Whenever, any of the users throws MatchCompleted completed event I need to store complex informations about the Match process,
// Ideally in one place, like this function.
}
public bool Match(Job job)
{
// Match will return TRUE only if all users will match it
foreach (var user in Users)
{
if (!user.Match(job)) return false;
}
return true;
}
}
代码中的这两个"TODO"就是我要找的。
如何捕捉一个可以由存储在一个列表中的多个对象引发的事件?
您可以在非常简单的方法中为每个 User
对象订阅事件:
public void AddUser(IUser user)
{
user.MatchCompleted += OnMatchCompleted; // subscribed event for each user
Users.Add(user);
}
您的活动将如下所示:
public void OnMatchCompleted(object sender,MatchCompletedEventArgs e)
{
User user = sender as User; // will work fine
IUser iUser = sender as IUser; // this will also work
// now you can use user information
// write your complex logic here
}
现在,当我们为每个用户订阅时,Users
集合中的每个用户都会调用 OnMatchCompleted
。
我有一个 UserGroup
对象,它有一个 属性: User
对象列表。每个 User
对象引发特定事件。我希望能够处理来自 UserGroup
中所有 Users
的事件。
例如:
public abstract class User : IUser
{
public event EventHandler<MatchCompletedEventArgs> MatchCompleted;
public bool Match(Job job)
{
bool result = UserMatch(job);
EventHandler<MatchCompletedEventArgs> handler = MatchCompleted;
handler?.Invoke(this, new MatchCompletedEventArgs(result));
return result;
}
protected abstract bool UserMatch(Job job);
}
public class UserGroup
{
public int Id { get; set; }
public string Name { get; set; }
public List<IUser> Users { get; set; }
public UserGroup(int id, string name)
{
Id = id;
Name = name;
Users = new List<IUser>();
}
public void AddUser(IUser user)
{
// TODO:
// In here I want to subscribe to user.MatchCompleted event
// Eventually I want to be able to handle MatchCompleted event in all users in the list
Users.Add(user);
}
public void OnMatchCompleted()
{
// TODO:
// Whenever, any of the users throws MatchCompleted completed event I need to store complex informations about the Match process,
// Ideally in one place, like this function.
}
public bool Match(Job job)
{
// Match will return TRUE only if all users will match it
foreach (var user in Users)
{
if (!user.Match(job)) return false;
}
return true;
}
}
代码中的这两个"TODO"就是我要找的。
如何捕捉一个可以由存储在一个列表中的多个对象引发的事件?
您可以在非常简单的方法中为每个 User
对象订阅事件:
public void AddUser(IUser user)
{
user.MatchCompleted += OnMatchCompleted; // subscribed event for each user
Users.Add(user);
}
您的活动将如下所示:
public void OnMatchCompleted(object sender,MatchCompletedEventArgs e)
{
User user = sender as User; // will work fine
IUser iUser = sender as IUser; // this will also work
// now you can use user information
// write your complex logic here
}
现在,当我们为每个用户订阅时,Users
集合中的每个用户都会调用 OnMatchCompleted
。