What are the good reasons to wish that .NET generics could inherit one of the generic parameter types?

后端 未结 8 1426
旧时难觅i
旧时难觅i 2020-12-06 04:40

This post is in continuation of this one.

I am trying to understand if I am the only one who misses and needs the ability of a .NET generic type to inherit one of it

8条回答
  •  难免孤独
    2020-12-06 05:44

    The good reason to make it possible to inherit from a type parameter is because of the following situation:

    I have would like to have an abstract ViewModel class which inherits from an Entity class and has some additional properties and methods.

    public abstract class ViewModel : TEntity, IViewModel 
    where TEntity : class, IEntity, new() {
        public void SomeMethod() {
    
        }
    }
    

    Then i could make a specific ViewModel

    public class EmployeeViewModel : ViewModel {
    
    }
    

    Nice ! it inherits all the fields of the entity and has the standard properties and methods of the abstract viewmodel ! ..

    This sadly cannot be done now.

提交回复
热议问题