tags:

views:

111

answers:

3

I've got an IDictionary field that I would like to expose via a property of type IDictionary<string, dynamic> the conversion is surprisingly difficult since I have no idea what I can .Cast<>() the IDictionary to.

Best I've got:

IDictionary properties;
protected virtual IDictionary<string, dynamic> Properties {
  get { 
        return _properties.Keys.Cast<string>()
              .ToDictionary(name=>name, name=> _properties[name] as dynamic); 
      }
    }
A: 

You need to use a KeyValuePair.

myDictionary.Cast<KeyValuePair<Type1, Type2>>()
Scott Bilas
Ach, I answered too quickly.Cast will return an enumerable. If you want it to be an IDictionary then you can't really avoid the new dictionary creation, at least given the extension methods I'm familiar with.Could go myDictionary.Cast<KeyValuePair<Type1, Type2>>().ToDictionary(kv => kv.Key, kv => kv.Value) to make that easier at least..Though of course you wouldn't want to put that on a property getter, given the work it does.Better solution would be to implement IDictionary<K,V> with a class that takes an IDictionary reference and casts where necessary.
Scott Bilas
+4  A: 

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:

  1. Copy the IDictionary to your generic type.
  2. 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.

Randolpho
+1  A: 

Unless the backing type for the IDictionary instance already implements IDictionary<string,dynamic> (like Dictionary<string,dynamic>) then casting won't help you. The Cast<>() method is only useful for returning IEnumerable<T> values and normal casting isn't an option.

If providing the data in the form of IDictionary<string,dynamic> is important, then why not go ahead and store it as a Dictionary<string,dynamic> from the start?

JaredPar
cause nhibernate dynamic components uses IDictionary
George Mauer
@George Mauer: then you're going to have to either copy or translate. You cannot cast.
Randolpho
Yeah, I guess cast is too strong a term - I'm looking for the best way to translate.
George Mauer