1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//using System;
//using System.Linq;
/// <summary> 델리게이트가 메소드를 갖고 있는지 검사 </summary>
private static bool CheckDelegateHasMethod<DType>(DType @delegate, DType method) where DType : Delegate
{
return @delegate?.GetInvocationList()
.Where(d => d.Method == method.Method)
.Count() > 0;
}
private Action<int> del;
private void MethodA(int i) { }
private void Example()
{
_ = CheckDelegateHasMethod(del, MethodA); // false
del += MethodA;
_ = CheckDelegateHasMethod(del, MethodA); // true
}
C# - 델리게이트가 특정 메소드를 갖고 있는지 확인하기
This post is licensed under
CC BY 4.0
by the author.