涉及数组时的隐式转换

时间:2015-06-18 14:26:41

标签: c# .net operator-keyword implicit-conversion

我有一个CompanyLegacy类,我想将其转换为CompanyNew类。 这是我的课程。

public class CompanyLegacy
{
    public string Name { get; set; }
    public Person[] Persons { get; set; }

    public static implicit operator CompanyNew(CompanyLegacy setting)
    {
        // How to convert Persons[] to Employee[]
    }
}

public class CompanyNew
{
    public string Name { get; set; }
    public Employee[] Employees { get; set; }

}

public class Employee
{
    public string LastName { get; set; }
    public string FirstName { get; set; }
}

public class Person
{
    public string LastName { get; set; }
    public string FirstName { get; set; }

    public static implicit operator Employee(Person p)
    {
        return new Employee()
        {
            LastName = p.LastName,
            FirstName = p.FirstName
        };
    }
}

理想情况下,我希望方法调用如下所示进行转换。

static CompanyNew GetCompany(CompanyLegacy company)         {             公司新公司=公司;             返回新公司;         }

正如您所看到的,我正在尝试使用隐式运算符进行转换。但是我不知道如何使用CompanyLegacy中的隐式运算符将Person []转换为Employee [],即使我可以将Person转换为Employee。

0 个答案:

没有答案