return false;
}
+
+ bool HasMvar ()
+ {
+ if (ContainsMethodTypeParameter (type))
+ return false;
+
+ var best = method_group.BestCandidate;
+ if (ContainsMethodTypeParameter (best.DeclaringType))
+ return false;
+
+ if (best.TypeArguments != null) {
+ foreach (var ta in best.TypeArguments) {
+ if (ContainsMethodTypeParameter (ta))
+ return false;
+ }
+ }
+
+ return true;
+ }
protected override Expression DoResolve (ResolveContext ec)
{
//
// Cannot easily cache types with MVAR
//
- if (ContainsMethodTypeParameter (type))
- return expr;
-
- if (ContainsMethodTypeParameter (method_group.BestCandidate.DeclaringType))
+ if (!HasMvar ())
return expr;
//
--- /dev/null
+using System.Collections.Generic;
+using System;
+
+public class Factory<TKey, TBase>
+{
+ delegate T InstantiateMethod<T> ();
+
+ Dictionary<TKey, InstantiateMethod<TBase>> _Products = new Dictionary<TKey, InstantiateMethod<TBase>> ();
+
+ public void Register<T> (TKey key) where T : TBase, new()
+ {
+ _Products.Add (key, Constructor<T>);
+ }
+
+ public TBase Produce (TKey key)
+ {
+ return _Products [key] ();
+ }
+
+ static TBase Constructor<T> () 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<byte, BaseClass> ();
+ factory.Register<ChildClass1> (1);
+ factory.Register<ChildClass2> (2);
+
+ if (factory.Produce (1).GetType () != typeof (ChildClass1))
+ return 1;
+
+ if (factory.Produce (2).GetType () != typeof (ChildClass2))
+ return 2;
+
+ return 0;
+ }
+}
\ No newline at end of file
</method>\r
</type>\r
</test>\r
+ <test name="gtest-602.cs">\r
+ <type name="Factory`2[TKey,TBase]">\r
+ <method name="Void Register[T](TKey)" attrs="134">\r
+ <size>26</size>\r
+ </method>\r
+ <method name="TBase Produce(TKey)" attrs="134">\r
+ <size>26</size>\r
+ </method>\r
+ <method name="TBase Constructor[T]()" attrs="145">\r
+ <size>54</size>\r
+ </method>\r
+ <method name="Void .ctor()" attrs="6278">\r
+ <size>18</size>\r
+ </method>\r
+ </type>\r
+ <type name="Factory`2+InstantiateMethod`1[TKey,TBase,T]">\r
+ <method name="T Invoke()" attrs="454">\r
+ <size>0</size>\r
+ </method>\r
+ <method name="IAsyncResult BeginInvoke(System.AsyncCallback, System.Object)" attrs="454">\r
+ <size>0</size>\r
+ </method>\r
+ <method name="T EndInvoke(IAsyncResult)" attrs="454">\r
+ <size>0</size>\r
+ </method>\r
+ <method name="Void .ctor(Object, IntPtr)" attrs="6278">\r
+ <size>0</size>\r
+ </method>\r
+ </type>\r
+ <type name="BaseClass">\r
+ <method name="Void .ctor()" attrs="6278">\r
+ <size>7</size>\r
+ </method>\r
+ </type>\r
+ <type name="ChildClass1">\r
+ <method name="Void .ctor()" attrs="6278">\r
+ <size>7</size>\r
+ </method>\r
+ </type>\r
+ <type name="ChildClass2">\r
+ <method name="Void .ctor()" attrs="6278">\r
+ <size>7</size>\r
+ </method>\r
+ </type>\r
+ <type name="TestClass">\r
+ <method name="Int32 Main()" attrs="150">\r
+ <size>108</size>\r
+ </method>\r
+ <method name="Void .ctor()" attrs="6278">\r
+ <size>7</size>\r
+ </method>\r
+ </type>\r
+ </test>\r
<test name="gtest-anontype-01.cs">\r
<type name="Test">\r
<method name="Int32 Main()" attrs="150">\r