Generic method to type casting

前端 未结 8 1204
不思量自难忘°
不思量自难忘° 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:06

    Your Cast.To() is just trying to interpret reference to given object as reference to T. Which fails of course.

    And if compiler encounters (B) a and knows that a is of type A and type A has compile-time cast operator to type B - it emits this cast. It is not your case.

提交回复
热议问题