简体   繁体   中英

Find the type of the Object, probably System.Collections.Specialized.StringCollection

I am not sure why the condition below is not getting fulfilled.

 else if (obj.GetType() == typeof(Dictionary<string, System.Collections.Specialized.StringCollection>))

Obj is an object of type Dictionary<string, System.Collections.Specialized.StringCollection>)

When I am executing the above code, it does not enter inside the else if condition.

Any pointers?

I would switch it to using the is keyword, so

else if (obj is Dictionary<string, System.Collections.Specialized.StringCollection>)

which will match if obj has that type somewhere in its ancestry. If you try to equal only on the exact types it may not work.

If that still doesn't work, then I'd put the types into variables and check them in the debugger to see what's really going on.

Tested just now.

object obj = new Dictionary<string, System.Collections.Specialized.StringCollection>();
        if (obj.GetType() == typeof(Dictionary<string, System.Collections.Specialized.StringCollection>))
            System.Diagnostics.Debug.Assert(false);

Its all right. Try to write

Type objType = obj.GetType();

and see what you get.

无需使用"=="进行比较,您可以将它们与is关键字, IsSubclassOf方法和Equals()进行比较,在这种情况下,任何一种都可以使用。

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