ComboBox is able to display a dictionary through a data source, so you can associate text with the data you really want, instead of checking the displayed text. Then in this case we bind it to the type of object:
Dictionary<string,Type> typeMap = new Dictionary<string,Type> { { "Person", typeof(Person) }, { "Student", typeof(Student) }, { "Grade", typeof(Grade) }, }
then bind it to the ComboBox as:
cbTables.DataSource = new BindingSource(typeMap, null); cbTables.DisplayMember = "Key"; cbTables.ValueMember = "Value";
then when you need to get the selected object use
Type entityType = (Type) cbTables.SelectedValue; DbSet set = dbContext.Set(entityType);
However, after that you need to check the entityType and display the form accordingly. If your form needs a list, for example. List then use
List<Student> studentList = set.Cast<Student>.ToList();
source share