CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
public interface ISomeInterface{}
public class SomeInterfaceImplA : ISomeInterface{}
public class SomeInterfaceImplB : ISomeInterface{}
public class SomeClass
{
private IList<Action<ISomeInterface>> _delegates = new List<Action<ISomeInterface>>();
public IEnumerable<Action<ISomeInterface>> Delegates{ get {return _delegates;} }
public void AddDelegate<T>(Action<T> del) where T : ISomeInterface
{
_delegates.Add(si => del((T)si));
}
}
public class Program
{
public static void Main()
{
var someClass = new SomeClass();
Action<SomeInterfaceImplA> someInterfaceImplADelegate = delegate(SomeInterfaceImplA siia){Console.WriteLine("SomeInterfaceImplADelegate");};
Action<SomeInterfaceImplB> someInterfaceImplBDelegate = delegate(SomeInterfaceImplB siib){Console.WriteLine("SomeInterfaceImplBDelegate");};
someClass.AddDelegate(someInterfaceImplADelegate);
someClass.AddDelegate(someInterfaceImplBDelegate);
var aDelegates = someClass.Delegates.Where(d => d is Action<SomeInterfaceImplA>);//What predicate should be used....
foreach(var del in aDelegates)
{
del(null); //...in order to only iterate over delegates with type SomeInterfaceImplA?
}
}
}