简体   繁体   中英

Comparing the run-time type of a generic type

I want to get all the ObjectSet's that start with, say, the word "Foo". I've written the code below but it does not step into the if construct.

foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(context))
            {
                if (prop.PropertyType == typeof(ObjectSet<>))
                {
                   // It doesn't step here even though 
                   // prop.PropertyType is an ObjectSet`1...

Please help.

I suspect prop.PropertyType is actually ObjectSet<X> for some X . You probably want something like:

if (prop.PropertyType.IsGenericType &&
    prop.PropertyType.GetGenericTypeDefinition() == typeof(ObjectSet<>))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM