0

It's understood that using a polymorphic set of subclasses is, in most cases, preferable to using enum and switch. The part I'm having trouble with is populating a ListBox or ComboBox with the available types. Binding a listbox to an enum is relatively straight-forward, but how do you do the same thing while avoiding enums altogether?

More specifically, if I have

class MyBase
class A : MyBase
class B : MyBase
class C : MyBase

How do I make present a listbox on the UI that contains a textual description of each class... something like

This is Class A
This is Class B
This is Class C

and have an instance of class B created after the user selects the 2nd item in the list?

Thanks

BCA
  • 7,776
  • 3
  • 38
  • 53

2 Answers2

4

You can use Reflection to get types that inherits from MyBase:

var baseType =  typeof(MyBase);
var types = Assembly.GetExecutingAssembly()
                    .GetTypes()
                    .Where(x => baseType.IsAssignableFrom(x));

Then the rest is easy, for example you can have a list of key value pairs where Key is the class type and the Value is the description of the type, and set the ValueMember to Key, DisplayMember to Value:

Assembly.GetExecutingAssembly()
        .GetTypes()
        .Where(x => typeof(MyBase).IsAssignableFrom(x));
        .Select(x => new KeyValuePair<Type, string>(x,"This is " + x.Name))
        .ToList();

Then handle the SelectedChanged event, and get the SelectedValue, cast it to Type, use Activator.CreateInstance to create an instance.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • 2
    If you go this route you may want to put this code in a static method of the base class and cache the results there for future calls. Enumerating all the types declared in an assembly isn't something you want to do often. – Bradley Uffner Mar 11 '15 at 19:35
0

I would override the ToString method of the class instance. Your listbox should display the ToString result.

So set the ItemsSource of your ListBox to an ObservableCollection of your class instances and rely on the ToString to display the value in the listBox.

Scott Nimrod
  • 11,206
  • 11
  • 54
  • 118
  • but the OP doesnt have a list of instance of those clasess. he wants all classes that derives from MyBase. and create an instance of the Selected type. Have you read the question? – Selman Genç Mar 11 '15 at 17:14
  • He does. He's relying on the Stratagy Pattern. So he has a base class or interface that his code is pointing to but a specific implementation to get executed. Did you read his question? – Scott Nimrod Mar 11 '15 at 17:18
  • "How do I make present a listbox on the UI that contains a textual description of each class... " see, he says _class_, not _instance_. and wants to create an instance of selected type`. if he already has the instances, why would he want to create a new instance of selected type? your answer assumes he does have instances, but there is no evidence that he does, except your claiming... – Selman Genç Mar 11 '15 at 17:22