in Education by
Is there a way to test if an object is a dictionary? In a method I'm trying to get a value from a selected item in a list box. In some circumstances, the list box might be bound to a dictionary, but this isn't known at compile time. I would like to do something similar to this: if (listBox.ItemsSource is Dictionary<??>) { KeyValuePair<??> pair = (KeyValuePair<??>)listBox.SelectedItem; object value = pair.Value; } Is there a way to do this dynamically at runtime using reflection? I know it's possible to use reflection with generic types and determine the key/value parameters, but I'm not sure if there's a way to do the rest after those values are retrieved. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Check to see if it implements IDictionary. See the definition of System.Collections.IDictionary to see what that gives you. if (listBox.ItemsSource is IDictionary) { DictionaryEntry pair = (DictionaryEntry)listBox.SelectedItem; object value = pair.Value; } EDIT: Alternative when I realized KeyValuePair's aren't castable to DictionaryEntry if (listBox.DataSource is IDictionary) { listBox.ValueMember = "Value"; object value = listBox.SelectedValue; listBox.ValueMember = ""; //If you need it to generally be empty. } This solution uses reflection, but in this case you don't have to do the grunt work, ListBox does it for you. Also if you generally have dictionaries as data sources you may be able to avoid reseting ValueMember all of the time.

Related questions

0 votes
    Is there a way to test if an object is a dictionary? In a method I'm trying to get a value ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    Is there a way to test if an object is a dictionary? In a method I'm trying to get a value ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    Is there a way to test if an object is a dictionary? In a method I'm trying to get a value ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    Why is Dictionary preferred over Hashtable in C#?...
asked Jan 16, 2021 in Technology by JackTerrance
0 votes
    How can I check if a key exists in a directory in Python, before updating the value of the key. I tried this ... better way to do this? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
0 votes
    How can I delete an element from a dictionary? In order to return a copy, how can I delete an element/ ... modifying the original)? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    Which of the following is not an advantage of dictionary attack? (a) Very fast (b) Time-saving (c) ... ,Need-for-Cyber Security:,Cyber Security-Jobs:,Cyber Security Applications...
asked Nov 1, 2021 in Education by JackTerrance
0 votes
    What can’t we put into an array or dictionary?...
asked Nov 10, 2020 in Technology by JackTerrance
0 votes
    In python3 console, input those: >>> import sys >>> sys.version_info sys.version_info(major=3, minor= ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    In python3 console, input those: >>> import sys >>> sys.version_info sys.version_info(major=3, minor= ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    In python3 console, input those: >>> import sys >>> sys.version_info sys.version_info(major=3, minor= ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    The data are kept in a list whose elements are dictionaries, where each dictionary contains the data for a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I'm having trouble with understanding how to properly do unwrapping in Swift. Here is the situation: I have ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have a dictionary d full of a collection of dataframes: key type size value gm1 dataframe mxn .. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
...