me puede llamar Get<int>(Stat);
o Get<string>(Name);
No se puede convertir implícitamente el tipo 'int' a 'T'
Pero al compilar me sale:
No se puede convertir implícitamente el tipo 'int' a 'T'
y lo mismo para string
.
public T Get<T>(Stats type) where T : IConvertible
{
if (typeof(T) == typeof(int))
{
int t = Convert.ToInt16(PlayerStats[type]);
return t;
}
if (typeof(T) == typeof(string))
{
string t = PlayerStats[type].ToString();
return t;
}
}
Probablemente esté pensando que el bloque if verificó que T es int, por lo que dentro del bloque, sabe que T es int y debe poder convertir imp ínticamente a T. Pero el compilador no está diseñado para seguir ese razonamiento, simplemente sabe que generalmente T no deriva de int, por lo que no permite la conversión implícita. (Y si el compilador lo soportaba, el verificador no lo haría, por lo que el ensamblado compilado no podría verificarse). – JGWeissman