I am trying to create a list of object of Student in School. Instead of Student the School can also have Teacher as a list. I want to use the class name as a parameter instead of the actual class type as a parameter while initiating class.
public class Program
{
static void Main(string[] args)
{
//School object with list of student
var schoolWithStudent = SchoolWithOject("Student");
//School object with list of teacher
var schoolWithTeacher = SchoolWithOject("Teacher");
}
public static object SchoolWithOject(string objType)
{
var objType = Type.GetType("objType");
var school = new School<objType>();
return school;
}
}
public class School<T>
{
public int Id;
public string Name;
private List<T> _components;
public School()
{
_components = new List<T>();
}
}
public class Student
{
public int Id;
public string Name;
}
public class Teacher
{
int Id;
string Name;
}
new School<Student>();?Personor something). That way you can restrict yourSchool<T>to only allow the common abstractionSchool<T>means each school object can only have one type of list (that type beingT, which you provide when usingnew). That seems like the wrong abstraction; you can have a school of only students, or a school of only teachers, but not a school with both.