.Net/.Net
[.Net] Func 및 Action 제네릭 대리자에 가변성 사용(C# 및 Visual Basic)
뚱2
2012. 4. 10. 00:31
링크 : http://msdn.microsoft.com/ko-kr/library/dd465122.aspx
링크 : http://msdn.microsoft.com/ko-kr/library/018hxwa8.aspx
이렇게도 사용할수 있군요. 간단한 메소드는 델리게이트 선언하지 않고 바로 사용 할 수 있을듯합니다.
응용 : http://www.devpia.co.kr/MAEUL/Contents/Detail.aspx?BoardID=18&MAEULNO=8&no=1723&page=11
범용 적으로 사용 하게 만든 클래스 사실 제네릭 인자 3개 더 추가밖에 없습니다.
public static class ControlExt { public static void InvokeIfNeeded(this Control control, Action action) { if ( control.InvokeRequired ) { control.Invoke(action); } else { action(); } } public static void InvokeIfNeeded< T >(this Control control, Action< T > action, T arg) { if ( control.InvokeRequired ) { control.Invoke(action, arg); } else { action(arg); } } public static void InvokeIfNeeded< T1 , T2 >(this Control control, Action< T1 , T2 > action, T1 arg1, T2 arg2) { if ( control.InvokeRequired ) { control.Invoke(action, new object[] {arg1, arg2}); } else { action(arg1, arg2); } } public static void InvokeIfNeeded< T1, T2, T3 >(this Control control, Action< T1, T2, T3 > action, T1 arg1, T2 arg2, T3 arg3) { if ( control.InvokeRequired ) { control.Invoke(action, new object[] { arg1, arg2, arg3 }); } else { action(arg1, arg2, arg3); } } public static void InvokeIfNeeded< T1, T2, T3, T4 >(this Control control, Action< T1, T2, T3, T4 > action, T1 arg1, T2 arg2, T3 arg3, T4 arg4) { if ( control.InvokeRequired ) { control.Invoke(action, new object[] { arg1, arg2, arg3, arg4 }); } else { action(arg1, arg2, arg3, arg4); } } }