lundi 1 juillet 2019

Iterating runtime dictionary

I would like to cast a runtime dictionary to Dictionary<object, object>

Initially I've just tried to cast to Dictionary

@object as Dictionary<object, object> but the resulting collection is a null.

The @object type is Dictionary, I cannot just cast to this explicit type since is a runtime type.

Another way I've tried was to

foreach(var keyValue in @object as System.Collections.IEnumerable)
{
    Type dictionary = typeof(Dictionary<,>); /// => type to construct.

    Type[] typeArgs = keyValue.GetType().GetGenericArguments();

    Type constructed = dictionary.MakeGenericType(typeArgs);

    object construct = Activator.CreateInstance(constructed); /// => value is 0
}

In the end I'm just looking to iterate over each key-value.





Aucun commentaire:

Enregistrer un commentaire