jeudi 15 novembre 2018

getting a IDictionary item value setter through reflection

I'm trying to get the setter function of a dictionary item value. I know the object is a Dictionary< TKey,TValue>, but I don't know the types of Tkey and TValue, so I think my only resort is to use an IDictionary.

Unfortunately the IDictionary has no indexer and I'm not sure how to get to the actual keys and values.

Right now I'm looping through the dictionary entries and get the setter from that, but whenever I call the setter it doesn't seem to change to value in the dictionary. So I suspect the DictionaryEntry is a copy and doesn't point to the actual value in the dictionary.

//don't know the TKey and TValue, so have to use IDictionary
IDictionary target;

foreach ( DictionaryEntry dictionaryEntry in dictionary )
{
    PropertyInfo prop = dictionaryEntry.GetType().GetProperty( "Value" );

    Action<object> setter = ( val ) =>
    {
        //changed the dictionaryEntry object, but the dictionary[key] remains unaltered
        prop.SetMethod.Invoke( target, new object[] { val } );
    };
}

Since I do know that the IDictionary is actually a Dictionary< TKey, TValue> underneath, maybe I can do some reflection magic to get the setter that way?





Aucun commentaire:

Enregistrer un commentaire