Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // base class for all object
- public class TrainingClass
- {
- public double NetSurface { get; set; } = 0;
- }
- // one sample object
- public class Blank : TrainingClass
- {
- public double Height { get; set; } = 0;
- public double Width { get; set; } = 0;
- }
- // the handling class
- public class RegModel<T> where T : TrainingClass
- {
- // empty constructor
- public RegModel() { }
- // 2 methods that use data of type T
- public void Train(List<T> datas) {}
- public void Solve(T data) {}
- }
- // create the data type the generic need to be of
- Type dataType = typeof(Blank);
- // create a dummy list of data
- var datas = new List<TrainingClass>();
- // create the generic base type
- Type genericClass = typeof(RegModel<>);
- // create the generic type
- Type constructedClass = genericClass.MakeGenericType(dataType);
- // create the class
- var rm = Activator.CreateInstance(constructedClass);
- // get the train method
- var trainMethod = constructedClass.GetMethod("Train");
- // invoke the train method passing the List<TrainingData>
- trainMethod.Invoke(rm, new[] { datas });
- var trainMethodGeneric = trainMethod.MakeGenericMethod(typeof(Blank));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement