C# 3.0 对象初始化 - 是否有对象正在初始化的通知?

发布于 2024-08-07 20:44:00 字数 348 浏览 9 评论 0原文

我们有几个域对象需要支持只读和读写模式;它们目前有一个用于此目的的 bool Locked 属性 - 当 Locked 尝试更改对象的属性时,会导致 InvalidOperationException。对象的默认状态是锁定。

C# 3 的对象初始化语法引入了一个小问题,即对象必须在初始化期间解锁(或默认解锁),然后在最后显式锁定。

使用 C# 3 的对象初始化语法时,是否有一种方法可以接收对象正在初始化或初始化已完成的通知? System.ComponentModel.ISupportInitialize 是我最大的希望,但它没有被调用。

We have several domain objects which need to support both read-only and read-write modes; they currently have a bool Locked property for this--when Locked attempts to alter properties on the object result in an InvalidOperationException. The default state for the objects is Locked.

The object-initialization syntax of C# 3 introduces a small issue with these, in that the object must be unlocked (or default to be unlocked) during initialization and then locked explicityly at the end.

When using C# 3's object initialization syntax is there a means of receiving notification that the object is being intitialized or that initialization is complete? System.ComponentModel.ISupportInitialize was my best hope, but it doesn't get called.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

屋檐 2024-08-14 20:44:00

您可以使用流畅的 API 并附加它:

var obj = new MyType { Id = 123, Name = "abc"}.Freeze();

其中 Freeze 方法返回相同的实例(流畅) - 类似于:(

class MyType {
    private bool isFrozen;
    public MyType Freeze() {
        isFrozen = true;
        return this;
    }
    protected void ThrowIfFrozen() {
        if (isFrozen) throw new InvalidOperationException("Too cold");
    }
    private int id;
    public int Id {
        get { return id; }
        set { ThrowIfFrozen(); id = value; }
    }
    private string name;
    public string Name {
        get { return name; }
        set { ThrowIfFrozen(); name = value; }
    }
}

如果需要,您可以更加集中检查)

You could use a fluent API and append it:

var obj = new MyType { Id = 123, Name = "abc"}.Freeze();

where the Freeze method returns the same instance (fluent) - something like:

class MyType {
    private bool isFrozen;
    public MyType Freeze() {
        isFrozen = true;
        return this;
    }
    protected void ThrowIfFrozen() {
        if (isFrozen) throw new InvalidOperationException("Too cold");
    }
    private int id;
    public int Id {
        get { return id; }
        set { ThrowIfFrozen(); id = value; }
    }
    private string name;
    public string Name {
        get { return name; }
        set { ThrowIfFrozen(); name = value; }
    }
}

(you could centralize the check a bit more if needed)

记忆之渊 2024-08-14 20:44:00

不,没有这样的通知机制。对象初始值设定项功能将简单地调用指定的构造函数,然后按照列出的顺序设置可访问的字段/属性。没有可用的界面支持此功能的通知。

No, there is no such notification mechanism. The object initializer feature will simply call the specified constructor and then set the accessible fields / properties in the order they are listed. There is no interface available which supports notifications for this feature.

昇り龍 2024-08-14 20:44:00

不。对象初始值设定项只是一个编译器功能,可帮助初始化对象。他们直接调用属性。

您需要强制使用构造函数,或者添加“锁定”方法来显式锁定它们。

No. The object initializers just are a compiler feature to assist in initializing your objects. They call the properties directly.

You need to either force constructor usage, or add a "lock" method to lock them down explicitly.

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