Cannot implicitly convert type 'Int' to 'T'

前端 未结 9 2327
甜味超标
甜味超标 2020-11-27 14:15

I can call Get(Stat); or Get(Name);

But when compiling I get:

Cannot implicitly convert typ

9条回答
  •  旧时难觅i
    2020-11-27 14:39

    You can simply cast like below,

    public T Get(Stats type) where T : IConvertible
    {
      if (typeof(T) == typeof(int))
      {
        int t = Convert.ToInt16(PlayerStats[type]);
        return t as T;
      }
     if (typeof(T) == typeof(string))
     {
        string t = PlayerStats[type].ToString();
        return t as T;
     }
    }
    

提交回复
热议问题