ソースコードを乾かしたい。主に1行で異なるこれらの機能のいくつかを持っていたとしましょう-どうすればそれを達成できますか?
public Result foo (int x, int y, int z){
Log.Say("Foo started!"); <<< DIFFERENCE
DoSomeMagic();
try {
result = controller.foo(x, y, z); <<< DIFFERENCE
} catch (Exception){
Log.Say("Something went terribly wrong");
}
return result;
}
public Result bar (Person a, string whatever){
Log.Say("Bar started!"); <<< DIFFERENCE
DoSomeMagic();
try {
result = controller.bar(a, whatever); <<< DIFFERENCE
} catch (Exception) {
Log.Say("Something went terribly wrong");
}
return result;
}
それはそれほど難しいことではないので、それは私を狂わせていますか?私は今ではさまざまなアプローチに混乱しすぎています。これまで、デリゲート、Func、Lambda式、無名関数を使用してそれを実行しようとしましたが、機能させることができません(休憩が必要だと思います)。
public Result handler (Func callbackMethodWithArgs) {
Result result = null;
Log.Say(method + " started!");
DoSomeMagic();
try {
result = invoke(callbackMethodWithArgs) as Result;
} catch (Exception) {
Log.Say("Something went terribly wrong");
}
return result;
}
public Result foo (int x, int y, int z) {
return handler(controller.foo(x, y, z));
}
public Result bar (Person a, string whatever) {
return handler(controller.bar(a, whatever);
}
さらに、次のような無名関数を使用できる可能性があると非常に便利です。
public Result foo (int x, int y, int z) {
return handler(delegate () {
controller.foo(x, y, z));
doSomethingOther();
});
}
助言がありますか??ありがとう!(私は同様のトピックに関する多くの質問を読みましたが、私にとって問題を解決するものは何も見つかりませんでした-それでおそらく重複していると思います-もしそうなら、ごめんなさい)