using System.Collections.Generic; using System; public class Factory { delegate T InstantiateMethod (); Dictionary> _Products = new Dictionary> (); public void Register (TKey key) where T : TBase, new() { _Products.Add (key, Constructor); } public TBase Produce (TKey key) { return _Products [key] (); } static TBase Constructor () where T : TBase, new() { return new T (); } } class BaseClass { } class ChildClass1 : BaseClass { } class ChildClass2 : BaseClass { } class TestClass { public static int Main () { var factory = new Factory (); factory.Register (1); factory.Register (2); if (factory.Produce (1).GetType () != typeof (ChildClass1)) return 1; if (factory.Produce (2).GetType () != typeof (ChildClass2)) return 2; return 0; } }