❔ Type is Typeof Dictionary
I need to test if a System.Type
t
is a Dictionary of any kind, but when I do this check, it doesn't pass it. How do I test for this?
I did a log of the type.tostring after and it said so I believe it is indeed a dictionary. Shouldn't it have passed the check?12 Replies
Ero#1111
REPL Result: Success
Result: bool
Compile: 470.383ms | Execution: 30.074ms | React with ❌ to remove this embed.
Weird
typeof(Dictionary<,>) is not a pattern match type. It is the generic type definition/open generic type.
You cannot create something with that type
So a real dictionary will never have the same type as an open generic type
What you were comparing it to is an instance of this generic type. The solution is to ask for the generic definition, like BadaBingBadaBoom showed
you can as well check the interface if it's IDictionary or not with
Type.IsAssignableFrom(Type)
depends on what you wantType.IsAssignableFrom(Type) Method (System)
Determines whether an instance of a specified type c can be assigned to a variable of the current type.
that worked, thank you
Can I use this for a Func<> delegate?
sure
you just need the right amount of commas
Windows10CE#8553
REPL Result: Success
Result: bool
Compile: 533.683ms | Execution: 59.212ms | React with ❌ to remove this embed.
I eventually figured it out, thanks. boy there's a lot of repetition
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.