[英]What is the best way to get a multiple of a TimeSpan
There is an operation in a program that will take up to 'n' times longer than another operation (so I need to wait 'n' times longer before failing). 程序中有一个操作比另一个操作要花费'n'倍的时间(因此我需要等待'n'倍的时间才能失败)。 The timeout for the other operation is saved as a TimeSpan.
其他操作的超时保存为TimeSpan。 How do I get (OtherOperation.TimeSpan * 'n')?
如何获取(OtherOperation.TimeSpan *'n')?
You might consider writing an extension method, such as: 您可以考虑编写扩展方法,例如:
public static class TimeSpanEx
{
public static TimeSpan MultiplyBy (this TimeSpan t, int multiplier)
{
return new TimeSpan(t.Ticks * multiplier);
}
}
Now you can simply call: 现在您可以简单地致电:
TimeSpan result = yourtimespan.MultiplyBy(3);
As an aside, it would be really nice if one could just overload the *
operator in an extension method, but this is currently not possible . 顺便说一句,如果只是可以在扩展方法中重载
*
运算符,那将是非常不错的选择,但这目前是不可能的 。
Are we talking .net? 我们在说.net吗?
If so use create a new timespan using timespan.frommilliseconds and pass in the original .totalmilliseconds value multiplied by your constant. 如果是这样,请使用timepan.frommilliseconds创建一个新的时间跨度,并将原始.totalmilliseconds值乘以您的常数。
Hope that helps 希望能有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.