基于字符串创建对象

时间:2016-12-23 11:11:10

标签: c# type-conversion

我正在开发一个程序,允许员工的添加,编辑等。我被困在给员工一个部门(部门是一个类)的部分,我需要根据其名称创建。有帮助吗?

代码:

系类

class Department
{
    public Department() { }

    public int DepartmentId { get; set; }
    public string DepartmentName { get; set; }
    public string Location { get; set; }

    public ICollection<Employee> EmployeesAtDepartment { get; set; }
}

员工类

abstract class Employee
{
    public int EmployeeId { get; set; }
    public string EmployeeName { get; set; }
    public string EmployeeSurname { get; set; }
    public string Address { get; set; }
    public int Grade { get; set; }
    public double Salary { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }

    public Department DepartmentOfEmployee {get; set; }

收集数据的方法

public Employee EmpDetails(Employee NewEmp)
{
    Console.WriteLine("Enter Employee Name:");
    NewEmp.EmployeeName = Console.ReadLine();
    Console.WriteLine("Enter Employee Surname:");
    NewEmp.EmployeeSurname = Console.ReadLine();
    Console.WriteLine("Enter Employee Address:");
    NewEmp.Address = Console.ReadLine();
    Console.WriteLine("Enter Employee Grade:");
    NewEmp.Grade = Convert.ToInt16(Console.ReadLine());
    Console.WriteLine("Enter Employee Salary:");
    NewEmp.Salary = Convert.ToDouble(Console.ReadLine());
    Console.WriteLine("Enter Department of Employee:");
    NewEmp.DepartmentOfEmployee = Console.ReadLine();
    Console.WriteLine("Enter Employee Username:");
    NewEmp.Username = Console.ReadLine();
    Console.WriteLine("Enter Employee Password:");
    NewEmp.Password = Console.ReadLine();

    return NewEmp;
}

2 个答案:

答案 0 :(得分:2)

您无法直接将字符串转换为类实例。但是,您可以使用Factory Pattern

来实现类似的功能
public static class DepartmentFactory
{
    public static Department CreateDepartment(string deptStr)
    {
        var dept = new Department();
        // rest of the code

        return dept;
    }
}

你的新代码:

public Employee EmpDetails(Employee NewEmp)
{
    Console.WriteLine("Enter Employee Name:");
    NewEmp.EmployeeName = Console.ReadLine();
    Console.WriteLine("Enter Employee Surname:");
    NewEmp.EmployeeSurname = Console.ReadLine();
    Console.WriteLine("Enter Employee Address:");
    NewEmp.Address = Console.ReadLine();
    Console.WriteLine("Enter Employee Grade:");
    NewEmp.Grade = Convert.ToInt16(Console.ReadLine());
    Console.WriteLine("Enter Employee Salary:");
    NewEmp.Salary = Convert.ToDouble(Console.ReadLine());
    // Edit here
    Console.WriteLine("Enter Department of Employee:");
    NewEmp.DepartmentOfEmployee = DepartmentFactory.CreateDepartment(Console.ReadLine());
    Console.WriteLine("Enter Employee Username:");
    NewEmp.Username = Console.ReadLine();
    Console.WriteLine("Enter Employee Password:");
    NewEmp.Password = Console.ReadLine();

    return NewEmp;
}

答案 1 :(得分:0)

为了保持整洁,只需使用枚举并传入文本值,如下所示:

public class Employee
    {
        public string Name { get; private set; }

        public AvailableClasses Class { get; private set; }

        public Employee(string name, string myClass)
        {
            Name = name;
            AvailableClasses classEnum;
            if (!Enum.TryParse(myClass, true, out classEnum))
                classEnum = AvailableClasses.Other;

            Class = classEnum;
        }
    }

    public enum AvailableClasses
    {
        Maths,
        Science,
        Other
    }