C# 对象初始值设定项和集合

发布于 2024-09-11 07:55:11 字数 1115 浏览 2 评论 0原文

我正在尝试设置一个类,以便可以使用对象初始值设定项对其进行初始化,但它包含一些集合。理想情况下,我希望客户端代码能够执行以下操作:

MyClass myObj = new MyClass
{
  Name = "Name",
  Contents = new[]
  {
    "Item1",
    "Item2"
  }
}

但是,其中 Contents 需要是 BindingList。底层字段存储对此列表的只读引用,我希望设置器本质上执行 Clear 操作,然后执行 AddRange 操作来设置内容。

我无法将 Contents 属性设为 IEnumerable,因为客户端代码看不到 Add 方法等,至少在没有先施放它的情况下是这样。我无法将其设为 BindingList 因为如果我设置它,我需要构造一个新的绑定列表来传递给它..这可能是可能的,但我不想引入仅出于将其传递给属性设置器的目的而构造新的 BindingList 效率低下。

理想的做法是让 getter 返回 BindingList 并且 setter 接受 IEnumerable,但 C# 不允许 getters /setters 属性具有不同的类型。

哦,在 BindingListIEnumerable 之间隐式转换是不允许的,所以我也不能这样做(链接)。

有什么办法解决这个问题吗?

I'm trying to set up a class so that it's possible to initialize it using an object initializer, but it contains some collections. Ideally I'd like client code to be able to do:

MyClass myObj = new MyClass
{
  Name = "Name",
  Contents = new[]
  {
    "Item1",
    "Item2"
  }
}

However, where Contents needs to be a BindingList<string>. The underlying field stores a readonly reference to this list, and I'd like the setter to essentially do a Clear followed by AddRange to set the contents.

I can't make the Contents property an IEnumerable<string>, because client code wouldn't see the Add method, among many others, at least not without casting it first. I can't make it a BindingList<string> because if I set it, I need to construct a new binding list to pass to it.. this might be possible but I'd rather not introduce the inefficiency of construct a new BindingList<string> solely for the purpose of passing it to the property setter.

The ideal thing to be able to do would be to have the getter return a BindingList<string> and the setter accept IEnumerable<string>, but C# doesn't allow getters/setters on a property to have different types.

Oh, and implicitly casting between BindingList<string> and IEnumerable<string> is a no-no, so I can't do that either (Link).

Is there any way around this?

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

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

发布评论

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

评论(3

寂寞清仓 2024-09-18 07:55:11

C# 初始值设定项语法将自动调用属性集合对象上的 Add 方法。当然,这不会事先调用 Reset(),但此时该对象仍然是空的,所以这并不重要。

替换列表是否必须使用属性集语法?让 setter 替换集合的内容而不实际更改集合对象标识是非常意外的,并且可能会导致错误。

C# initializer syntax will automatically call the Add method on your property's collection object. That won't call Reset() beforehand of course, but the object is still empty at that point, so it doesn't matter.

Does replacing the list have to use property set syntax? Having a setter replace the content of a collection without actually changing the collection object identity is very unexpected and will likely lead to bugs.

岛徒 2024-09-18 07:55:11

创建派生自 BindingList 的自定义集合类,并添加来自 string[] 类型的隐式转换

Create a custom collection class that derives from BindingList<string> and add an implicit cast from type string[]

带上头具痛哭 2024-09-18 07:55:11

我建议封装 BindingList。在这种情况下,请回到创建对象的老式方法,这样就不会创建不必要的耦合。优先考虑良好的面向对象而不是语言约定。

I would recommed encapsulating the BindingList. In this situation go back to the old school way of creating objects so that you aren't creating unnecessary couplings. Favor good OO over language conventions.

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