mardi 17 avril 2018

IDictionary`2 not assignable from Dictionary`2

I want to determine wether a given Type object refers to a generic dictionary. To achieve this I tried the solution from this answer. But to my surprise, the following test code prints not a generic dictionary!

private void Test()
{
    var dict = new Dictionary<string, string>();
    var type = dict.GetType();

    if (type.IsGenericType &&
            typeof(IDictionary<,>).IsAssignableFrom(type.GetGenericTypeDefinition()))
        Console.WriteLine("a generic dictionary");
    else
        Console.WriteLine("not a generic dictionary!");
}

How can it be that IDictionary`2 is not assignable from Dictionary`2 or do I something fundamentally wrong?

In case it matters, I'm doing this in Visual Studio 2015 with .NET 4.6 as target.





Aucun commentaire:

Enregistrer un commentaire