Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public abstract class View
- {
- }
- public abstract class ViewModel<T>
- where T : View
- {
- }
- public class TestView : View
- {
- }
- public class TestViewModel : ViewModel<TestView>
- {
- }
- public class SomeClassWithLogic
- {
- public ViewModel<T> GetView<T>()
- where T : View
- {
- // Here get all types from assembly.
- var assemblyTypes = new List<Type> {typeof(TestViewModel)};
- var viewModel = assemblyTypes
- .Where(t => t.GetTypeInfo().IsSubclassOf(typeof(ViewModel<>)))
- .First(t => t.GetGenericArguments().Contains(typeof(TestView)));
- return (ViewModel<T>) Activator.CreateInstance(viewModel);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement