简体   繁体   English

如何从另一个参数的泛型参数类型中推断泛型类型

[英]How to Infer generic type from generic argument type of another argument

MVC has nothing to do with my problem. MVC与我的问题无关。 Don't get confused for the example. 不要为这个例子感到困惑。 Its a pure C# problem 它是一个纯粹的C#问题

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 假设,我有一个名为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 . 现在,您必须注意到,当控制器中的Repository1类型是Repository1 ,实体类型必须是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. 如果是Java。 Replacing ControllerBase declaration with 用。替换ControllerBase声明

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 由于某种原因,没有约束类型推断: 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? 如果没有在Controller12定义中指定Entity1类型参数,编译器应该检查什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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