简体   繁体   English

类型参数声明必须是标识符

[英]Type parameter declaration must be an identifier

interface IRepository<TEntity,Tid> where TEntity :class 
{
    int Insert(TEntity entity);
    int Delete(TEntity entity);
    TEntity GetById(Tid id);
    TEntity GetByFilters(TEntity entity);
    int Update(TEntity entity);        
}

Trying to implement 尝试实施

internal class Repository<XYZClass, string> : IRepository<XYZCLass, string> 
{    
    //...All interface methods    
}

Getting below error: 出现以下错误:

type parameter declaration must be an identifier not a type 类型参数声明必须是标识符而不是类型

Any Suggestions.. 有什么建议么..

If you want Repository to be non-generic you need to specify both types: 如果您希望Repository是非通用的,则需要指定两种类型:

internal class Repository : IRepository<XYZClass, string> { ... }

If you want Repository to be generic but specify that TId is a string you can use 如果您希望Repository是通用的,但指定TIdstring ,则可以使用

internal class Repository<TEntity> : IRepository<TEntity, string> where TEntity : class
{    
    //...All interface methods    
}

string is a type, not a generic type parameter. string是类型,而不是泛型类型参数。

The declaration should only include type parameters for those that are generic to those consuming the class. 该声明应仅包含那些使用该类的参数的类型参数。

In other words, if you implementation of IRepository sets the TEntity parameter to XYZClass and the TID parameter to string (ie a concrete class definition), then you should have: 换句话说,如果实现IRepository,则将TEntity参数设置为XYZClass,而TID参数设置为字符串(即具体的类定义),那么您应该具有:

internal class Repository : IRepository<XYZCLass, string> 
{    
    //...All interface methods    
}

If you wish to define one type and leave the other generic then: 如果要定义一种类型,而保留另一种泛型,则:

internal class Repository<TEntity> : IRepository<TEntity, string> 
{    
    //...All interface methods    
}

Otherwise, both types should still be left open: 否则,两种类型都应保持打开状态:

internal class Repository<TEntity,TID> : IRepository<TEntity, TID> 
{    
    //...All interface methods    
}

Maybe you want a base class constraint? 也许您想要基类约束?

internal class Repository<TYourEntity> : IRepository<TYourEntity, string>
    where TYourEntity : XyzClass
{    
    //...All interface methods    
}

Note that this constraint means that the constraint on the interface (the reference type constraint TEntity : class ) is satisfied. 注意,此约束意味着满足接口上的约束(引用类型约束TEntity : class )。


For completeness, the other "legal" interpretations of what you meant (also in other answers already), are: 为了完整起见,对您的意思的其他“合法”解释是(也已经在其他答案中):

// don't constrain generic parameter, just name it TXyz (TXyz is "declared" here)

internal class Repository<TXyz> : IRepository<TXyz, string>
    where TXyz : class
{    
    //...All interface methods    
}

and: 和:

// make class non-generic (the type XyzClass exists and is declared elsewhere)

internal class Repository : IRepository<XyzClass, string>
{    
    //...All interface methods    
}

因此,存储库不是通用的(它是通过具体类参数化的存储库接口的实现):

internal class Repository : IRepository<XYZCLass, string>

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

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