[英]How to avoid the following repeated code?
I wrote two functions which look similar, how can I optimize them? 我写了两个看起来相似的函数,如何优化它们?
Note: 注意:
1. AsyncCompletedEventArgs
is the base class of DownloadStringCompletedEventArg
and UploadStringCompletedEventArgs
. 1. AsyncCompletedEventArgs
是DownloadStringCompletedEventArg
和UploadStringCompletedEventArgs
的基类。
2. Result
property is not in the AsyncCompletedEventArgs
. 2. Result
属性不在AsyncCompletedEventArgs
。
3. DownloadStringCompletedEventArgs
has a Error
property, if Error
is null
, then try to access Result
property, the exception occurs. 3. DownloadStringCompletedEventArgs
具有Error
属性,如果Error
为null
,则尝试访问Result
属性,将发生异常。
void fun1(DownloadStringCompletedEventArgs e)
{
try
{
string s = e.Result;
}
catch (WebException eX)
{
HandleWebException();
}
}
void fun2(UploadStringCompletedEventArgs e)
{
try
{
string s = e.Result;
}
catch (WebException eX)
{
HandleWebException();
}
}
You code may be changed to something like below: 您的代码可能会更改为如下所示:
void fun1(DownloadStringCompletedEventArgs e) { Process(e); }
void fun2(UploadStringCompletedEventArgs e) { Process(e); }
private void Process(dynamic eventArgs)
{
try
{
string s = eventArgs.Result;
}
catch (WebException e)
{
HandleWebException(e);
}
}
UploadStringCompletedEventArgs
and DownloadCompletedEventArgs
both extend AsyncCompletedEventArgs
but unfortunately the base class does not define the Result
property. UploadStringCompletedEventArgs
和DownloadCompletedEventArgs
都扩展了AsyncCompletedEventArgs
但不幸的是,基类未定义Result
属性。
A TryX pattern with a result accessor delegate might be appropriate here: 具有结果访问器委托的TryX模式在这里可能是合适的:
public bool TryGetResult(Func<string> resultAccessor, out string result)
{
try
{
result = resultAccessor();
return true;
}
catch(WebException)
{
HandleWebException();
result = null;
return false;
}
}
void fun1(DownloadStringCompletedEventArgs e)
{
string result;
if (TryGetResult(() => e.Result, out result))
{
// Success
}
}
void fun2(UploadStringCompletedEventArgs e)
{
string result;
if (TryGetResult(() => e.Result, out result))
{
// Success
}
}
I'd recommend trying to work in a check to AsyncCompletedEventArgs.Error , though, as exceptions are quite expensive. 但我建议尝试检查AsyncCompletedEventArgs.Error ,因为异常的代价很高。
Something like this: 像这样的东西:
void fun1(DownloadStringCompletedEventArgs e)
{
var result = Process<string>(e);
if (result != null)
{
// TODO your logic here
}
}
void fun2(UploadStringCompletedEventArgs e)
{
var result = Process<string>(e);
if (result != null)
{
// TODO your logic here
}
}
private T Process<T>(AsyncCompletedEventArgs result)
{
if (result.Error != null)
HandleWebException(result.Error);
else if (!result.Cancelled)
{
//var prop = result.GetType().GetProperty("Result");
//return (T) prop.GetValue(result, null);
return (T) ((dynamic)result).Result;
}
//else // TODO handle cancelled
return default(T);
}
Maybe you could write a function that takes a parameter of type AsyncCompletedEventArgs (from which both eventArg classes you use inherit) and then attempt to cast it to the correct type in your code. 也许您可以编写一个函数,该函数采用AsyncCompletedEventArgs类型的参数(您使用的两个eventArg类都从该参数继承),然后尝试将其强制转换为代码中的正确类型。 That would allow you to complete both in the same method, but looking at your code it probably wouldn't have much benefit for you. 那将允许您使用相同的方法完成这两个操作,但是查看您的代码可能对您没有太大好处。 Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.