Given these classes:
public abstract class HostBase
{}
public abstract class ConfigBase
{}
public abstract class HostBase<TConfig> : HostBase where TConfig : ConfigBase
{
protected internal TConfig Config { get; set; }
}
public class GenericHost : HostBase<Config>
{}
public class HostFactory
{
public static THost Create<THost, TConfig>(TConfig config)
where THost : HostBase<TConfig>, new()
where TConfig : ConfigBase
{
return new THost { Config = config };
}
}
Why can't the compiler infer the type of TConfig
from HostFactory.Create<GenericHost>(new Config())
? It seems to me that there is only one possible type for TConfig
?
I don't get an inference error from the compiler, though:
The type '
GenericHost
' must be convertible toHostBase<TConfig>
in order to use it as parameter 'THost
' in the generic method 'THost HostFactory.Create<THost, TConfig>(TConfig)
'
This error seems strange, because this does compile: HostBase<Config> h = new GenericHost()
.
What am I missing?