Implementing the MVVM pattern in C#: How can I create a ViewModel instance, given a Model object whose exact type is only known at runtime?
I have an abstract ModelBase class and an abstract ViewModelBase class as well as several derived classes, e.g. FirstModel : ModelBase, SecondModel : ModelBase and so on, as well as FirstViewModel : ViewModelBase, SecondViewModel : ViewModelBase etc.
Now I want to implement a function that creates the appropriate view model for a given model object. Something along these lines:
ViewModelBase CreateViewModel(ModelBase someObject)
{
return new ViewModelBase(someObject);
}
The above code does not work of course, because ViewModelBase is abstract. I rather want to create a new FirstViewModel, SecondViewModel and so on depending on the type of someObject which is only known at runtime.
How would I achieve this?