public interface IProcessor<T>
{
void Process(T instance);
}
foreach(AbstractType instance in myClass.SomeCollection)
OnProcess(instance);
public void OnProcess<T>(T instance)
{
IProcessor<T> processor =
unityContainer.Resolve<IProcessor<T>>();
processor.Process(instance);
}
The problem with this code is that the in
OnProcess
is always AbstractType
, and not the concrete type of the instance being passed. I currently see two possibilities.
01: Create a non generic IProcessor
and use it as the base for IProcessor
. Any implementor will have to implement both generic and non-generic Process
methods, typically typecasting and passing onto the generic method.
02: Use Type.MakeGenericType
to get the IProcessor
, resolve that, and then use reflection to invoke the Process method.
Both of these approaches feel a bit "unclean". Can anyone think of a way I can do this without having to resort to these practices?
Thanks
Pete