【发布时间】:2010-11-15 21:45:52
【问题描述】:
自从我上次接触 Java 以来,我就没有使用过反射,我有点生疏了。我创建了一个使用计时器的 Windows 服务,并且对于某些类,我需要动态的对象名称(从 sql 表中提取)。对象的创建范围在一个 while 循环内,我需要能够保存每个对象的属性。
编辑:
最后使用了字典,以便我以后可以检索每个对象的属性。
private void GetActiveScans()
{
string sql = "SELECT * FROM scans WHERE enabled = 1";
SqlConnection sqlconn = new SqlConnection(connectionString);
sqlconn.Open();
SqlCommand cmd = new SqlCommand(sql, sqlconn);
SqlDataReader drActiveScans = cmd.ExecuteReader(CommandBehavior.CloseConnection);
Dictionary<int, WindowsServiceAudit> WSA = new Dictionary<int, WindowsServiceAudit>();
while (drActiveScans.Read())
{
string tmpscantype = drActiveScans["scantype"].ToString();
switch (tmpscantype)
{
case "services":
int scanid = Convert.ToInt32(drActiveScans["scanid"]);
string scanname = drActiveScans["scanname"].ToString();
int serverclass = Convert.ToInt32(drActiveScans["serverclass"]);
int interval = Convert.ToInt32(drActiveScans["interval"]);
WindowsServiceAudit x = new WindowsServiceAudit(scanid, scanname, serverclass, interval);
WSA.Add(scanid, x);
break;
}
}
}
【问题讨论】:
-
哇,这是迄今为止我见过的最疯狂的问题!