【发布时间】:2011-09-29 16:42:34
【问题描述】:
对于这个新手问题,我很抱歉,我对 C# 非常陌生,它是语义。我有以下结构:
public struct MT5Command{
public MT5CommandType Type{ get; set;}
public AutoResetEvent ThreadWaitHandle { get; set; }
public object InParam { get; set; }
public object OutParam { get; set; }
}
还有这段代码sn-p:
MT5Command Cmd = new MT5Command();
Cmd.Type = MT5CommandType.GetServerInformation;
Cmd.ThreadWaitHandle = waitHandle.Value;
attributes = new ServerAttributes();
Cmd.OutParam = attributes;
....
ServerAttributes SrvAttributes = new ServerAttributes();
Cmd.OutParam = (ServerAttributes)SrvAttributes;
最后一行无法编译:无法修改“command”的成员,因为它是“foreach 迭代变量” 如何将 OutParam 字段分配给另一个 ServerAttributes 结构?
这是外部 for-each 循环:
foreach (MT5Command Cmd in mCommandQueue.GetConsumingEnumerable())
{
...
}
谢谢,于尔根
【问题讨论】:
-
您需要在代码 sn-p 中包含您的 foreach 循环。但基本上,你正在做类似
foreach (MT5Command foo in bar) { foo.OutParam = ... }的事情。您可以改为使用 for 循环并直接访问您的集合元素,例如。bar[i].