Generic method to type casting

前端 未结 8 1120
不思量自难忘°
不思量自难忘° 2021-02-02 11:55

I\'m trying to write generic method to cast types. I want write something like Cast.To(variable) instead of (Type) variable. My wrong versi

8条回答
  •  梦如初夏
    2021-02-02 12:20

    Maybe not what you what to do exactly, but that will works:

    public class Cast
    {
        public static targetType To(object o)
        {
            return (targetType)((sourceType) o);
        }
    }
    

    But well, such a method seems useless to me...

提交回复
热议问题