Advertisement
Guest User

Some generic shit.

a guest
Apr 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.67 KB | None | 0 0
  1. public abstract class View
  2. {
  3. }
  4.  
  5. public abstract class ViewModel<T>
  6.     where T : View
  7. {
  8. }
  9.  
  10. public class TestView : View
  11. {
  12. }
  13.  
  14. public class TestViewModel : ViewModel<TestView>
  15. {
  16. }
  17.  
  18. public class SomeClassWithLogic
  19. {
  20.     public ViewModel<T> GetView<T>()
  21.         where T : View
  22.     {
  23.         // Here get all types from assembly.
  24.         var assemblyTypes = new List<Type> {typeof(TestViewModel)};
  25.  
  26.         var viewModel = assemblyTypes
  27.             .Where(t => t.GetTypeInfo().IsSubclassOf(typeof(ViewModel<>)))
  28.             .First(t => t.GetGenericArguments().Contains(typeof(TestView)));
  29.  
  30.         return (ViewModel<T>) Activator.CreateInstance(viewModel);
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement