简体   繁体   English

抽象函数具有动态返回类型-C#

[英]Abstract function have dynamic return type - C#

I am trying to have an abstract function return a List of a custom data type during run time. 我正在尝试让抽象函数在运行时返回自定义数据类型的列表。

//Abstract class: Integr //抽象类:Integr

abstract public List < object > getRefills();

//Implementation in derived class: TMTStandard //在派生类中的实现:TMTStandard

public override List < TMTStandardRefill > getRefills()
{

    List<TMTStandardRefill> refills = db.TMTStandarRefills.ToList();
    return refills;

}

//I call the function to bind my data to a gridview data source //我调用函数将我的数据绑定到gridview数据源

dataGridView1.DataSource = integr.getRefills();

Any help would be appreciated. 任何帮助,将不胜感激。 I have looked at Generics but was unable to come up with a solution. 我看过泛型,但无法提出解决方案。

Make Integr a generic of T : 使IntegrT的泛型:

public abstract class Integr<T>
{
    public abstract IList<T> GetRefills();
}

public class TMTStandard : Integr<TMTStandardRefill>
{
    public override IList<TMTStandardRefill> GetRefills()
    {
        // ...
    }
}

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

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