传递字符串 [] 时无法调用正确的 .NET 重载
Unable to call correct .NET overload when passing string[]
我有一个要从 powershell 调用的方法,它是
Workspace.GetPendingChanges(string[] items)
这个方法有多个重载,但让我生活困难的是:
Workspace.GetPendingChanges(string items)
Workspace.GetPendingChanges(string[] items)
Workspace.GetPendingChanges(ItemSpec[] itemspecs)
我正在使用来自 TFS 2015 的新构建任务的 Find-Files
cmdlet 获取文件列表:
if ($ItemSpec.Contains("*") -Or $ItemSpec.Contains("?"))
{
Write-Verbose "Pattern found in solution parameter. Calling Find-Files."
Write-Verbose "Calling Find-Files with pattern: $ItemSpec"
[string[]] $FilesToCheckin = @(Find-Files -SearchPattern $ItemSpec -RootFolder $env:BUILD_SOURCESDIRECTORY)
Write-Verbose "Found files: $FilesToCheckin"
}
else
{
Write-Verbose "No Pattern found in solution parameter."
[string[]] $FilesToCheckin = @($ItemSpec)
}
然后我调用:
$pendingChanges = $provider.Workspace.GetPendingChanges( @($FilesToCheckin))
我试过任何版本的强制转换 [string[]]
强制数组表示法 @()
我知道,但是 find-files 的结果在传递到正确的重载:
System.Management.Automation.MethodInvocationException: Exception calling "GetPendingChanges" with "1" argument(s): "TF400889: The following path contains more than the allowed 259 characters:
这是生成的"long string"
C:\TfsData\Build\_work\s\BuildProcessTemplates\AzureContinuousDeployment.11.xaml C:\TfsData\Build\_work\s\BuildProcessTemplates\DefaultTemplate.11.1.xaml C:\TfsData\Build\_work\s\BuildProcessTemplates\LabDefaultTemplate.11.xaml C:\TfsData\Build\_work\s\BuildProcessTemplates\UpgradeTemplate.xaml C:\TfsData\Build\_work\s\checkin-changes.ps1 C:\TfsData\Build\_work\s\update.txt C:\TfsData\Build\_work\s\updatefile.ps1.
Specify a shorter path." ---> Microsoft.TeamFoundation.InvalidPathException: TF400889: The following path contains more than the allowed 259 characters: C:\TfsData\Build\_work\s\BuildProcessTemplates\AzureContinuousDeployment.11.xaml C:\TfsData\Build\_work\s\BuildProcessTemplates\DefaultTemplate.11.1.xaml C:\TfsData\Build\_work\s\BuildProcessTemplates\LabDefaultTemplate.11.xaml ...
at System.IO.PathHelper.GetFullPathName()
at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths)
at System.IO.Path.GetFullPathInternal(String path)
at Microsoft.TeamFoundation.Common.FileSpec.GetFullPathWrapper(String path)
--- End of inner exception stack trace ---
at Microsoft.TeamFoundation.Common.FileSpec.GetFullPathWrapper(String path)
at Microsoft.TeamFoundation.Common.FileSpec.GetFullPath(String path, Boolean checkForIllegalDollar)
at Microsoft.TeamFoundation.VersionControl.Common.VersionControlUtil.GetFullPath(String item, PathLength maxServerPathLength)
at Microsoft.TeamFoundation.VersionControl.Client.ItemSpec..ctor(String item, RecursionType recursionType, Int32 deletionId)
at Microsoft.TeamFoundation.VersionControl.Client.ItemSpec.FromStrings(String[] paths, RecursionType recursion)
at
这表明它正在调用正确的重载
Microsoft.TeamFoundation.VersionControl.Client.Workspace.GetPendingChanges(String[] items, RecursionType recursion, Boolean includeDownloadInfo)
cmdlet returns List<string>
以下:
protected override void ProcessRecord()
{
base.ProcessRecord();
if (!this.IncludeFiles && !this.IncludeFolders)
{
this.IncludeFiles = true;
}
List<string> sendToPipeline = FindFiles.FindMatchingFiles(this.RootFolder, this.SearchPattern, this.IncludeFiles, this.IncludeFolders);
base.WriteObject(sendToPipeline, true);
}
为了确保我从对 Find-Files
的调用中得到一个列表,我添加了:
Write-Verbose "Found files: " -Verbose
Write-Verbose $FilesToCheckin.Count -Verbose
其中 returns:7,符合预期。
我的问题
如何防止 Powershell 在将 find-files
的结果传递给 GetPendingChanges
之前将其转换为一个长字符串?
项目可用,损坏的代码在底部注释掉:
我不知道为什么会这样,但是在添加 @()
并在所有地方强制转换为 [string[]]
并删除 Write-Verbose $FilesToCheckin
之后它起作用了:
if ($ItemSpec.Contains("*") -Or $ItemSpec.Contains("?"))
{
Write-Verbose "Pattern found in solution parameter. Calling Find-Files."
Write-Verbose "Calling Find-Files with pattern: $ItemSpec"
[string[]] $FilesToCheckin = @(Find-Files -SearchPattern $ItemSpec -RootFolder $env:BUILD_SOURCESDIRECTORY)
}
else
{
Write-Verbose "No Pattern found in solution parameter."
[string[]] $FilesToCheckin = @($ItemSpec)
}
$pendingChanges = $provider.Workspace.GetPendingChanges( [string[]] @($FilesToCheckin) )
您是否尝试过通过反射调用方法来强制正确重载?类似于:
[Object[]] $parameters = @($stringArray)
$methods = $provider.Workspace.GetType().GetMethods() | where {$_.Name -eq "GetPendingChanges" -and $_.GetParameters()[0].ParameterType.ToString() -eq "System.String[]" } | Select-Object $_
$methods[0].Invoke($provider.Workspace, $parameters);
我有一个要从 powershell 调用的方法,它是
Workspace.GetPendingChanges(string[] items)
这个方法有多个重载,但让我生活困难的是:
Workspace.GetPendingChanges(string items)
Workspace.GetPendingChanges(string[] items)
Workspace.GetPendingChanges(ItemSpec[] itemspecs)
我正在使用来自 TFS 2015 的新构建任务的 Find-Files
cmdlet 获取文件列表:
if ($ItemSpec.Contains("*") -Or $ItemSpec.Contains("?"))
{
Write-Verbose "Pattern found in solution parameter. Calling Find-Files."
Write-Verbose "Calling Find-Files with pattern: $ItemSpec"
[string[]] $FilesToCheckin = @(Find-Files -SearchPattern $ItemSpec -RootFolder $env:BUILD_SOURCESDIRECTORY)
Write-Verbose "Found files: $FilesToCheckin"
}
else
{
Write-Verbose "No Pattern found in solution parameter."
[string[]] $FilesToCheckin = @($ItemSpec)
}
然后我调用:
$pendingChanges = $provider.Workspace.GetPendingChanges( @($FilesToCheckin))
我试过任何版本的强制转换 [string[]]
强制数组表示法 @()
我知道,但是 find-files 的结果在传递到正确的重载:
System.Management.Automation.MethodInvocationException: Exception calling "GetPendingChanges" with "1" argument(s): "TF400889: The following path contains more than the allowed 259 characters:
这是生成的"long string"
C:\TfsData\Build\_work\s\BuildProcessTemplates\AzureContinuousDeployment.11.xaml C:\TfsData\Build\_work\s\BuildProcessTemplates\DefaultTemplate.11.1.xaml C:\TfsData\Build\_work\s\BuildProcessTemplates\LabDefaultTemplate.11.xaml C:\TfsData\Build\_work\s\BuildProcessTemplates\UpgradeTemplate.xaml C:\TfsData\Build\_work\s\checkin-changes.ps1 C:\TfsData\Build\_work\s\update.txt C:\TfsData\Build\_work\s\updatefile.ps1.
Specify a shorter path." ---> Microsoft.TeamFoundation.InvalidPathException: TF400889: The following path contains more than the allowed 259 characters: C:\TfsData\Build\_work\s\BuildProcessTemplates\AzureContinuousDeployment.11.xaml C:\TfsData\Build\_work\s\BuildProcessTemplates\DefaultTemplate.11.1.xaml C:\TfsData\Build\_work\s\BuildProcessTemplates\LabDefaultTemplate.11.xaml ...
at System.IO.PathHelper.GetFullPathName()
at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths)
at System.IO.Path.GetFullPathInternal(String path)
at Microsoft.TeamFoundation.Common.FileSpec.GetFullPathWrapper(String path)
--- End of inner exception stack trace ---
at Microsoft.TeamFoundation.Common.FileSpec.GetFullPathWrapper(String path)
at Microsoft.TeamFoundation.Common.FileSpec.GetFullPath(String path, Boolean checkForIllegalDollar)
at Microsoft.TeamFoundation.VersionControl.Common.VersionControlUtil.GetFullPath(String item, PathLength maxServerPathLength)
at Microsoft.TeamFoundation.VersionControl.Client.ItemSpec..ctor(String item, RecursionType recursionType, Int32 deletionId)
at Microsoft.TeamFoundation.VersionControl.Client.ItemSpec.FromStrings(String[] paths, RecursionType recursion)
at
这表明它正在调用正确的重载
Microsoft.TeamFoundation.VersionControl.Client.Workspace.GetPendingChanges(String[] items, RecursionType recursion, Boolean includeDownloadInfo)
cmdlet returns List<string>
以下:
protected override void ProcessRecord()
{
base.ProcessRecord();
if (!this.IncludeFiles && !this.IncludeFolders)
{
this.IncludeFiles = true;
}
List<string> sendToPipeline = FindFiles.FindMatchingFiles(this.RootFolder, this.SearchPattern, this.IncludeFiles, this.IncludeFolders);
base.WriteObject(sendToPipeline, true);
}
为了确保我从对 Find-Files
的调用中得到一个列表,我添加了:
Write-Verbose "Found files: " -Verbose
Write-Verbose $FilesToCheckin.Count -Verbose
其中 returns:7,符合预期。
我的问题
如何防止 Powershell 在将 find-files
的结果传递给 GetPendingChanges
之前将其转换为一个长字符串?
项目可用,损坏的代码在底部注释掉:
我不知道为什么会这样,但是在添加 @()
并在所有地方强制转换为 [string[]]
并删除 Write-Verbose $FilesToCheckin
之后它起作用了:
if ($ItemSpec.Contains("*") -Or $ItemSpec.Contains("?"))
{
Write-Verbose "Pattern found in solution parameter. Calling Find-Files."
Write-Verbose "Calling Find-Files with pattern: $ItemSpec"
[string[]] $FilesToCheckin = @(Find-Files -SearchPattern $ItemSpec -RootFolder $env:BUILD_SOURCESDIRECTORY)
}
else
{
Write-Verbose "No Pattern found in solution parameter."
[string[]] $FilesToCheckin = @($ItemSpec)
}
$pendingChanges = $provider.Workspace.GetPendingChanges( [string[]] @($FilesToCheckin) )
您是否尝试过通过反射调用方法来强制正确重载?类似于:
[Object[]] $parameters = @($stringArray)
$methods = $provider.Workspace.GetType().GetMethods() | where {$_.Name -eq "GetPendingChanges" -and $_.GetParameters()[0].ParameterType.ToString() -eq "System.String[]" } | Select-Object $_
$methods[0].Invoke($provider.Workspace, $parameters);