简体   繁体   中英

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

abstract public List < object > getRefills();

//Implementation in derived class: 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

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 :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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