Generics with Generic Parameters and Abstract class

前端 未结 4 410
不思量自难忘°
不思量自难忘° 2020-12-03 14:03

I\'ve got two generic base classes. The second generic class has a constraint on its parameter of the first class.

abstract class FirstClass {...}
         


        
4条回答
  •  佛祖请我去吃肉
    2020-12-03 14:48

    This is actually an answer to Interface with two generic parameters, solve one automatically which has been marked as duplicate of this question.

    You could declare a bunch of interfaces with a specific Id type. Not a perfect solution, but simplifies the declaration of entity classes.

    public interface IIntPersistentEntityService
            : IPersistentEntityService
        where TPersistentEntity : IPersistentEntity
    {
    }
    
    public interface IStringPersistentEntityService
            : IPersistentEntityService
        where TPersistentEntity : IPersistentEntity
    {
    }
    

    Then the User class can be declared like this:

    public class UserService : IIntPersistentEntityService
    {
        public User Get(int id)
        {
            throw new NotImplementedException();
        }
    }
    

    You will get a compiler error if you have not the matching Id type.

提交回复
热议问题