I have a class that creates a List<Action<int>> and holds onto them until a later time. This class can add or remove delegates from this list. It works well until people are too fancy. To combat the anonymous function (which cannot be removed) I check that the delegate target is zero. If its value is null, I throw an exception. The problem occurs when there is an anonymous delegate containing a function. This has a purpose, but the same cannot be eliminated. The simplified code below illustrates my concerns.
public class MyDelegateContainer { List<Action<int>> m_Container = new List<Action<int>>(); public void Add(Action<int> del) { if (del.Target == null) { throw new Exception("No static handlers"); } m_Container.Add(del); } public bool Remove(Action<int> del) { if (m_Container.Contains(del)) { m_Container.Remove(del); return true; } return false; } } public class MyFakeActionClass { public void Test(int temp) { } } class Program { static void Main(string[] args) { bool removed = false; int counter = 0; MyDelegateContainer container = new MyDelegateContainer(); MyFakeActionClass fake = new MyFakeActionClass();
I need to determine a way
p => { fake.Test(p); counter++; }
is an anonymous function, so I can quit if someone tries it. Thanks for any help
EDIT: I should note that I could use the Action<int> variable for an anonymous function, and everything will work, but Add and Remove will never be in the same area in practice.
Steve source share