我有一些需要重构的代码有问题。现在它使用lambda作为事件处理程序,但它们不能正确删除。从我读过的内容来看,这甚至不可能?无论如何,我想重写它使用委托,而不是一个匿名函数,现在我的问题是,现在它需要一个行动作为参数,我似乎无法弄清楚如何将行动传递给我的新代表。这是代码:向事件处理程序参数添加一个动作c#
void RetrieveData(
int pointId,
int? chartCollectionId,
Action action)
{
if (pointId <= 0)
throw new ArgumentException("PointId not valid");
LastPointId = NextPointId;
NextPointId = pointId;
Clear();
_csr = new CustomerServiceRepository();
_csr.ServiceClient.GetChartDataCompleted += (se, ea) =>
{
_cachedCharts = ea.Result;
ChartDataRetrieved(ea.Result);
if (action != null)
action.Invoke();
_csr = null;
};
_csr.ServiceClient.GetChartDataAsync(
Settings.Current.Customer.CustomerName,
pointId,
chartCollectionId);
_csr.ServiceClient.GetChartDataCompleted -= (se, ea) => //remove after usage
{
_cachedCharts = ea.Result;
ChartDataRetrieved(ea.Result);
if (action != null)
action.Invoke();
_csr = null;
};
}
我在想,也许我可以创建以下文件:
public class extendedEventArgs : GetChartDataCompletedEventArgs
{
Action foo { get; set; }
}
void tang(object sender, extendedEventArgs e)
{
_cachedCharts = e.Result;
ChartDataRetrieved(e.Result);
if (action != null)
action.Invoke();
_csr = null;
}
而且通过行动在扩展事件参数的参数,但是当我尝试使用像这样
_csr.ServiceClient.GetChartDataCompleted += new EventHandler<extendedEventHandler>(tang);
它给出了一个错误:
Cannot implicitly convert type System.EventHandler<Conwx.Net.Client.CustomerClient.Controls.ChartControls.ChartListForecast.extendedEventArgs>' to System.EventHandler<Conwx.Net.Client.Framework.CustomerServiceReference.GetChartDataCompletedEventArgs>'
我在这里做错了什么?替代解决方案也受到欢迎。
.K
这看起来很有希望,非常感谢! – Keller