C# 如何正确定义要创建的不同对象的数量?

C# 如何正确定义要创建的不同对象的数量?,c#,list,object,C#,List,Object,我有一个包含这两个方法的类和一个列表: public class Example { protected List<Person> ExampleList = new List<Person>(); public Example() { CreatePersonInstances(); foreach (Person personToInitialize in ExampleList) p

我有一个包含这两个方法的类和一个列表:

public class Example
{
    protected List<Person> ExampleList = new List<Person>();

    public Example()
    {
        CreatePersonInstances();

        foreach (Person personToInitialize in ExampleList)
            personToInitialize.Initialize();
    }

    protected void CreatePersonInstances()
    {
        ExampleList.Add(new Employee());
        ExampleList.Add(new Manager());
        ExampleList.Add(new Recruiter());                                                                            
    }
}
公共类示例
{
受保护列表ExampleList=新列表();
公共示例()
{
CreatePersonInstances();
foreach(示例列表中的personToInitialize)
personToInitialize.Initialize();
}
受保护的void CreatePersonInstances()
{
添加(新员工());
Add(newmanager());
添加(新招聘人员());
}
}
如何从用户定义的数量正确创建特定数量的项目


例如,如果用户决定创建两名员工、一名经理和零名招聘人员,我必须能够在
CreatePersonInstaces()中创建定义数量的对象

您可以首先在构造函数中添加3个参数,每个参数表示要创建的员工/经理/招聘人员的数量

public Example(int employee,int manager,int Recruiter)
{
   // Rest of code
}
protected void CreatePersonInstances<T>(int count) where T:Person,new()
{
    EmployeeList.AddRange(IEnumerable.Range(1,count).Select(x=>new T()));
}
您还可以使CreatePersonInstances方法成为泛型方法,该方法接受从Person继承的参数类型以及要创建的实例数

public Example(int employee,int manager,int Recruiter)
{
   // Rest of code
}
protected void CreatePersonInstances<T>(int count) where T:Person,new()
{
    EmployeeList.AddRange(IEnumerable.Range(1,count).Select(x=>new T()));
}
受保护的void CreatePersonInstances(int count),其中T:Person,new()
{
AddRange(IEnumerable.Range(1,count).Select(x=>newt());
}
现在可以将构造函数定义更改为

public Example(int employeeCount,int managerCount,int recruiterCount)
{
    CreatePersonInstances<Employee>(employeeCount);
    CreatePersonInstances<Manager>(managerCount);
    CreatePersonInstances<Recruiter>(recruiterCount);
    
    foreach (Person personToInitialize in ExampleList)
            personToInitialize.Initialize();
}
公共示例(int employeeCount、int managerCount、int recruiterCount)
{
CreatePersonInstances(employeeCount);
CreatePersonInstances(managerCount);
CreatePersonInstances(招聘人员计数);
foreach(示例列表中的personToInitialize)
personToInitialize.Initialize();
}
员工、经理和招聘人员从个人实体继承


    public class Person
    {
        public string Name { get; set; }
    }

    public class Employee : Person
    {
        public int EmployeeId { get; set; }
    }

    public class Manager : Person
    {
        public int ManagerId { get; set; }
    }

    public class Recruiter : Person
    {
        public int RecruiterId { get; set; }
    }

除了问题的范围之外,整个模型是工厂设计模式的一个很好的用例。如果没有,请检查它的传入参数?然后
for(int i=0;i
etcIf您希望在创建
Example
类型的对象时始终指定这些数字-为什么不添加三个构造函数参数:
public void示例(int numep,int numManagers,int numRecruiters)
-这样,您将始终获得这些值,并且您可以使用这些值来正确初始化object中的列表。感谢您提供关于Factory设计模式的非常有用的建议。我研究了它,它非常有用。很高兴它有帮助,如果你觉得这个答案解决了你的疑问,请不要忘记勾选它