0

我有一个名为 Job 的类,它封装了另一个名为的类的对象,该类SelectedItem具有DateTime名为ItemDate;

class Job
{
    private SelectedItem m_SelectedItem = null;

    public Job() { }

    public SelectedItem Item
    {
        get { return m_SelectedItem; }
        set { m_SelectedItem = value; }
    }
}


class SelectedItem
{
    DateTime m_ItemDate = default(DateTime);

    public SelectedItem() { }

    public DateTime ItemDate
    {
        get 
        { 
            return (m_ItemDate == default(DateTime))
                ? DateTime.Now
                : m_ItemDate; 
        }
        set { m_ItemDate = value; }
    }
}

然后我有一个 JobManager 类,它有一个 Job 列表:JobQueue;

class JobManager
{
    private ICollection<Job> m_JobQueue = null;

    public JobManager() 
    {
        m_JobQueue = new List<Job>(); 
    }

    public ReadOnlyCollection<Job> JobQueue
    {
        get { return m_JobQueue.ToList().AsReadOnly(); }
    }

    private void CreateJob(SelectedItem item)
    {
        Job newJob = new Job();
        newJob.Item = item;
        m_JobQueue.Add(newJob);
    }

    public IList<Job> GetJobsFromQueue(int quantity, ICollection<Job> ignoreList)
    {
        return (from job in JobQueue
                select job).Skip(ignoreList.Count).Take(quantity).ToList();
    }

    public void CreateJobQueue(ICollection<SelectedItem> jobData)
    {
        for (long daysCount = 100; daysCount >= 1; daysCount--)
        {
            SelectedItem item = GetRandomItem(jobData.ToList()); //This just returns a random item from the specified jobData.
            item.ItemDate = DateTime.Today.AddDays((daysCount - 1));
            CreateJob(item);
        }
    }
}

现在的问题是,当我从主例程调用 CreateJobQueue() 时,ItemDate 将根据 daysCount 值正确插入,并完美地填充 JobQueue。

但是当尝试通过调用GetJobsFromQueue() 方法来检索一组作业时,整个JobQueue 中的ItemDate 值被弄乱了(即与插入的不同)。

任何人都可以有线索吗?

4

0 回答 0