對不起,這個新手問題,我是C#的新手,它的語義。我有以下結構:C#分配/轉換到結構中的對象字段
public struct MT5Command{
public MT5CommandType Type{ get; set;}
public AutoResetEvent ThreadWaitHandle { get; set; }
public object InParam { get; set; }
public object OutParam { get; set; }
}
而這個代碼片段:
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;
最後一行不會編譯:不能修改的「命令」成員,因爲它是一個「的foreach迭代變量」 如何是否可以將OutParam字段分配給另一個ServerAttributes結構?
這是外for-each循環:
foreach (MT5Command Cmd in mCommandQueue.GetConsumingEnumerable())
{
...
}
感謝,克林斯曼
您需要在代碼段的foreach循環。但基本上,你正在做'foreach(MT5Command foo in bar){foo.OutParam = ...}'。您可以改爲使用for-loop並直接訪問您的集合元素,例如。 '杆[I]'。 – ashes999