如何从自定义事件处理程序返回一个项目
我正在处理的项目要求我能够在每次将项目添加到列表时触发事件。为此,我创建了一个继承自List的自定义List类,并添加了一个OnAdd事件。我还想返回作为EventArgs添加的项目,我为其添加了一些代码(如下所示):
public class ClientListObservable<Client>:List<Client>
{
public event EventHandler<EventArgs<Client>> OnAdd;
public void Add(Client item)
{
if (null != OnAdd)
{
OnAdd(this, new EventArgs<Client>(item));
}
base.Add(item);
}
}
public class EventArgs<Client> : EventArgs
{
public EventArgs(Client value)
{
m_value = value;
}
private Client m_value;
public Client Value
{
get { return m_value; }
}
}
这是我添加处理程序的方式
clientList.OnAdd += new EventHandler<EventArgs<Client>>(clientList_OnAdd);
但是,在OnAdd方法中:
private void clientList_OnAdd(object sender, EventArgs e)
{
//want to be able to access the data members of the Client object added
}
我只能访问e.Equals,e.GetHashCode,e.GetType和e.ToString,以及Client类的所有成员。
没有找到相关结果
已邀请:
4 个回复
懊毁暗
然后:
末钉蹈泰唬
?
缕嚏冻
筷啸够对铅