对象初始化和“命名构造函数习惯用法”
好的。 所以我有一个值列表,我想做类似以下的事情:
MyObjectValues
.Select(currentItems=>new MyType()
{
Parameter1 = currentItems.Value1,
Parameter2 = currentItems.Value2
});
所以这就是问题。 我需要上面的示例来使用命名构造函数,例如:
MyObjectValues
.Select(currentItems=>MyType.GetNewInstance()
{
Parameter1 = currentItems.Value1,
Parameter2 = currentItems.Value2
});
有什么办法可以做到这一点吗? 基本上,我需要调用一个静态方法来获取对象实例,并且我想按照上面的方式初始化它。
编辑:我目前没有一种简单的方法来修改 MyType 的接口,因此添加新的函数调用(虽然可能是“最佳”方法)目前不太实用。
Ok. So I have a list of values, and I'd like to do something like the following:
MyObjectValues
.Select(currentItems=>new MyType()
{
Parameter1 = currentItems.Value1,
Parameter2 = currentItems.Value2
});
So here's the problem. I need the above example to work with named constructors, such as:
MyObjectValues
.Select(currentItems=>MyType.GetNewInstance()
{
Parameter1 = currentItems.Value1,
Parameter2 = currentItems.Value2
});
Is there any way I can do that? Basically, I have a static method I need to call to get the object instance back, and I'd like to initialize it as above.
EDIT: I don't have an easy way to modify the interface of MyType at present, so adding new function calls (while probably the 'best' approach) isn't very practical at the moment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,C# 3.0 中没有对此直接支持。 仅构造函数调用支持对象初始值设定项。 但是,您可能会考虑构建器模式。 在我的 Protocol Buffers 端口中,我像这样支持它:
所以你的例子将变成:
当然,这意味着编写嵌套的 Builder 类型,但它可以很好地工作。 如果您不介意 MyType 严格来说是可变的,则可以保留不可变的 API,但创建
Builder
实例立即创建MyType
的新实例code>,然后设置属性(因为它可以访问私有成员),最后在Build()
方法中返回MyType
的实例。 (然后它应该“忘记”该实例,以便禁止进一步的突变。)Unfortunately there's no direct support for this in C# 3.0. Object initializers are only supported for constructor calls. However, you might consider the Builder pattern. In my Protocol Buffers port, I support it like this:
So your example would become:
Of course, it means writing the nested Builder type, but it can work quite well. If you don't mind MyType being strictly speaking mutable, you can leave an immutable API but make an instance of
Builder
immediately create a new instance ofMyType
, then set properties as it goes (as it has access to private members), and then finally return the instance ofMyType
in theBuild()
method. (It should then "forget" the instance, so that further mutation is prohibited.)我确信有人会想出一种聪明的方法来在 4.0 之前的 C# 中做到这一点,但我刚刚读过 Sam Ng 关于 C# 4.0 中命名参数的博客文章。 这应该可以解决你的问题。 它看起来像这样:
编辑
忘了提一下,它的有用之处在于您可以设置参数的默认值,因此您不必传递所有参数。 这篇博文是一个很好的简短总结。
I'm sure someone will think of a clever way to do it in pre 4.0 C#, but I just read Sam Ng's blog post on named parameters in C# 4.0. This should solve your problem. It would look like this:
EDIT
Forgot to mention, what makes this useful is that you can set defaults for the parameters, so you don't have to pass them all. The blog post is a good short summary.
更新:工厂怎么样:
Update: how about a factory: