C # controladores genéricos, ¿qué estoy malentendido?
No estoy seguro de por qué esto no funciona. ¿No le gusta TResponse para el controlador out y handlerMap, aunque TResponse es un IResponse? Me imagino que debo estar malinterpretando algo sobre los genéricos, o tal vez más, sobre C #. ¿Por qué no funciona esto y hay una mejor manera de lograr lo que estoy tratando de hacer aquí?
private static Dictionary<Type, List<IResponseHandler<IResponse>>> _handlerMap;
public static void AddResponseHandler<TResponse>(IResponseHandler<TResponse> handler) where TResponse : IResponse
{
List<IResponseHandler<TResponse>> handlers;
_handlerMap.TryGetValue(typeof (TResponse), out handlers);
if (handlers == null)
{
handlers = new List<IResponseHandler<TResponse>>();
_handlerMap.Add(typeof (TResponse), handlers);
}
handlers.Add(handler);
}
public interface IResponseHandler<TResponse> where TResponse : IResponse
{
void Handle(TResponse response);
}
Estoy recibiendo estos errores durante la compilación:
Error 1 La mejor coincidencia del método sobrecargado para 'System.Collections.Generic.Dictionary >>. TryGe tValue (System.Type, out System.Collections.Generic.List>)' tiene algunos argumentos no válidos C: ... \ NetworkManager. cs 39 13 Asamblea -Carrote-vs
Error 2 Argumento 2: no se puede convertir de 'out System.Collections.Generic.List>' a 'out System.Collections.Generic.List>' C: ... \ NetworkManager.cs 39 61 Assembly-CSharp-vs
If I change TResponse to IResponse within the method, everything above
handlers.Add(handler) compiles fine. I don't understand why I can't add a handler of
<TResponse : IResponse> to a List<IResponseHandler<IReponse>>?