c# - Generic inference of type parameter with constraint to other type parameter -


given these classes:

public abstract class hostbase {} public abstract class configbase {} public abstract class hostbase<tconfig> : hostbase tconfig : configbase {     protected internal tconfig config { get; set; } } public class generichost : hostbase<config> {} public class hostfactory {     public static thost create<thost, tconfig>(tconfig config)         thost : hostbase<tconfig>, new()         tconfig : configbase     {         return new thost { config = config };     }  } 

why can't compiler infer type of tconfig hostfactory.create<generichost>(new config())? seems me there 1 possible type tconfig?

i don't inference error compiler, though:

the type 'generichost' must convertible hostbase<tconfig> in order use parameter 'thost' in generic method 'thost hostfactory.create<thost, tconfig>(tconfig)'

this error seems strange, because compile: hostbase<config> h = new generichost().

what missing?

you can't infer some type parameters within method call. generic type inference either infers all type parameters, or none. there's no way of inferring thost parameters (there multiple classes derive hostbase<config>), means can't use type inference method.

looking @ specific example, think you're going find tricky use type inference @ all, because of way relationships work.


Comments