Implicit conversion when an array is involved

192 views Asked by At

I have a CompanyLegacy class that I would like to convert to CompanyNew class. Here are my classes.

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
        };
    }
}

Ideally I would like to have a method call as simple as below for doing the conversion.

static CompanyNew GetCompany(CompanyLegacy company) { CompanyNew newCompany = company; return newCompany; }

As you can see I am trying to use implicit operators for doing the conversion. However I am not sure how can I use the implicit operator within CompanyLegacy for converting the Person[] to an Employee[] even though I can convert a Person to an Employee.

0

There are 0 answers