带有参数的 C# 类类型

标签 c# .net

我有一个接受类型参数的c#类

 public abstract class Repository<TDomainType, TIdType, TDatabaseType>
    : IUnitOfWorkRepository
    where TDomainType
    : IAggregateRoot
    where TDatabaseType : class

这个基类实际上是我想要在我的应用程序的正常和安全部分中使用的,为了不重复代码,我想做这样的事情:

 public class PmRepository<TDomainType, TIdType, TDatabaseType> : Repository<TDomainType, TIdType, TDatabaseType>

有没有办法做到这一点?

编辑 抱歉,我没有收到错误,因为我认为这样做有问题。

我得到的错误是这个

The type 'TDatabaseType' must be a reference type in order to use it as parameter 'TDatabaseType' in the generic type or method 'Repository<TDomainType, TIdType, TDatabaseType>'   

The type 'TDomainType' cannot be used as type parameter 'TDomainType' in the generic type or method 'Repository<TDomainType, TIdType, TDatabaseType>'. There is no boxing conversion or type parameter conversion from 'TDomainType' to 'ThunderCat.Core.Domain.IAggregateRoot'.    

最佳答案

您的继承类需要与父类(super class)相同的约束:

public class PmRepository<TDomainType, TIdType, TDatabaseType>
    : Repository<TDomainType, TIdType, TDatabaseType>
    where TDomainType : IAggregateRoot
    where TDatabaseType : class

关于带有参数的 C# 类类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33724791/

相关文章:

c# - Entity Framework CTP4 代码优先 : Mapping protected properties

c# - XmlSerializer、GenerateSerializer 和集合

c# - 为什么不能从不同的线程更新 ObservableCollection?

c# - 'T' 不包含 'Foo' 的定义,最佳扩展方法重载需要类型为 'IType' 的接收器

c# - C# 和非托管 native C++ 是否有一种通用方法来确定已安装的 CLR 版本?

C# DispatcherOperation 循环永不中断

c# - 针对 Active Directory 进行身份验证一次算作两次无效登录

C# DateTime.Now 精度

.net - ORM 和构造函数

c# - 将 "reference type"作为参数传递给具有 'ref' 键的方法是否有意义?