jeudi 14 janvier 2021

Why is checking equality of open generic types inconsistent? [duplicate]

For some reason this:

typeof(Dictionary<string,string>).GetGenericTypeDefinition()
                                 .GetInterfaces()
                                 .Contains(typeof(IDictionary<,>))

is false but this:

typeof(Dictionary<string,string>).GetInterfaces()
                                 .Any(i => i.IsGenericType
                                        && i.GetGenericTypeDefinition() == typeof(IDictionary<,>))

is true, even though they both appear as IDictionary'2 in the debugger with the same GUID.

In the first example, [0] in the interface array is IDictionary'2, and if I compare it directly with typeof (without .Contains(...)) with either == or .Equals(...) and even .IsAssignableFrom(...) it's the same outcome.

Please, someone tell me why!





Aucun commentaire:

Enregistrer un commentaire