All pastes #624162 Raw Edit

hah

public text v1 · immutable
·published 2007-07-18 02:23 UTC
rendered paste body
using System;

namespace test
{
    public class FactoryBase { }
    public class Factory<T> : FactoryBase 
        where T : FactoryBase, new()
    {
        public virtual FactoryBase Make()
        {
            return new T();
        }
    }

    public class DerivedA : Factory<DerivedA>
    {
        public DerivedA() { Console.WriteLine("DerivedA ctor"); }
    }

    public class DerivedB : Factory<DerivedB>
    {
        public DerivedB() { Console.WriteLine("DerivedB ctor"); }
    }

    static class main
    {
        static void Main(string[] args)
        {
            DerivedA a = new DerivedA();
            DerivedB b = new DerivedB();

            FactoryBase binst = b.Make();
            FactoryBase ainst = a.Make();
        }
    }
}