当使用PowerShell时,为什么我不能使用TimeSpan结构的重载的ToString() 方法 ?
$ts = New-TimeSpan -Days 5 $ts.ToString("g") Cannot find an overload for "ToString" and the argument count: "1". At line:1 char:13 + $ts.ToString <<<< ("g") + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodCountCouldNotFindBest
有没有解决方法?
我想现在发生的事情是,你需要.NET 4版本的TimeSpan.ToString() ,它有2个重载,并且你得到了.NET 3.5版本的TimeSpan.ToString() ,它没有任何重载。 如果您在StackOverflow上查看此问题,则会显示如何让PS使用.NET 4程序集。
[添加]通过上面的链接创buildconfiguration文件后,我能够得到以下成功执行:
PS C:\Scripts > add-type -Path "C:\Windows\Microsoft.NET\framework64\v4.0.30319\mscorlib.dll" PS C:\Scripts > $ts = New-Object -Type System.TimeSpan PS C:\Scripts > $ts.Tostring("g") 0:00:00