Creating List<Type> from type name

A

Andrus

I use switch statement to create Generic list for combobox:

using EntityExtension;

public static class ComboPickList {
public static object GetObjectPickList(string entityClassName) {

switch (entityClassName) {
case "Customer":
return new List<Customer>();

case "Item":
return new List<Item>();

....
}}}


All entity classes are defined in same assembly in EntityExtension
namespace.
there are approx 40 entity classes so switch is long.
More importantly, some entityClassName 's are not known at design time
because may be defined by user at runtime.

How to refactor this method code so it allows to use any entityClassName
without adding it to case label ?
Can I use MakeGenericType() or reflection ?

Andrus.
 
J

Jon Skeet [C# MVP]

How to refactor this method code so it allows to use any entityClassName
without adding it to case label ?
Can I use MakeGenericType() or reflection ?

Yes, you need to use MakeGenericType - but that's not "or" reflection
- that *is* reflection.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top