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

后端 未结 6 1185
后悔当初
后悔当初 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:23

    What you are looking for is if there's an implicit cast from the one type to the other. I would think that's doable by reflection, though it might be tricky because the implicit cast should be defined as an operator overload which is a static method and I think it could be defined in any class, not just the one that can be implicitly converted.

提交回复
热议问题