I am doing some reflection stuff. I have the following situation:
- I have a variable
Type myType
with some runtime value (e.g.string
orList<int>
). - I have two variables
object a
andobject b
for which I know that they are of typeIImmutableSet<myType>
. (So ifmyType=string
they'd beIImmutableSet<string>
, ifmyType=List<int>
they'd beIImmutableSet<List<int>>
and so on.)
How can I cast them?
Motivation:
I want to do a comparison of a
and b
by content, i,e.
- check that both their size is equal. For that, I need a
Count
property (or equivalent property/method). - check that
a
contains all elements ofb
. For that, I need aContains(...)
method.
If I had ISet<T>
s, I'd just cast them to ICollection
and use that interface's Count
and Contains(...)
.
But since IImmutableSet<T>
does not implement any non-generic collection interface, I need to cast a
and b
to IImmutableSet<myType>
(or IReadOnlyCollection<myType>
). That syntax doesn't work because it expects a compile-time constant type.
--
Another thought... if casting isn't possible, I'd be happy with being able to call the said methods. Since I'm already doing heavy reflection, I don't care about speed.
Aucun commentaire:
Enregistrer un commentaire