是的,我知道,ICollection
和ICollection<T>
是兩回事。讓我來描述一下我想,雖然...分配我的ICollection <T>到ICollection的財產
這是我希望做什麼:
public class SubmissionListViewModel
{
public ICollection<ProjectSubmissionViewModel> Submissions { get; set; }
public ReportBase GetContribAuthorsReport()
{
var report = new ContribAuthorsReport{ ReportType = "CSV" };
var chapAffil = new ChapterAffiliates {ReportItems = Submissions};
report.ReportSections.Add(chapAffil);
// AT this point, I would expect report.ReportSections.First().ReportItems
// to have the records from Submissions, but I have `null`.
return report;
}
}
問:爲什麼report.ReportSections.First().ReportItems
空? (參見代碼中的註釋以上。)
下面是如何ContribAuthorsReport
和ChapterAffiliates
定義:
public class ContribAuthorsReport : ReportBase
{
public ContribAuthorsReport()
{
ReportSections = new List<ReportSectionBase>();
}
public override string ReportName { get { return "Contributing Affiliates' Contact Information"; } }
}
public class ChapterAffiliates : ReportSectionBase
{
public override string ReportSectionName { get { return "Chapter Affiliates"; } }
// This is what I have now
new public ICollection<ProjectSubmissionViewModel> ReportItems { get; set; }
// This won't compile
//public override ICollection<ProjectSubmissionViewModel> ReportItems { get; set; }
// This would compile, but breaks GetContribAuthorsReport()
//public override ICollection ReportItems { get; set; }
}
我會從ReportSectionBase繼承其他類,這將會對ICollection的不同車型。我相信這個問題源於我如何定義基類。 (見下)
我的基類。
public abstract class ReportBase
{
virtual public string ReportType { get; set; }
virtual public string ReportName { get; set; }
public ICollection<ReportSectionBase> ReportSections { get; set; }
}
public abstract class ReportSectionBase
{
virtual public string ReportSectionName { get; set; }
virtual public ICollection ReportItems { get; set; }
}
UPDATE:最終結果 - 這是我現在使用的東西。
public class ReportBase
{
public ReportBase()
{
ReportSections = new List<IReportSection>();
}
public string ReportType { get; set; }
public string ReportName { get; set; }
public ICollection<IReportSection> ReportSections { get; set; }
}
public interface IReportSection
{
string ReportSectionName { get; }
ICollection ReportItems { get; set; }
}
public class ReportSection<T> : IReportSection
{
public string ReportSectionName { get; set; }
public ICollection<T> ReportItems { get; set; }
ICollection IReportSection.ReportItems
{
get { return ReportItems as ICollection; }
set { ReportItems = value as ICollection<T>; }
}
}
這讓我來定義,因爲這容易的報告:
public ReportBase GetContribAuthorsReport
(
ICollection<ProjectAffiliateViewModel> projectAffiliates,
ICollection<SubmissionAffiliateViewModel> submissionAffiliates
)
{
var caReport = new ReportBase { ReportType = "CSV", ReportName = "Reviewers' Contact Information" };
caReport.ReportSections.Add(new ReportSection<ProjectAffiliateViewModel> { ReportItems = projectAffiliates });
caReport.ReportSections.Add(new ReportSection<SubmissionAffiliateViewModel> { ReportItems = submissionAffiliates });
return caReport;
}
你的問題是,到底是什麼? –
在我的評論中:report.ReportSections.Add(chapAffil); //在這一點上,我期望report.ReportSections.First()。ReportItems //擁有來自提交的記錄,但我有'null'。 –
您需要重寫您的問題,以便明確*說明*具體說明*您需要什麼。不要讓人們挖掘你的代碼行,試圖破譯你想弄明白的東西。 –