是否可以返回调用者无法修改的只读 ObservableCollection?

发布于 2024-11-15 13:02:23 字数 360 浏览 2 评论 0原文

考虑以下代码:

public ObservableCollection<Message> Messages {
   get { return new ObservableCollection<Message>(); }
}

我可以采取什么措施来防止调用者代码添加/删除/更改项目?

编辑:抱歉,大家,我刚刚注意到这是一个骗局,但接受的答案是错误的。

Consider the following code:

public ObservableCollection<Message> Messages {
   get { return new ObservableCollection<Message>(); }
}

Is there anything I can do to prevent the caller code from adding/removing/changing items?

EDIT: Sorry, everyone, I just noticed it's a dupe, but the accepted answer is wrong.

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

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

发布评论

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

评论(4

痴情 2024-11-22 13:02:23

使用 ReadOnlyObservableCollection

代码会是:

public ReadOnlyObservableCollection<Message> Messages 
{
  get { return new ReadOnlyObservableCollection<Message>(new ObservableCollection<Message>()); }
}

阅读关于ReadOnlyObservableCollection 反模式 我意识到上述方法不是一个好方法,可能会导致微妙的错误。建议的方法是:

public class MyClass
{
  public MyClass()
  {
    myCollection = new ObservableCollection<Message>();
    myReadOnlyCollection = new ReadOnlyObservableCollection<Message>(myCollection);
  }

  public ReadOnlyObservableCollection<Message> Messages 
  {
    get { return myReadOnlycollection; }
  }

  private readonly ObservableCollection<Message> myCollection;
  private readonly ReadOnlyObservableCollection<Message> myReadOnlyCollection;
}

What about using ReadOnlyObservableCollection

Code would be:

public ReadOnlyObservableCollection<Message> Messages 
{
  get { return new ReadOnlyObservableCollection<Message>(new ObservableCollection<Message>()); }
}

Having read aboutReadOnlyObservableCollection Anti Pattern I realise the above is the not a good approach and may lead to subtle bugs. The suggested approach is:

public class MyClass
{
  public MyClass()
  {
    myCollection = new ObservableCollection<Message>();
    myReadOnlyCollection = new ReadOnlyObservableCollection<Message>(myCollection);
  }

  public ReadOnlyObservableCollection<Message> Messages 
  {
    get { return myReadOnlycollection; }
  }

  private readonly ObservableCollection<Message> myCollection;
  private readonly ReadOnlyObservableCollection<Message> myReadOnlyCollection;
}
抽个烟儿 2024-11-22 13:02:23

为什么需要只读 ObservableCollection。
如果该集合是只读的,则无需观察它。
考虑一个集合

Why do you need a read only ObservableCollection.
If this collection is read only you don't need to observe it.
Consider a Collection<T>

抱着落日 2024-11-22 13:02:23

返回一个ReadOnlyObservableCollection

private ReadOnlyObservableCollection<Message> _Messages;

public ReadOnlyObservableCollection<Message> Messages {
   get { this._Messages = this._Messages ?? new ReadOnlyObservableCollection(new ObservableCollection<Message>()); return this._Messages; }
}

Return a ReadOnlyObservableCollection instead:

private ReadOnlyObservableCollection<Message> _Messages;

public ReadOnlyObservableCollection<Message> Messages {
   get { this._Messages = this._Messages ?? new ReadOnlyObservableCollection(new ObservableCollection<Message>()); return this._Messages; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文