刚接触c#,没看懂这段代码什么意思,invoke起到什么作用,这个foreach循环做了什么,有什么用处,求解
public ResultModule CreateStationOrderPicking(DataTable linedt,string iszlgpla, string itzlgpla, string creater, DateTime created)
{
ResultModule result = new ResultModule();
try
{
try
{
if(String.IsNullOrEmpty(iszlgpla)) iszlgpla = "TPD001";
if (String.IsNullOrEmpty(itzlgpla)) itzlgpla = "TPD002";
(new Mara()).SetMaraDT();
RfcDestination prd = RFCConn.Open();
IRfcFunction qafun = prd.Repository.CreateFunction("Z_PP_FUNC_CREATE_MVT_WS");
qafun.SetValue("CREATER", creater);
string strZGWBQ = "";
IRfcTable itemtable = qafun.GetTable("T_ZMMT006");//接参
foreach (DataRow dr in linedt.Rows)//datajson
{
strZGWBQ = dr["ZGWBQ"].ToString().ToUpper();
itemtable.Append();//将字符串连接到一起
itemtable.SetValue("ZLGPLA", iszlgpla);
itemtable.SetValue("ZTYPE", "PO");
itemtable.SetValue("MATNR", dr["MATNR"].ToString().ToUpper());
itemtable.SetValue("MAKTX", dr["MAKTX"]);
itemtable.SetValue("MENGE", dr["MENGE"].ToString());
itemtable.SetValue("TLGPLA", itzlgpla);
}
if (!String.IsNullOrEmpty(strZGWBQ))
{
qafun.SetValue("I_ZGWBQ", strZGWBQ);
}
qafun.Invoke(prd);
result.result = qafun.GetString("E_ZTRNO");//接参
result.message = qafun.GetString("E_MSG");//接参
}
catch (Exception exception)
{
WriteFileLog.WriteLine(exception.ToString());
}
}
finally
{
RFCConn.Close();
}
return result;
}
因为这里面有好几处自定义的类,只能根据代码大致猜测自定义类的含义和功能。
1、根据你的提问,你大致是了解foreach是针对实现了IEnumerable接口的类型的一种循环访问的语法。使用for循环或者while循环也能实现。foreach循环里看着主要起作用的是 strZGWBQ = dr["ZGWBQ"].ToString().ToUpper();赋值这块,给itemtable类似DataTable的变量循环赋值也是在这里实现的,但是后面代码没看到使用itemtable的地方,着实有些奇怪,不知道是不是代码不全。DataTable里有一个 DataRowCollection类型的属性Rows,它是一个集合,就像数据库里表里多条记录的一个集合,每条记录然后又包含多个字段。所以foreach就是循环Rows集合中的每条记录,并通过每条记录各字段的名称访问得到对应的数据。
2、Invoke看代码就是理解为调用一个方法,去实现某个业务操作,看名称猜测是执行数据创建Order的操作。
VS IDE中,在不明白的符号上点鼠标右键,选转到定义。