我正在制作一个Windows应用程序。
首先我声明了var,它包含另一个类方法。
var ExtList = ExtTarget.GetExtTargets();
GetExtTargets()就像这样
public static List<ExtTarget> GetExtTargets()
{
var dt = SqlHelper.ExecuteDataTable(QueryHelper.ConnectionString,
@"
SELECT [seq],[SourceKind],[ExtKind],[DBKind],[ConnectionString]
,[FilePath],[TableName],[FileKind],[RowSplitter],[ColumnSplitter]
,[Title],[GroupName],[SyncOrder],[RepeatKind],[RepeatMonth]
,[RepeatDay],[RepeatHour],[RepeatMin],[RepeatWeek],[RepeatWeekNum]
,[LastSyncExecDate]
FROM [ExtTarget]
order by GroupName,SyncOrder");
return dt.Rows.Cast<DataRow>().Select<DataRow, ExtTarget>(a => ExtTarget.RowToModel(a)).ToList();
}
然后,我用它来foreach然后我想将Ext传递给另一个方法的参数。
代码是这样的。
public void ProcessExtSync(object obj)
{
while (IsProcessGoing)
{
Thread.Sleep(ThreadDelay);
if (!IsProcessGoing) return;
var ExtList = ExtTarget.GetExtTargets();
foreach (var Ext in ExtList) // I want to use this Ext as parameter
{
while (IsSourceSyncGoing)
{
Thread.Sleep(ThreadDelay);
}
IsExtSyncGoing = true;
bool ExtSyncForceToRun = ConfigSettingHelper.Instance.IsServiceConfig(Words.ExtSyncForceToRun);
bool ExtSyncForceToRunOnlyError = ConfigSettingHelper.Instance.IsServiceConfig(Words.ExtSyncForceToRunOnlyError);
bool ExtSyncNeedToRun = ConfigSettingHelper.Instance.GetNextExecutingTime(Ext) < DateTime.Now;
if (ExtSyncForceToRun || ExtSyncNeedToRun)
{
//I want to pass Ext as parameter to this method
ServiceProcess.Instance.SyncExt();
if (ExtSyncForceToRun)
{
ConfigSettingHelper.Instance.SetServiceConfig(Words.ExtSyncForceToRun, false);
}
if (ExtSyncForceToRunOnlyError)
{
ConfigSettingHelper.Instance.SetServiceConfig(Words.ExtSyncForceToRunOnlyError, false);
}
}
if (!IsProcessGoing) return;
}
IsExtSyncGoing = false;
}
}
如何修改该代码?请帮帮我。
答案 0 :(得分:3)
var
只是隐式输入变量的快捷方式。它节省了一些打字,但是当读者无法确定类型时,有时会使代码难以阅读。编译器可以找出强类型(或者你会得到一个编译器错误),如果你将它悬停在Visual Studio中,编译器会告诉你实际的类型。
除此之外,您需要做的就是确保要传递变量的方法采用您想要传递它的类型(请记住类型不是ExtTarget
,但在你的情况下,它是public void SyncExt(ExtTarget extTarget)
{
// Implementation code here
}
。
您正在调用的方法应该具有与此类似的签名(尽管它可以返回任何类型):
ServiceProcess.Instance.SyncExt(Ext);
然后在上面的代码中,您可以致电:
apache-storm-1.0.2
hbase-1.2.1
kafka_2.10-0.10.0.0
zookeeper-3.4.9