[英]How to limit the amount of method for a delegate?
我有一位delegate
,我只希望可以注冊5次。 我知道我可以將delegate
設置為private
並編寫AddRegister()
方法來控制已registered
方法的數量。 還有其他想法嗎?
您可以在委托對象上使用GetInvocationList
方法,然后在其上調用Count
方法。 有以下影響的東西
Mydel d1 = this.MyMethod;
int count = d1.GetInvocationList().Count();//Here count will be 1
另外,我希望將注冊與上述功能結合在一個位置,類似於您在問題中提到的位置。 這樣,無論在什么地方注冊,都不必寫count <= 5
條件。 因此,它將更易於維護。
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.