【问题标题】:C# Multiple Generic ParametersC# 多个通用参数
【发布时间】:2018-11-07 12:50:21
【问题描述】:

下面的代码使用一个通用参数。

有没有办法在我想要 2 个或更多类的地方获取多个泛型变量? (例如,T1 类、T2 类等)

原始通用:

public interface IGenericRepository<T> where T : class 
{
    IQueryable<T> GetAll();
    IQueryable<T> FindBy(Expression<Func<T, bool>> predicate);
    void Add(T entity);
    void Delete(T entity);
    void Edit(T entity);
    void Save();
}

【问题讨论】:

标签: c# asp.net-core interface repository


【解决方案1】:

泛型类型可以是任何东西,而不仅仅是 T - T 恰好是常见的。

例子:

void foo<TOne, TTwo>() 
   where TOne : BaseOne
   where TTwo : BaseTwo

更多信息可以查看here。查看“约束多个参数”部分。

【讨论】:

  • 我也可以只使用Class,而不是Base1,Base2,如下所示? void foo() where TOne : class where TTwo : class
  • 是的,您可以根据链接文章开头的表格使用类来限制类类型。
猜你喜欢
  • 1970-01-01
  • 2012-01-31
  • 1970-01-01
  • 2014-11-16
  • 1970-01-01
  • 2011-01-02
  • 2011-02-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多