在.Net中设计时更改属性序列化的顺序
我有一个类,在设计时中继按字母顺序序列化的属性是有问题的。 换句话说,属性 Z 必须在属性 A 之前序列化。
问题的出现是因为属性 Z 会在属性 A 发生更改时清除它 - 这发生在 InitializeComponent 中。 我通过设置 FirstTime 标志来解决这个问题,但这看起来很混乱。
我找到了一个解决方案 这里(翻译自西班牙语),但这似乎有点过头了。
处理问题的正确方法是什么?
I have a class where it's problematic to relay on the properties being serialized in alphabetical order at design time.
In other words, property Z must be serialized before property A.
The problem arises because property property Z clears property A whenever it changes - which happens in InitializeComponent.
I work around this problem by setting a FirstTime flag, but that seems messy.
I found a solution here (translated from Spanish), but that seems like over kill.
What's the correct way to handle the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里的最佳实践是重写您的类,以便所有设计时属性彼此独立。这就是 Microsoft 提供的所有可设计类型的工作原理。
我以前曾多次差点陷入这个陷阱,并且我总是通过使有趣的相互依赖关系仅发生在运行时而不是设计时来解决它。
The best practice here is to re-write your class so that all design-time properties are independent of each other. That is how all Microsoft-supplied designable types work.
I have almost fallen into this trap many times before, and I always solved it by making the funny inter-dependencies happen only at run time, not design time.
我是“过度杀伤”解决方案的作者。 :-) 如果您不喜欢基于 CodeDomSerializer 的解决方案,我认为您唯一的选择是设计您的类,以便可以按任何顺序初始化属性(无论如何,这被认为是良好的做法) 。例如,如果这是第一次调用属性 A 的 setter,您是否可以让属性 Z 不清除属性 A?比如:
我确信一定有更好的方法来完成同样的事情,但你明白了。
I am the author of the "overkill" solution. :-) If you don't like the solution based on
CodeDomSerializer
, I thnik that your only option is to design your class so that properties can be initialized in any order (which is considered good practice anyway). Can you for example have property Z to not clear property A if it is the first time that its setter is invoked? Something like:I'm sure there must be a better way to accomplish the same thing, but you get the idea.