在.Net中设计时更改属性序列化的顺序

发布于 2024-08-08 21:24:28 字数 478 浏览 3 评论 0原文

我有一个类,在设计时中继按字母顺序序列化的属性是有问题的。 换句话说,属性 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你与清晨阳光 2024-08-15 21:24:28

这里的最佳实践是重写您的类,以便所有设计时属性彼此独立。这就是 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.

花海 2024-08-15 21:24:28

我是“过度杀伤”解决方案的作者。 :-) 如果您不喜欢基于 CodeDomSerializer 的解决方案,我认为您唯一的选择是设计您的类,以便可以按任何顺序初始化属性(无论如何,这被认为是良好的做法) 。例如,如果这是第一次调用属性 A 的 setter,您是否可以让属性 Z 不清除属性 A?比如:

bool propertyZHasBeenSet=false;
SomeType propertyZ
{
    get {
        //Property getter
    }

    set {
        if(propertyZHasBeenSet) {
            //Clear property A
        } else {
            propertyZHasBeenSet=true;
        }
        //The remaining of the property setter
    }
}

我确信一定有更好的方法来完成同样的事情,但你明白了。

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:

bool propertyZHasBeenSet=false;
SomeType propertyZ
{
    get {
        //Property getter
    }

    set {
        if(propertyZHasBeenSet) {
            //Clear property A
        } else {
            propertyZHasBeenSet=true;
        }
        //The remaining of the property setter
    }
}

I'm sure there must be a better way to accomplish the same thing, but you get the idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文