Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. // base class for all object
  2. public class TrainingClass
  3. {
  4. public double NetSurface { get; set; } = 0;
  5. }
  6.  
  7. // one sample object
  8. public class Blank : TrainingClass
  9. {
  10. public double Height { get; set; } = 0;
  11. public double Width { get; set; } = 0;
  12. }
  13.  
  14. // the handling class
  15. public class RegModel<T> where T : TrainingClass
  16. {
  17. // empty constructor
  18. public RegModel() { }
  19.  
  20. // 2 methods that use data of type T
  21. public void Train(List<T> datas) {}
  22. public void Solve(T data) {}
  23. }
  24.  
  25. // create the data type the generic need to be of
  26. Type dataType = typeof(Blank);
  27.  
  28. // create a dummy list of data
  29. var datas = new List<TrainingClass>();
  30.  
  31. // create the generic base type
  32. Type genericClass = typeof(RegModel<>);
  33.  
  34. // create the generic type
  35. Type constructedClass = genericClass.MakeGenericType(dataType);
  36.  
  37. // create the class
  38. var rm = Activator.CreateInstance(constructedClass);
  39.  
  40. // get the train method
  41. var trainMethod = constructedClass.GetMethod("Train");
  42.  
  43. // invoke the train method passing the List<TrainingData>
  44. trainMethod.Invoke(rm, new[] { datas });
  45.  
  46. var trainMethodGeneric = trainMethod.MakeGenericMethod(typeof(Blank));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement