ipjohnson / Grace

Grace is a feature rich dependency injection container library
MIT License
336 stars 33 forks source link

No type constraints on generic As() methods #234

Closed silkfire closed 4 years ago

silkfire commented 4 years ago

Is there a particular reason why no type constraint is enforced on the generic .As...<> methods?

As of right now, this code would be perfectly fine to write:

scope.Export<string>.As<int>();

Ideally the implementing type parameter should be constrained:

public IFluentExportInstanceConfiguration<T> As<TInterface>()
   where T : TInterface
{
   _exportConfiguration.AddExportAs(typeof(TInterface));

   return this;
}
ipjohnson commented 4 years ago

You can't technically define the method that way. The compiler complains.

DependencyInjection\IFluentExportStrategyConfiguration.cs(173,70,173,71): error CS0699: 'IFluentExportStrategyConfiguration<T>.As<TInterface>()' does not define type parameter 'T'
silkfire commented 4 years ago

I see, a limitation of the CLR then. Very well, good to know then.