IEnumerable to typed Dictionary Scenario

A portable controls/widgets library for Xamarin.Forms (PCL Profile 111) contains a combobox/dropdown/picker control where the ItemsSource property is an IEnumerable.


The ItemsSource can be populated with any IEnumerable of objects.
The combobox displays value by


DisplayMemberPath: string

The combobox syncs selection:  


SelectedItem : object
SelectedIndex: int



To do all this the IEnumerable must be parsed so we can


Select index
Select item from source collection


The tricky part

Parse IEnumerable to Dictionary&lt;object, object&gt; in a Portable Class Library.
The sourceDictionary field is used to sync the "selected" properties as described above.

    private Dictionary&lt;object, object&gt; sourceDictionary;

    /// &lt;summary&gt;
    ///     Parse type of ItemsSource as Dictionary and populate &lt;see cref="Picker.Items" /&gt; and
    ///     &lt;see cref="sourceDictionary" /&gt;.
    /// &lt;/summary&gt;
    private void PopulateItemsAndSelectionValuesFromDictionary()
    {
        // TODO: Better way to get Dictionary from IEnumerable
        if (this.ItemsSource is Dictionary&lt;object, object&gt;)
        {
            foreach (var newItem in (Dictionary&lt;object, object&gt;)this.ItemsSource)
            {
                this.AddToItemsAndSelectionValues(newItem.Key, newItem.Value);
            }
        }
        else if (this.ItemsSource is Dictionary&lt;object, string&gt;)
        {
            foreach (var newItem in (Dictionary&lt;object, string&gt;)this.ItemsSource)
            {
                this.AddToItemsAndSelectionValues(newItem.Key, newItem.Value);
            }
        }
        else if (this.ItemsSource is Dictionary&lt;object, int&gt;)
        {
            foreach (var newItem in (Dictionary&lt;object, int&gt;)this.ItemsSource)
            {
                this.AddToItemsAndSelectionValues(newItem.Key, newItem.Value);
            }
        }
        else if (this.ItemsSource is Dictionary&lt;int, object&gt;)
        {
            foreach (var newItem in (Dictionary&lt;int, object&gt;)this.ItemsSource)
            {
                this.AddToItemsAndSelectionValues(newItem.Key, newItem.Value);
            }
        }
        else if (this.ItemsSource is Dictionary&lt;string, object&gt;)
        {
            foreach (var newItem in (Dictionary&lt;string, object&gt;)this.ItemsSource)
            {
                this.AddToItemsAndSelectionValues(newItem.Key, newItem.Value);
            }
        }
        else if (this.ItemsSource is Dictionary&lt;int, int&gt;)
        {
            foreach (var newItem in (Dictionary&lt;int, int&gt;)this.ItemsSource)
            {
                this.AddToItemsAndSelectionValues(newItem.Key, newItem.Value);
            }
        }
        else if (this.ItemsSource is Dictionary&lt;string, string&gt;)
        {
            foreach (var newItem in (Dictionary&lt;string, string&gt;)this.ItemsSource)
            {
                this.AddToItemsAndSelectionValues(newItem.Key, newItem.Value);
            }
        }
    }

    /// &lt;summary&gt;
    ///     Add the provided KeyValuePair to &lt;see cref="Picker.Items" /&gt; and &lt;see cref="sourceDictionary" /&gt;
    /// &lt;/summary&gt;
    private void AddToItemsAndSelectionValues(object key, object value)
    {
        this.Items.Add(this.ValueIsDisplayMember() ? value.ToString() : key.ToString());
        this.sourceDictionary.Add(this.ValueIsDisplayMember() ? key : value, new KeyValuePair&lt;object, object&gt;(key, value));
    }
If you know that ItemsSource is a dictionary then you can simply cast it into IDictionary and iterate it because you just need objects and it gives you exacly that:

var items = (IDictionary)ItemsSource;
foreach (var key in items.Keys)
{
    AddToItemsAndSelectionValues(key, items[key]);
}


You cast it into a concrete dictionary but you never use the types anyway so go directly with objects instead.

That's all. You don't need any of those ifs.