如果我的问题很愚蠢,我很抱歉,但我有这样的代码:
public Object1 Method1(Object2 parameter)
{
try
{
return this.linkToMyServer.Method1(parameter);
}
catch (Exception e)
{
this.Logger(e);
}
return null;
}
public Object3 Method2(Object4 parameter)
{
try
{
return this.linkToMyServer.Method2(parameter);
}
catch (Exception e)
{
this.Logger(e);
}
return null;
}
/* ... */
public ObjectXX Method50(ObjectXY parameter)
{
try
{
return this.linkToMyServer.Method50(parameter);
}
catch (Exception e)
{
this.Logger(e);
}
return null;
}我想你看到了其中的规律。在这个try catch中只有一个try catch并传递一个泛型方法,有没有一个好的方法?
我本能地会使用委托,但委托必须有相同的签名,对吗?
提前谢谢。
致以问候。
发布于 2014-05-02 16:07:36
这可能对你有用。
public object BaseMethod(object[] userParameters,String FunctionName)
{
try
{
Type thisType = this.GetType();
MethodInfo theMethod = thisType.GetMethod(FunctionName);
object returnObj;
returnObj = theMethod.Invoke(this, userParameters);
return returnObj;
}
catch (Exception e)
{
this.Logger(e.InnerException);
}
}https://stackoverflow.com/questions/23423043
复制相似问题