Today my brain went dead, and I couldn't figure out a clean way of forcing the compiler to use inheritance for Generic inference.
Imagine the following 4 classes
Models
public abstract class Model
{
}
public class CodePerfModel : Model
{
}
Entities
public abstract class ModelEntity<TModel> where TModel : Model
{
public TModel Model { get; set; }
}
public class CodePerfEntity : ModelEntity<CodePerfModel>
{
}
Now to me logically I should take for granted that when I take something that inherits from ModelEntity<>
(it will specify the type of TModel
) via inheritance, because any class that inherits from ModelEntity<>
will have to specify it.
Is there anyway to force the compiler to figure this out for me?
E.g.
If I currently want to use ModelEntity<>
, I have to specify a type for it. Such as the following:
public class CallerClass<TEntity, TModel>
where TEntity : ModelEntity<TModel>
where TModel : Model
{
}
How can I get rid of the TModel
argument everywhere? While still having access to the TModel type at compile time? E.g. via the base Model
property.
To me, something like the following:
public class CallerClass<TEntity>
where TEntity : ModelEntity<>
{
}
Would make perfect sense as when calling it all I should have to speicfy is e.g.
SomeCall<CodePerfEntity>();
rather than
SomeCall<CodePerfEntity, CodePerfModel>();
Is this something that is currently possible?
Would this be worth raising for C# 6/7?