C#分配/转换为结构中的对象字段(C# Assign/cast to object fields in a struct)

编程入门 行业动态 更新时间:2024-10-28 21:19:10
C#分配/转换为结构中的对象字段(C# Assign/cast to object fields in a struct)

对不起这个新手问题,我对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;

最后一行不能编译:无法修改'command'的成员,因为它是'foreach迭代变量'如何将OutParam字段分配给另一个ServerAttributes结构?

这是每个循环的外部:

foreach (MT5Command Cmd in mCommandQueue.GetConsumingEnumerable()) { ... }

谢谢,Juergen

Sorry for this newbie question, I'm very new to C# and it's semantics. I have the following struct:

public struct MT5Command{ public MT5CommandType Type{ get; set;} public AutoResetEvent ThreadWaitHandle { get; set; } public object InParam { get; set; } public object OutParam { get; set; } }

And this code snippet:

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;

The last line does not compile: Cannot modify members of 'command' because it is a 'foreach iteration variable' How is it possible to assign the OutParam field to another ServerAttributes struct?

This is the outer for-each loop:

foreach (MT5Command Cmd in mCommandQueue.GetConsumingEnumerable()) { ... }

Thanks, Juergen

最满意答案

在这种情况下,你不能。 你还没有给出完整的片段,但我怀疑它是这样的:

foreach (MT5Command command in someCollection) { // Code as posted }

现在因为MT5Command是一个结构体,你可以得到someCollection中任何值的副本 。 改变财产几乎肯定不会做你想做的事情。 编译器通过将command变量设置为只读来保护您不受自己的影响。 你可以这样做:

MT5Command copy = command; copy.OutParam = SrvAttributes;

......但我强烈怀疑这不是你想要的。

作为一个规则,有一个可变的结构像你提出的结构是一个非常糟糕的主意 - 并且听起来不像这应该是一个结构。

了解更多信息:

我关于价值类型和参考类型的文章 关于什么时候适合创建一个结构的MSDN指南

You can't, in this case. You haven't given the full snippet, but I suspect it's something like this:

foreach (MT5Command command in someCollection) { // Code as posted }

Now because MT5Command is a struct, you're getting a copy of whatever value is in someCollection. Changing the property almost certainly wouldn't do what you wanted it to anyway. The compiler is protecting you from yourself, by making the command variable read-only. You could do this:

MT5Command copy = command; copy.OutParam = SrvAttributes;

... but I strongly suspect that's not what you want.

As a rule, it's a really bad idea to have mutable structs like the one you've come up with - and it doesn't sound like that should be a struct in the first place.

For more information:

My article on value types and reference types MSDN guidelines on when it's appropriate to create a struct

更多推荐

本文发布于:2023-08-05 04:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1427048.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   转换为   分配   对象   结构

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!