Powershell 4中的方法解析错误

时间:2018-10-05 14:11:03

标签: .net powershell powershell-v4.0

我正在尝试从Powershell脚本调用TfvcHttpClient.GetItemsAsync()

$Full = [Microsoft.TeamFoundation.SourceControl.WebApi.VersionControlRecursionType]::Full
$None = [System.Threading.CancellationToken]::None
$Items = $Cli.GetItemsAsync($TFVCPath, $Full, $false, $null, $null, $None).GetAwaiter().GetResult()

在Powershell 4中,它会出错:

##[error]System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at System.Management.Automation.Adapter.CompareOverloadCandidates(OverloadCandidate candidate1, OverloadCandidate candidate2, Object[] arguments)
   at System.Management.Automation.Adapter.FindBestCandidate(IEnumerable`1 candidates, Object[] arguments)
   at System.Management.Automation.Adapter.FindBestCandidate(IEnumerable`1 candidates, Object[] arguments, PSMethodInvocationConstraints invocationConstraints)
   at System.Management.Automation.Adapter.FindBestMethod(MethodInformation[] methods, PSMethodInvocationConstraints invocationConstraints, Object[] arguments, String& errorId, String& errorMsg, Boolean& expandParamsOnBest)
   at System.Management.Automation.Language.PSInvokeMemberBinder.InvokeDotNetMethod(DynamicMetaObject target, DynamicMetaObject[] args, BindingRestrictions restrictions, MethodInformation[] mi, Type errorExceptionType)
   at System.Management.Automation.Language.PSInvokeMemberBinder.FallbackInvokeMember(DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion)
   at System.Dynamic.DynamicMetaObject.BindInvokeMember(InvokeMemberBinder binder, DynamicMetaObject[] args)
   at System.Dynamic.DynamicMetaObjectBinder.Bind(Object[] args, ReadOnlyCollection`1 parameters, LabelTarget returnLabel)
   at System.Runtime.CompilerServices.CallSiteBinder.BindCore[T](CallSite`1 site, Object[] args)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute7[T0,T1,T2,T3,T4,T5,T6,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6)
   at System.Management.Automation.Interpreter.DynamicInstruction``8.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

看起来,重载解决方案的逻辑是错误的。该方法确实是重载的,但是只有一个带有6个参数的重载。另外两个有7。

Powershell 4支持省略具有默认值的方法参数(只要这些参数是null),因此重载解决方案逻辑仍然适用。但是,我的调用不是模棱两可的-没有其他将VersionControlRecursionType作为第二个参数的重载。

任何想法如何解决此问题?也许有一种方法可以告诉Powershell调用特定的重载?

他们已经在PS5中修复了此问题,但仍然...


编辑:在一个干净的DLL上复制。这是DLL代码,再现了令人反感的方法签名:

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.TeamFoundation.SourceControl.WebApi
{
    public enum VersionControlRecursionType
    {
        None = 0, OneLevel = 1, OneLevelPlusNestedEmptyFolders = 4, Full = 120
    }
    public class TfvcItem { }
    public class TfvcVersionDescriptor { }

    public abstract class TfvcHttpClientBase
    {
        public TfvcHttpClientBase() { }

        public virtual Task<List<TfvcItem>> GetItemsAsync(string project, string scopePath = null, VersionControlRecursionType? recursionLevel = null, bool? includeLinks = null, TfvcVersionDescriptor versionDescriptor = null, object userState = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            return null;
        }
        public virtual Task<List<TfvcItem>> GetItemsAsync(Guid project, string scopePath = null, VersionControlRecursionType? recursionLevel = null, bool? includeLinks = null, TfvcVersionDescriptor versionDescriptor = null, object userState = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            return null;
        }
        public virtual Task<List<TfvcItem>> GetItemsAsync(string scopePath = null, VersionControlRecursionType? recursionLevel = null, bool? includeLinks = null, TfvcVersionDescriptor versionDescriptor = null, object userState = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            return null;
        }
    }


    public class TfvcHttpClient : TfvcHttpClientBase
    {
        public TfvcHttpClient() { }
    }
}

这是演示行为的Powershell:

Add-Type -Path "Foo.dll"
$Cli = New-Object Microsoft.TeamFoundation.SourceControl.WebApi.TfvcHttpClient
$Full = [Microsoft.TeamFoundation.SourceControl.WebApi.VersionControlRecursionType]::Full
$None = [System.Threading.CancellationToken]::None
$Items = $Cli.GetItemsAsync("", $Full, $false, $null, $null, $None)

我也已经在调试器中捕获到异常。令人反感的方法位于System.Management.Automation.dll中。描述重载方法的candidate参数包含两个数组-argumentsconversionRanks。代码期望它们具有相同的长度,但是前者有7个元素,后者有6个元素。

1 个答案:

答案 0 :(得分:0)

以GUID作为第一个参数的7参数方法重载不会以这种特殊方式出错。 Powershell 4仍然存在方法解析错误:)