List of generic Type

前端 未结 2 1073
再見小時候
再見小時候 2021-01-01 20:19

I have a generic class and I want to create a list of it. and then at run time I get the type of the item

Class

public class Job
{
    pub         


        
2条回答
  •  一整个雨季
    2021-01-01 20:44

    By making an interface and implement it in your class you will be able to create a list of that interface type,adding any job :

    interface IJob
    {
        //add some functionality if needed
    }
    
    public class Job : IJob
    {
        public int ID { get; set; }
        public Task Task { get; set; }
        public TimeSpan Interval { get; set; }
        public bool Repeat { get; set; }
        public DateTimeOffset NextExecutionTime { get; set; }
    
        public Job RunOnceAt(DateTimeOffset executionTime)
        {
            NextExecutionTime = executionTime;
            Repeat = false;
            return this;
        }
    }
    
    List x = new List();
    x.Add(new Job());
    

提交回复
热议问题