Powershell 4 中的重载方法解析错误

Overloaded method resolution bug in Powershell 4

我正在尝试从 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 个。

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