简体   繁体   中英

How to Infer generic type from generic argument type of another argument

MVC has nothing to do with my problem. Don't get confused for the example. Its a pure C# problem

The title of the question is not well explaining the question I think.


Suppose, I have a base class for some entity classes called EntityBase

Some classes are like

class Entity1 : EntityBase
class Entity2 : EntityBase

I have an abstract Repository that works with basic operations on entities. The declaration is:

abstract class RepositoryBase<TEntity> where TEntity : EntityBase

And there are several implementations of this class

class Repository1 : RepositoryBase<Entity1>
class Repository2 : RepositoryBase<Entity2>

Now there are some controllers with a base:

public abstract class RepositoryControllerBase<TRepository, TEntity> 
        where TRepository : RepositoryBase<TEntity>
        where TEntity : EntityBase

And implementations are like

class Controller1 : RepositoryControllerBase<Repository1, Entity1>
class Controller2 : RepositoryControllerBase<Repository2, Entity2>

Now, you must have noticed that, When the type of repository in a controller is Repository1 , The entity type must be Entity1 . Otherwise it will be a compilation error.

So, I think there is a way to skip the second generic type and automatically infer that one. I just do not know how. Any suggestions?

Perhaps, the problem could be easily solved with ? if it was Java. Replacing ControllerBase declaration with

public abstract class RepositoryControllerBase<TRepository> 
            where TRepository : RepositoryBase<?>

There is no constraint type inference for a reason: http://blogs.msdn.com/b/ericlippert/archive/2012/03/09/why-not-automatically-infer-constraints.aspx

Also, the obvious counterexample for your idea would be using interfaces:

interface IEntity1 : IEntityBase {}
interface IEntity2 : IEntityBase {}

interface IRepositoryBase<TEntity> where TEntity : class, IEntityBase {}

class Repository1 : RepositoryBase<IEntity1> {}
class Repository2 : RepositoryBase<IEntity2> {}
class Repository12 : IRepositoryBase<IEntity1>, IRepositoryBase<IEntity2> {}

public abstract class RepositoryControllerBase<TRepository, TEntity> 
    where TRepository : RepositoryBase<TEntity>
    where TEntity : IEntityBase {}

class Controller1 : RepositoryControllerBase<Repository1, Entity1>
class Controller2 : RepositoryControllerBase<Repository2, Entity2>
class Controller12 : RepositoryControllerBase<Repository12, Entity1>

Without specifying an Entity1 type parameter in a Controller12 definition, what should a compiler check?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM