vendredi 27 octobre 2023

Get the type name of a Dictionary with generics in C#

Is there an easy or elegant way to get the type name of a Dictionary with the generic types in C#?

Currently I have this code works:

if (property.PropertyType.Name == "Dictionary`2")
{
    Type[] arguments = property.PropertyType.GetGenericArguments();
    Type keyType = arguments[0];
    Type valueType = arguments[1];
    properties[property.Name] = $"Dictionary<{keyType.Name},{valueType.Name}>";
}

I was wondering whether there is more easy way of achieving this formatted string that I'm looking for, like: "Dictionary<type, type>"





Aucun commentaire:

Enregistrer un commentaire