简体   繁体   English

具有泛型的C#抽象类调用库

[英]C# abstract class calling base with generics

If I have the following abstract class: 如果我有以下抽象类:

public abstract class Page<T> where T : class
{
    public Func<T, object> SortBy { get; private set; }

    public Page(Func<T, object> sortBy)
    {
        this.SortBy = sortBy;
    }
}

How can I call the base constructor from the inheriting class? 如何从继承的类中调用基本构造函数? I want to do something like this: 我想做这样的事情:

public class ProductGridPage : Page<ProductGrid>
{
    public ProductGridPage() : base<ProductGrid>(pg => pg.Title)
    { 

    }
}

However base<ProductGrid>(pg => pg.Title) is not valid and wont compile. 但是base<ProductGrid>(pg => pg.Title)无效,无法编译。

public abstract class Page<T> where T : class 
{ 
    public Func<T, object> SortBy { get; private set; } 

    public Page(Func<T, object> sortBy) 
    { 
        this.SortBy = sortBy; 
    } 
} 

public class ProductGridPage : Page<ProductGrid> 
{ 
    public ProductGridPage() : base(pg => pg.Title) 
    {  

    } 
}
public class ProductGrid
{
  public string Title;
}

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

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