有条件地传递命名参数
我有一个结构体,它在构造函数中接受 3 个命名参数...
public struct MyData
{
private readonly double _value1;
private readonly double _value2;
private readonly double _value3;
public MyData(
double value1 = 1.0,
double value2 = 2.0,
double value3 = 3.0)
{
_value1 = value1;
_value2 = value2;
_value3 = value3;
}
}
创建该类的方法调用接收三个可空双精度数,仅当可空双精度数不为空时,我才想用它们来创建 MyData 类...
public MyData CreateMyData(double? value1, double? value2, double? value3)
{
MyData myData;
if (value1.HasValue)
{
if (value2.HasValue)
{
if (value3.HasValue)
{
myData = new MyData(value1, value2, value3);
}
else
{
myData = new MyData(value1, value2);
}
}
else
{
if (value3.HasValue)
{
myData = new MyData(value1, value3: value3);
}
else
{
myData = new MyData(value1);
}
}
}
else
{
if (value2.HasValue)
{
if (value3.HasValue)
{
myData = new MyData(value2: value2, value3: value3);
}
else
{
myData = new MyData(value2: value2);
}
}
else
{
if (value3.HasValue)
{
myData = new MyData(value3: value3);
}
else
{
myData = new MyData();
}
}
}
return myData;
}
是有没有更好的方法来编写此方法而不修改 MyData 类?即我可以有条件地传递命名参数还是可以传递一个指示符来表示默认的命名参数值?
I have a struct which takes 3 named parameters in to the constructor...
public struct MyData
{
private readonly double _value1;
private readonly double _value2;
private readonly double _value3;
public MyData(
double value1 = 1.0,
double value2 = 2.0,
double value3 = 3.0)
{
_value1 = value1;
_value2 = value2;
_value3 = value3;
}
}
The method call that creates the class receives in three nullable doubles which I want to use to create the MyData class only if the nullable doubles are not null...
public MyData CreateMyData(double? value1, double? value2, double? value3)
{
MyData myData;
if (value1.HasValue)
{
if (value2.HasValue)
{
if (value3.HasValue)
{
myData = new MyData(value1, value2, value3);
}
else
{
myData = new MyData(value1, value2);
}
}
else
{
if (value3.HasValue)
{
myData = new MyData(value1, value3: value3);
}
else
{
myData = new MyData(value1);
}
}
}
else
{
if (value2.HasValue)
{
if (value3.HasValue)
{
myData = new MyData(value2: value2, value3: value3);
}
else
{
myData = new MyData(value2: value2);
}
}
else
{
if (value3.HasValue)
{
myData = new MyData(value3: value3);
}
else
{
myData = new MyData();
}
}
}
return myData;
}
Is there a nicer way to write this method without modifying the MyData class? I.e. Can I conditionally pass named parameters or can I pass an indicator to represent the default named parameter value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这样,您可以直接传递
null
而不会受到 组合的影响爆炸。另外,如果您决定更改默认值,即使不重新编译此代码的所有用户,它也可以工作。
This way, you can pass the
null
s directly and won't suffer from the combinatorial explosion.Also, if you ever decide to change the defaults, it would work even without recompiling all users of this code.
检查这个
check this
也许您只想:
?
Maybe you want just:
?
您需要后期绑定才能执行此操作。开源框架 ImpromptuInterface 可让您访问动态选择命名参数。
You need late-binding to do this. The open source framework ImpromptuInterface gives you access to the DLR features needed to dynamically pick named arguments.