多个方法中如何使用trycatch?抱歉,如果我的问题很愚蠢,但我有这样的代码:publicObject1Method1(Object2parameter){try{returnthis.linkToMyServer.Method1(parameter);}catch(Exceptione){this.Logger(e);}返回空值;}publicObject3Method2(Object4parameter){try{returnthis.linkToMyServer.Method2(parameter);}catch(Exceptione){this.Logger(e);}返回空值;}/*...*/publicObjectXXMethod50(ObjectXYparameter){try{returnthis.linkToMyServer.Method50(parameter);}catch(Exceptione){this.Logger(e);}返回空值;我想你看到了模式。有没有一种很好的方法可以在这个trycatch中只有一个trycatch并传递一个泛型方法?本能地,我会使用代表,但代表必须具有相同的签名吗?提前致谢。问候。每当您看到这样的代码时,您都可以应用模板方法模式。也许是这样的:privateTResultExecuteWithExceptionHandling(TParamparameter,Funcfunc){try{returnfunc(parameter);}catch(Exceptione){this.Logger(e);}返回默认值(TResult);}publicObject1Method1(Object2parameter){returnExecuteWithExceptionHandling(parameter,linkToMyServer.Method1);}publicObject3Method2(Object4parameter){returnExecuteWithExceptionHandling(parameter,linkToMyServer.Method2);等...这可能对你有用。以上就是C#学习教程:如何在多个方法中使用trycatch?如果分享的内容对你有用,需要进一步了解C#学习教程,希望你多多关注——publicobjectBaseMethod(object[]userParameters,StringFunctionName){try{TypethisType=this.获取类型();MethodInfotheMethod=thisType.GetMethod(FunctionName);对象返回对象;returnObj=theMethod.Invoke(this,userParameters);返回返回对象;}catch(Exceptione){this.Logger(e.InnerException);不代表立场,如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
