내 소스 코드를 마르고 싶습니다. 이 기능 중 일부가 주로 한 줄로 다른 기능을 가지고 있다고 가정 해 봅시다. 어떻게 구현할 수 있습니까?다양한 매개 변수가있는 델리게이트 사용
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, 람다 식, 익명 함수를 사용하여 작업을 시도했지만 작동하도록 가져올 수 없습니다 (휴식 필요).
public Result foo (int x, int y, int z) {
return handler(delegate() {
controller.foo(x, y, z));
doSomethingOther();
});
}
어떤 제안이 같은이 익명의 기능을 사용 할 수있을 정말 좋은 것 첨가로
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);
}
, ?? 감사! (비슷한 주제에 대해 많은 질문을 읽었지 만 그 문제를 해결할만한 것을 찾지 못했습니다. 아마도 중복이라고 생각합니다. 그렇다면 미안 해요)
나는 당신의 방법을 좋아한다. – ykatchou
어떤 .NET 버전을 사용하고 계십니까? – yamen
.NET 4를 사용하고 있습니다 –