我需要创建一个对象的实例,该对象的类型将在运行时确定.从SQL中提取对象的类型并将其设置为字符串值.我还需要在实例化时传递一些参数.参数的数量/类型每次都相同(至少现在).我需
private void StartScans(int scan_typeid, SqlDataReader drActiveServers) { string sql = "SELECT scan_typeclass from scan_types WHERE scan_typeid = " + scan_typeid.ToString(); sqlconn.Open(); SqlCommand cmd = new SqlCommand(sql, sqlconn); SqlDataReader drScanClass = cmd.ExecuteReader(CommandBehavior.CloseConnection); string scan_class = drScanClass["scan_typeclass"].ToString(); //Create object here }
编辑:
理查德·伯格的解决方案在控制台应用程序中工作但不在上面的示例中,我已经转储了scan_class并验证了它获取值但是我一直收到此错误:
System.ArgumentNullException:值不能为null.
参数名称:类型
这是我更新的代码:
try { string sql = "SELECT scan_typeclass from scan_types WHERE scan_typeid = " + scan_typeid.ToString(); sqlconn3.Open(); SqlCommand cmd = new SqlCommand(sql, sqlconn3); SqlDataReader drScanClass = cmd.ExecuteReader(); drScanClass.Read(); string scan_class = drScanClass["scan_typeclass"].ToString(); var type = Type.GetType(scan_class); var myObj = Activator.CreateInstance(type, scan_id, scan_name, interval, drActiveServers); } catch (Exception e) { string sSource = "SharedAuditSVC"; string sLog = "Application"; string sEvent = e.ToString(); if (!EventLog.SourceExists(sSource)) EventLog.CreateEventSource(sSource, sLog); EventLog.WriteEntry(sSource, sEvent); EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Warning, 0); }
嗯,我认为这与范围有关,虽然我没有成功通过这种方法调用我的自定义类.我会睡在它上面..