I’d like to be able to create a static generic type with a base type constraint like
public static class Manager<T> where T : HasId
{
public static T GetSingleById(ref List<T> items, Guid id)
{
// the Id is a property provided by HasId
return (from i in items where i.Id == id select i).SingleOrDefault();
}
}
Then add another method
...
public static IEnumerable<T> GetManyByParentId(ref List<T> items, Guid parentId) where T : HasIdAndParentId
{
// the parentId is a property of HasIdAndParentId which subclasses HasId
return from i in items where i.ParentId == parentId select i;
}
...
Since HasIdAndParentId subclasses HasId the constraint T : HasId is met but the compiler won’t accept the where base type constraint on the method.
Any ideas?
In this case, you’re not redefining the type parameter on the method, so you can’t apply any new constraints. You should be able to do it this way: