ScenarioA 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 byDisplayMemberPath: stringThe combobox syncs selection:  SelectedItem : objectSelectedIndex: intTo do all this the IEnumerable must be parsed so we canSelect indexSelect item from source collectionThe tricky partParse IEnumerable to Dictionary<object, object> in a Portable Class Library.The sourceDictionary field is used to sync the selected properties as described above.    private Dictionary<object, object> sourceDictionary;    /// <summary>    ///     Parse type of ItemsSource as Dictionary and populate <see cref=Picker.Items /> and    ///     <see cref=sourceDictionary />.    /// </summary>    private void PopulateItemsAndSelectionValuesFromDictionary()    {        // TODO: Better way to get Dictionary from IEnumerable        if (this.ItemsSource is Dictionary<object, object>)        {            foreach (var newItem in (Dictionary<object, object>)this.ItemsSource)            {                this.AddToItemsAndSelectionValues(newItem.Key, newItem.Value);            }        }        else if (this.ItemsSource is Dictionary<object, string>)        {            foreach (var newItem in (Dictionary<object, string>)this.ItemsSource)            {                this.AddToItemsAndSelectionValues(newItem.Key, newItem.Value);            }        }        else if (this.ItemsSource is Dictionary<object, int>)        {            foreach (var newItem in (Dictionary<object, int>)this.ItemsSource)            {                this.AddToItemsAndSelectionValues(newItem.Key, newItem.Value);            }        }        else if (this.ItemsSource is Dictionary<int, object>)        {            foreach (var newItem in (Dictionary<int, object>)this.ItemsSource)            {                this.AddToItemsAndSelectionValues(newItem.Key, newItem.Value);            }        }        else if (this.ItemsSource is Dictionary<string, object>)        {            foreach (var newItem in (Dictionary<string, object>)this.ItemsSource)            {                this.AddToItemsAndSelectionValues(newItem.Key, newItem.Value);            }        }        else if (this.ItemsSource is Dictionary<int, int>)        {            foreach (var newItem in (Dictionary<int, int>)this.ItemsSource)            {                this.AddToItemsAndSelectionValues(newItem.Key, newItem.Value);            }        }        else if (this.ItemsSource is Dictionary<string, string>)        {            foreach (var newItem in (Dictionary<string, string>)this.ItemsSource)            {                this.AddToItemsAndSelectionValues(newItem.Key, newItem.Value);            }        }    }    /// <summary>    ///     Add the provided KeyValuePair to <see cref=Picker.Items /> and <see cref=sourceDictionary />    /// </summary>    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<object, object>(key, value));    }
IEnumerable to typed Dictionary
c#;iterator;dictionary;xamarin
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.