الجمعة، 20 ديسمبر 2013

typeof and a Base class

Yes, but it's going to be a little bit more complicated than your example. A quick example:

typeof(int?).IsAssignableFrom(typeof(int))

The IsAssignableFrom method will allow you to check if there's an implicit cast between the two types - in the case of an inherited class, this is a given. So you can say

typeof(Base).IsAssignableFrom(type)

However, as you can see, this means you can't use a Dictionary for the types anymore - you have to check every single possibility separately, and in the proper order. The easiest way would be to treat some types as simple (Dictionary lookup) and some as supporting inheritance (List of base types).


View the original article here

ليست هناك تعليقات:

إرسال تعليق