-1

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;
}
10
  • Have you asked yourself why you would want this? Your code does not seem dynamic at all, what is wrong with new School<Student>();? Commented Oct 11, 2017 at 15:25
  • 1
    Also, if Teacher and Student have the same properties (or share some number of properties) it should be abstracted into a base class or an interface (like Person or something). That way you can restrict your School<T> to only allow the common abstraction Commented Oct 11, 2017 at 15:26
  • Check this stackoverflow.com/questions/2194949/… Commented Oct 11, 2017 at 15:27
  • I have written this code just to demonstrate the problem. In real time I won't be aware of which Class it would be using. I would just have Class in my project. The Type of list selection needs to be dynamic and passed at the runtime. Commented Oct 11, 2017 at 15:28
  • School<T> means each school object can only have one type of list (that type being T, which you provide when using new). 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. Commented Oct 11, 2017 at 15:29

1 Answer 1

0

If I understand your question correctly and you are not forced to use generics, I would recommend the following solution.

Since a school contains students and teachers you can implement an interface for both types.

interface IPerson
{
    public int Id;
    public string Name;
}

class Student : IPerson
{
    // student specific stuff
}

class Teacher : IPerson
{
    // teacher specific stuff
}

So your school looks like this:

public class School
{
    public int Id;
    public string Name;
    private List<Person> _persons;

    public School()
    {
        _persons = new List<Person>();
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

The problem would this approach would that the Dynamic Class won't be implementing the interface. They can be anything.
Maybe try to find a better example or description of your problem.
I have just edit the code. Hope it explains my problem a bit better. Thank you for your answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.