I have a web page where a user can drag and drop certain widgets into a single placeholder. Each widget has its own set of properties.
For example, suppose I have the following HTML widgets:
Widget1:
- Title field
- Technical name field
And another widget that is more dynamic:
Widget2:
- Range picker field 1 (0 to 100)
- Range picker field 2 (0 to 100)
------------------
| (+) Add picker |
------------------
(can add as many range pickers as needed)
Now with the click of a button these settings are saved to the database. But at some point I also need to read that data from it again.
For example, suppose I have the following classes for the Widgets:
interface IWidget { }
class Widget1 : IWidget
{
public string Title {get;set;}
public string TechnicalName {get;set;}
}
class Widget2 : IWidget
{
public List<int> PickerValues {get;set;}
}
Then store that in a List
.
var widgets = new List<IWidget>();
widgets.Add(new Widget1());
widgets.Add(new Widget2());
That would work. But then getting an object from that list again is a problem:
var widget = widgets.First();
widget.????? // <-- NO INTELLISENSE!
That means it doesn't know what actual implementation it has. I'd have to cast it. But how, if I don't know what widgets are stored in there? It can be a list of 1, 5 or even 10 different widgets in any order.
How can I solve this problem?