File: BackEnd\TaskParameterTypeVerifier.cs
Web Access
Project: ..\..\..\src\MSBuildTaskHost\MSBuildTaskHost.csproj (MSBuildTaskHost)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
using System;
using Microsoft.Build.Framework;
 
namespace Microsoft.Build.TaskHost.BackEnd;
 
/// <summary>
/// Provide a class which can verify the correct type for both input and output parameters.
/// </summary>
internal static class TaskParameterTypeVerifier
{
    /// <summary>
    /// Is the parameter type a valid scalar input value.
    /// </summary>
    internal static bool IsValidScalarInputParameter(Type parameterType)
        => parameterType.IsValueType || parameterType == typeof(string) || parameterType == typeof(ITaskItem);
 
    /// <summary>
    /// Is the passed in parameterType a valid vector input parameter.
    /// </summary>
    internal static bool IsValidVectorInputParameter(Type parameterType)
        => (parameterType.IsArray && parameterType.GetElementType().IsValueType) ||
            parameterType == typeof(string[]) ||
            parameterType == typeof(ITaskItem[]);
 
    /// <summary>
    /// Is the passed in value type assignable to an ITask or ITask[] object.
    /// </summary>
    internal static bool IsAssignableToITask(Type parameterType)
        => typeof(ITaskItem[]).IsAssignableFrom(parameterType) || // ITaskItem array or derived type, or
           typeof(ITaskItem).IsAssignableFrom(parameterType);     // ITaskItem or derived type
 
    /// <summary>
    /// Is the passed parameter a valid value type output parameter.
    /// </summary>
    internal static bool IsValueTypeOutputParameter(Type parameterType)
        => (parameterType.IsArray && parameterType.GetElementType().IsValueType) || // array of value types, or
            parameterType == typeof(string[]) ||                                    // string array, or 
            parameterType.IsValueType ||                                            // value type, or
            parameterType == typeof(string);                                        // string
 
    /// <summary>
    /// Is the parameter type a valid scalar or value type input parameter.
    /// </summary>
    internal static bool IsValidInputParameter(Type parameterType)
        => IsValidScalarInputParameter(parameterType) || IsValidVectorInputParameter(parameterType);
 
    /// <summary>
    /// Is the parameter type a valid scalar or value type output parameter.
    /// </summary>
    internal static bool IsValidOutputParameter(Type parameterType)
        => IsValueTypeOutputParameter(parameterType) || IsAssignableToITask(parameterType);
}