如何封装具有类属性列表?
题
如何封装下面的代码(我需要refoctoring)
public class mycollection
{
private DateTime tarih;
private int sira;
private int visitingcount;
public DateTime TARIH { get { return tarih; } set { tarih = value; } }
public int SIRA { get { return sira; } set { sira = value; } }
public int VISITINGCOUNT { get { return visitingcount; } set { visitingcount = value; } }
}
<强>我用下面这个类强>
DataRow[] rows = dsChart.Tables[0].Select("TARIH>='" + DateGap1 + "' and TARIH<='" + DateGap2 + "'");
list = new List<mycollection>();
foreach (DataRow dr in rows)
{
mycollection mycol = new mycollection();
mycol.TARIH = Convert.ToDateTime(dr["TARIH"].ToString());
mycol.SIRA = Convert.ToInt32(dr["SIRA"].ToString());
mycol.VISITINGCOUNT = Convert.ToInt32(dr["VISITINGCOUNT"].ToString());
list.Add(mycol);
}
<强>我需要这样的:强>
public static void LoadDepartman(string departmanName)
{
List<StuffDepartman> li = new List<StuffDepartman>();
GetDepartman departman = new GetDepartman();
DataSet ds = departman.GetDepartmanA(departmanName);
foreach (DataRow dr in ds.Tables[0].Rows)
{
li.Add(new StuffDepartman
{
Name = dr["Name"].ToString(),
SurName = dr["SurName"].ToString(),
Address = dr["Address"].ToString(),
Phone = dr["Phone"].ToString(),
DepartmanName = dr["DepartmanName"].ToString(),
Salary =int.Parse( dr["Salary"].ToString()),
Married = dr["Married"].ToString()
}
);
}
HttpContext.Current.Session["Stuffs"] = li;
}
没有正确的解决方案
其他提示
(我不完全了解前两个例子是如何与第三......他们似乎并没有分享任何共同之处)
那么,目前突破?我可以看到的最大的问题是,(这个)为可靠地可用与Session
(具有不同的后端实现方式)的项目必须是可序列化(VAR BinaryFormatter
),但可能是因为标记mycollection
那样简单与[Serializable]
。
有关信息mycollection
的东西是一个令人困惑的名称是行的实体,而不是实际的征收 - 如果你有C#3.0,你可以简化事情有点与automattically实现的属性:
[Serializable]
public class SomeSensibleName
{
public DateTime Tarih {get;set;}
public int Sira {get;set;}
public int VisitCount {get;set;}
}
然而;目前尚不清楚(从您的代码),这是否会改变什么。请你能澄清什么是目前发生的事情?
如果我理解正确的话,这是你正在寻找的重构:
DataRow[] rows = dsChart.Tables[0].Select("TARIH >='" + DateGap1 + "' and TARIH <='" + DateGap2 + "'");
List<mycollection> list = new List<mycollection>();
foreach (DataRow dr in rows)
{
list.Add(new mycollection
{
TARIH = Convert.ToDateTime(dr["TARIH"].ToString());
SIRA = Convert.ToInt32(dr["SIRA"].ToString());
VISITINGCOUNT = Convert.ToInt32(dr["VISITINGCOUNT"].ToString());
});
}
请记住,为了这个工作,你需要运行在.NET Framework 3.X(VS2008)。
不隶属于 StackOverflow