Merge pull request #1861 from saper/home-override
[mono-project.git] / mcs / tests / gtest-602.cs
blob87ae36011e6432b46850e5dd76020d7f4ad76965
1 using System.Collections.Generic;
2 using System;
4 public class Factory<TKey, TBase>
6 delegate T InstantiateMethod<T> ();
8 Dictionary<TKey, InstantiateMethod<TBase>> _Products = new Dictionary<TKey, InstantiateMethod<TBase>> ();
10 public void Register<T> (TKey key) where T : TBase, new()
12 _Products.Add (key, Constructor<T>);
15 public TBase Produce (TKey key)
17 return _Products [key] ();
20 static TBase Constructor<T> () where T : TBase, new()
22 return new T ();
26 class BaseClass
30 class ChildClass1 : BaseClass
34 class ChildClass2 : BaseClass
38 class TestClass
40 public static int Main ()
42 var factory = new Factory<byte, BaseClass> ();
43 factory.Register<ChildClass1> (1);
44 factory.Register<ChildClass2> (2);
46 if (factory.Produce (1).GetType () != typeof (ChildClass1))
47 return 1;
49 if (factory.Produce (2).GetType () != typeof (ChildClass2))
50 return 2;
52 return 0;