暗黙の型変換

implicit operatorを使って継承の真似事をしてみて思ったのですが、インタフェース継承時にimplicit operatorが指定されていたら、インタフェースは実装済みとしてくれたら便利な気がします。

using System;

public class Person
{
    string name;
    int age;

    public Person(string name, int age)
    {
        this.name = name;
        this.age = age;
    }

    public string Name
    {
        get { return name; }
    }

    public int Age
    {
        get { return age; }
    }
}

public class Programmer
{
    Person person;
    string langage;

    public Programmer(Person person, string langage)
    {
        this.person = person;
        this.langage = langage;
    }

    public string Langage
    {
        get { return langage; }
    }

    public static implicit operator Person(Programmer src)
    {
        return src.person;
    }
}

public class Manager
{
    Person person;
    string dept;

    public Manager(Person person, string dept)
    {
        this.person = person;
        this.dept = dept;
    }

    public string Dept
    {
        get { return dept; }
    }

    public static implicit operator Person(Manager src)
    {
        return src.person;
    }
}

public class ProgramManager : Person
{
    Programmer programmer;
    Manager manager;
    public ProgramManager(string name, int age, string dept, string langage)
        : base(name, age)
    {
        programmer = new Programmer(this, langage);
        manager = new Manager(this, dept);
    }

    public static implicit operator Programmer(ProgramManager src)
    {
        return src.programmer;
    }

    public static implicit operator Manager(ProgramManager src)
    {
        return src.manager;
    }
}

class Program
{
    static string PrintBase(Person person)
    {
        return String.Format("name = {0}, age = {1}", person.Name, person.Age);
    }

    static void PrintProgrammer(Programmer programmer)
    {
        Console.WriteLine("{0}, lang = {1}", PrintBase(programmer), programmer.Langage);
    }

    static void PrintManager(Manager manager)
    {
        Console.WriteLine("{0}, dept = {1}", PrintBase(manager), manager.Dept);
    }

    static void Main(string[] args)
    {
        ProgramManager pm = new ProgramManager("taro", 20, "Product", "C#");
        PrintProgrammer(pm);
        PrintManager(pm);
    }
}
/*
name = taro, age = 20, lang = C#
name = taro, age = 20, dept = Product
 */

上記を、

public class ProgramManager : Person, IProgrammer, IManager
{
    Programmer programmer;
    Manager manager;
    public ProgramManager(string name, int age, string dept, string langage)
        : base(name, age)
    {
        programmer = new Programmer(this, langage);
        manager = new Manager(this, dept);
    }

    public static implicit operator IProgrammer(ProgramManager src)
    {
        // src.programmerはIProgrammerを実装している
        return src.programmer;
    }

    public static implicit operator IManager(ProgramManager src)
    {
        // src.managerはIManagerを実装している
        return src.manager;
    }
}

こんな感じで書かせてくれたら良かったのに。