c# - "Reverse" type inference possible? -
given following classes (factories used because c# doesn't support type inference on constructors):
public class a<t> { public a(b<t> b) { } } public class b<t> { public b(c<t> c) { } } public class c<t> { public c(t tee) { } } public class d<t> { public static d<t> create<v>(expression<func<t, v>> property) { return new d<t, v>(property); } } public class d<t, v> : d<t> { public d(expression<func<t, v>> property) { } } public class model { public int p1 { get; set; } public string p2 { get; set; } } public class afactory { public static a<t> create<t>(b<t> bee) { return new a<t>(bee); } } public class bfactory { public static b<t> create<t>(c<t> cee) { return new b<t>(cee); } } public class cfactory { public static c<t> create<t>(params d<t>[] tees) { return null; } } the following compiles:
afactory.create(bfactory.create(cfactory.create( d<model>.create(m => m.p1), d<model>.create(m => m.p2) ))); the following does not:
afactory.create<model>(bfactory.create(cfactory.create( d.create(m => m.p1), d.create(m => m.p2) ))); the difference in first example i'm specifying type of model on innermost classes, type inference works , propagates tree. problem have specify model type on every d.create() call, seems redundant.
the second example way i'd like write code: tell outermost class type model , classes being constructed use type well. essentially, it's syntactic sugar afactory.create<model>(bfactory.create<model>(/* turtles way down... */)).
is there way achieve in c#? i've tried permutations of inheritance , type constraints can think of, nothing has given me desired result.
i'm aware might missing fundamental generics - please feel free educate me if that's case.
Comments
Post a Comment