是否可以将 ac# 对象初始值设定项与工厂方法一起使用?
我有一个带有静态工厂方法的类。 我想调用工厂来检索类的实例,然后进行额外的初始化,最好通过 C# 对象初始值设定项语法:
MyClass instance = MyClass.FactoryCreate()
{
someProperty = someValue;
}
vs
MyClass instance = MyClass.FactoryCreate();
instance.someProperty = someValue;
I have a class with a static factory method on it. I want to call the factory to retrieve an instance of the class, and then do additional initialization, preferablly via c# object initializer syntax :
MyClass instance = MyClass.FactoryCreate()
{
someProperty = someValue;
}
vs
MyClass instance = MyClass.FactoryCreate();
instance.someProperty = someValue;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不。或者,您可以接受 lambda 作为参数,这也使您可以完全控制将调用“创建”过程的哪一部分。 通过这种方式,您可以这样调用它:
创建看起来类似于:
另一种选择是返回一个构建器(使用 MyClass 的隐式强制转换运算符)。 您可以这样称呼:
检查构建器的 this
两者其中一些版本在编译时进行检查,并具有完整的智能感知支持。
第三个选项需要默认构造函数:
它介于编译时检查和不检查之间。 它确实需要一些工作,因为它强制在赋值上使用常量表达式。 我认为其他任何内容都是答案中已有方法的变体。 请记住,您也可以使用正常的分配,请考虑您是否确实需要其中任何一个。
No. Alternatively you could accept a lambda as an argument, which also gives you full control in which part of the "creation" process will be called. This way you can call it like:
The create would look similar to:
Another option is to return a builder instead (with an implicit cast operator to MyClass). Which you would call like:
Check this for the builder
Both of these versions are checked at compile time and have full intellisense support.
A third option that requires a default constructor:
Its a middle between checked at compile time and not checked. It does need some work, as it is forcing constant expression on the assignments. I think that anything else are variations of the approaches already in the answers. Remember that you can also use the normal assignments, consider if you really need any of this.
是的。 您可以通过以下技巧对已创建的实例使用对象初始值设定项。 您应该创建一个简单的对象包装器:
现在您可以像这样使用它来初始化您的对象:
PS dotnet 存储库中的相关讨论:
https://github.com/dotnet/csharplang/issues/803
Yes. You can use object initializer for already created instance with the following trick. You should create a simple object wrapper:
And now you can use it like this to initialize your objects:
P.S. Related discussion in dotnet repository:
https://github.com/dotnet/csharplang/issues/803
您可以使用如下扩展方法:
您可以按如下方式调用它:
You can use an extension method such as the following:
You would call it as follows:
“否”+1。
这是匿名对象方式的替代方法:
在这种情况下,
FactoryCreate()
类似于:+1 on "No".
Here's an alternative to the anonymous object way:
In this case
FactoryCreate()
would be something like:不可以,对象初始值设定项只能在使用构造函数调用“new”时使用。 一种选择可能是向工厂方法添加一些额外的参数,以便在工厂内创建对象时设置这些值。
No, the object initializer can only be used on a call to "new" with the constructor. One option might be to add some additional args to your factory method, to set those values at object creation inside the factory.
正如大家所说,没有。
已经建议使用 lambda 作为参数。
更优雅的方法是接受匿名并根据对象设置属性。 即,
这会慢得多,因为必须反映对象的所有属性。
Like everyone said, no.
A lambda as an argument has already been suggested.
A more elegant approach would be to accept an anonymous and set the properties according to the object. i.e.
That would be much slower though, since the object would have to be reflected on for all the properties.
不,那是你只能“内联”做的事情。 工厂函数能为您做的就是返回一个引用。
No, that's something you can only do 'inline'. All the factory function can do for you is to return a reference.