在MyClass中添加通用列表,但如何?
我如何在通用类中添加列表? 首先我的通用类是:
[Serializable] public class ScheduleSelectedItems { private string Frequency; List FrequencyDays = new List(); private string Time; private string StartTime; private string EndTime; private string StartDate; private string EndDate; private string Name; public ScheduleSelectedItems(string frequency,List frequencydays, string time, string starttime, string endtime, string startdate, string enddate, string name) { Frequency = frequency; FrequencyDays = frequencydays; Time = time; StartTime = starttime; EndTime = endtime; StartDate = startdate; EndDate = enddate; Name = name; } } [Serializable] public class ScheduleSelectedItemsList { public List Items; public ScheduleSelectedItemsList() { Items = new List(); } } and i want to add ScheduleSelectedItems into ScheduleSelectedItemsList in form1.cs Form1.cs codes is here : private void timer1_Tick(object sender, EventArgs e) { string saat = DateTime.Now.ToShortTimeString(); string bugun = DateTime.Today.ToShortDateString(); ScheduleMng smgr = new ScheduleMng(); ScheduleItemsList schlist = smgr.LoadXml(); List list = new List(); for (int i = 0; i = Convert.ToDateTime(schlist.Items[i].StartDate.ToString()) && Convert.ToDateTime(bugun)
slist.Items.Add(列表); ---->我不使用这些代码。 这些错误“包含一些无效的论点”,你怎么能帮助我? :)
我想这就是你想要的:
List<ScheduleSelectedItems> list = new List<ScheduleSelectedItems>();
泛型可以在函数/类的最后被简化
List<T> //generic type
T
表示一个类型,即int
(主类型)或MyClass
(类)
所以
List<MyClass> listOfMyClass = new List<MyClass>();
是MyClass
类型的项目List
在你的情况下,你没有得到一个泛型类,但我认为你可以通过以下方式使它泛型:
public class ScheduleSelectedItems<T>
{
private string frequency;
List<T> itemsToSchedule = new List<T>();
//(...)
public ScheduleSelectedItems(string frequency,List<T> items, /*(...)*/)
{
this.frequency = frequency;
this.itemsToSchedule = items;
//(...)
}
}
然后调用它
ScheduleSelectedItems<FrequencyDays> myItems = new ScheduleSelectedItems<FrequencyDays>("frequency", new List<FrequencyDays>())
它使用FrequencyDays
列表创建您的课程的新对象
这里是一个MSDN文章,解释泛型的基础知识
链接地址: http://www.djcxy.com/p/46563.html