对象初始化和“命名构造函数习惯用法”

发布于 2024-07-14 06:09:43 字数 546 浏览 8 评论 0原文

好的。 所以我有一个值列表,我想做类似以下的事情:

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 技术交流群。

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

发布评论

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

评论(3

笛声青案梦长安 2024-07-21 06:09:43

不幸的是,C# 3.0 中没有对此直接支持。 构造函数调用支持对象初始值设定项。 但是,您可能会考虑构建器模式。 在我的 Protocol Buffers 端口中,我像这样支持它:

MyType foo = new MyType.Builder {Parameter1 = 10, Parameter2 = 20}.Build();

所以你的例子将变成:

MyObjectValues.Select(currentItems => new MyType.Builder
{
     Parameter1 = currentItems.Value1,
     Parameter2 = currentItems.Value2
}.Build());

当然,这意味着编写嵌套的 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:

MyType foo = new MyType.Builder {Parameter1 = 10, Parameter2 = 20}.Build();

So your example would become:

MyObjectValues.Select(currentItems => new MyType.Builder
{
     Parameter1 = currentItems.Value1,
     Parameter2 = currentItems.Value2
}.Build());

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 of MyType, then set properties as it goes (as it has access to private members), and then finally return the instance of MyType in the Build() method. (It should then "forget" the instance, so that further mutation is prohibited.)

手长情犹 2024-07-21 06:09:43

我确信有人会想出一种聪明的方法来在 4.0 之前的 C# 中做到这一点,但我刚刚读过 Sam Ng 关于 C# 4.0 中命名参数的博客文章。 这应该可以解决你的问题。 它看起来像这样:

MyObjectValues.Select(currentItems=>MyType.GetNewInstance(
    Parameter1:currentItems.Value1,
    Parameter2:currentItems.Value2));

编辑

忘了提一下,它的有用之处在于您可以设置参数的默认值,因此您不必传递所有参数。 这篇博文是一个很好的简短总结。

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:

MyObjectValues.Select(currentItems=>MyType.GetNewInstance(
    Parameter1:currentItems.Value1,
    Parameter2:currentItems.Value2));

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.

满地尘埃落定 2024-07-21 06:09:43

更新:工厂怎么样:

MyObjectValues.Select(currentItems=>MyTypeFactory.GetNewInstance(currentItems.Value1,
currentItems.Value2));


public static class MyTypeFactory
{
    public static MyType GetNewInstance(
         typeofvalue1 value1, 
         typeofvalue2 value2)
    {
        return new MyType { Parameter1 = value1, Parameter2 = value2 };
    }
}

Update: how about a factory:

MyObjectValues.Select(currentItems=>MyTypeFactory.GetNewInstance(currentItems.Value1,
currentItems.Value2));


public static class MyTypeFactory
{
    public static MyType GetNewInstance(
         typeofvalue1 value1, 
         typeofvalue2 value2)
    {
        return new MyType { Parameter1 = value1, Parameter2 = value2 };
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文