How to check assignability of types at runtime in C#?

后端 未结 6 1203
后悔当初
后悔当初 2021-02-06 02:11

The Type class has a method IsAssignableFrom() that almost works. Unfortunately it only returns true if the two types are the same or the first is in

6条回答
  •  梦谈多话
    2021-02-06 02:15

    In order to find out if one type can be assigned to another, you have to look for implicit conversions from one to the other. You can do this with reflection.

    As Timwi said, you will also have to know some built-in rules, but those can be hard-coded.

提交回复
热议问题