I have a class that i need to reflect through an object properties and check values and other things. Everything seems to be working exception when i have a list and try to
The fact that int
is a value type makes the difference here. According to C# spec there must be a reference or identity conversion between generic types to make types variant-convertible:
A type
T<A1, …, An>
is variance-convertible to a typeT<B1, …, Bn>
ifT
is either an interface or a delegate type declared with the variant type parametersT<X1, …, Xn>
, and for each variant type parameterXi
one of the following holds:
Xi
is covariant and an implicit reference or identity conversion exists fromAi
toBi
Xi
is contravariant and an implicit reference or identity conversion exists fromBi
toAi
Xi
is invariant and an identity conversion exists fromAi
toBi
IEnumerable<T>
is covariant, so the line I marked is important. Ai
in your case is int
and Bi
is int
. There is no reference conversion from int
to object
, because int
is not a reference type. There is also no identity conversion between these two, because they are not the same.
I don't know the entire context of your question, but you can use non-generic IEnumerable
instead of IEnumerable<object>
. They are pretty much the same. However, you might be facing an XY problem here, so it's hard to help you without knowing more about your case.