If the underlying type of the IDictionary
does not implement IDictionary<string, dynamic>
, you cannot cast the object, period. If it does, a simple cast via (IDictionary<string, dynamic>)localVar
will suffice.
If it's not, there are two things you can do:
- Copy the
IDictionary
to your generic type.
- Build an Adapter class that accepts the
IDictionary
as a dependency and implements the generic IDictionary
you want, mapping calls from one to the other.
Edit: The sample code you've just posted will copy the dictionary every time it gets called! I will edit again in a moment with some suggested code.
Option 1
your sample code approach is solid as a means of copying the data, but the copy should be cached or you're going to copy lots of times. I'd suggest you put the actual translation code into a separate method and call that from your property the first time it's used. For example:
private IDictionary dataLayerProperties;
private IDictionary<string, dynamic> translatedProperties = null;
protected virtual IDictionary<string, dynamic> Properties
{
if(translatedProperties == null)
{
translatedProperties = TranslateDictionary(dataLayerProperties);
}
return translatedProperties;
}
public IDictionary<string, dynamic> TranslateDictionary(IDictionary values)
{
return values.Keys.Cast<string>().ToDictionary(key=>key, key => values[key] as dynamic);
}
Now, there are obvious cons to this approach... what if dataLayerProperties needs to be refreshed? You have to go setting translatedProperties to null again, etc.
Option 2
This is my preferred approach.
public class TranslatedDictionary : IDictionary<string, dynamic>
{
private IDictionary Original = null;
public TranslatedDictionary(IDictionary original)
{
Original = original;
}
public ICollection<string> Keys
{
get
{
return Original.Keys.Cast<string>().ToList();
}
}
public dynamic this[string key]
{
get
{
return Original[key] as dynamic;
}
set
{
Original[key] = value;
}
}
// and so forth, for each method of IDictionary<string, dynamic>
}
//elsewhere, using your original property and field names:
Properties = new TranslatedDictionary(properties);
Now, there are obvious cons to this approach as well, the most glaring is the fact that the Keys
(and Value and anything else that returns ICollection
on IDictionary
has to return a new array for every call. But this still allows the most flexible approach, since it ensures the data is always up to date.