1 // Copyright (c) Microsoft. All rights reserved. 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information. 3 //----------------------------------------------------------------------- 4 // </copyright> 5 // <summary>TaskParameterTypeVerifier verifies the correct type for both input and output parameters.</summary> 6 //----------------------------------------------------------------------- 7 8 using System; 9 using Microsoft.Build.Framework; 10 using System.Reflection; 11 using Microsoft.Build.Shared; 12 13 namespace Microsoft.Build.BackEnd 14 { 15 /// <summary> 16 /// Provide a class which can verify the correct type for both input and output parameters. 17 /// </summary> 18 internal static class TaskParameterTypeVerifier 19 { 20 /// <summary> 21 /// Is the parameter type a valid scalar input value 22 /// </summary> IsValidScalarInputParameter(Type parameterType)23 internal static bool IsValidScalarInputParameter(Type parameterType) 24 { 25 bool result = (parameterType.GetTypeInfo().IsValueType || parameterType == typeof(string) || parameterType == typeof(ITaskItem)); 26 return result; 27 } 28 29 /// <summary> 30 /// Is the passed in parameterType a valid vector input parameter 31 /// </summary> IsValidVectorInputParameter(Type parameterType)32 internal static bool IsValidVectorInputParameter(Type parameterType) 33 { 34 bool result = (parameterType.IsArray && parameterType.GetElementType().GetTypeInfo().IsValueType) || 35 parameterType == typeof(string[]) || 36 parameterType == typeof(ITaskItem[]); 37 return result; 38 } 39 40 /// <summary> 41 /// Is the passed in value type assignable to an ITask or Itask[] object 42 /// </summary> IsAssignableToITask(Type parameterType)43 internal static bool IsAssignableToITask(Type parameterType) 44 { 45 bool result = typeof(ITaskItem[]).GetTypeInfo().IsAssignableFrom(parameterType.GetTypeInfo()) || /* ITaskItem array or derived type, or */ 46 typeof(ITaskItem).IsAssignableFrom(parameterType); /* ITaskItem or derived type */ 47 return result; 48 } 49 50 /// <summary> 51 /// Is the passed parameter a valid value type output parameter 52 /// </summary> IsValueTypeOutputParameter(Type parameterType)53 internal static bool IsValueTypeOutputParameter(Type parameterType) 54 { 55 bool result = (parameterType.IsArray && parameterType.GetElementType().GetTypeInfo().IsValueType) || /* array of value types, or */ 56 parameterType == typeof(string[]) || /* string array, or */ 57 parameterType.GetTypeInfo().IsValueType || /* value type, or */ 58 parameterType == typeof(string); /* string */ 59 return result; 60 } 61 62 /// <summary> 63 /// Is the parameter type a valid scalar or value type input parameter 64 /// </summary> IsValidInputParameter(Type parameterType)65 internal static bool IsValidInputParameter(Type parameterType) 66 { 67 return IsValidScalarInputParameter(parameterType) || IsValidVectorInputParameter(parameterType); 68 } 69 70 /// <summary> 71 /// Is the parameter type a valid scalar or value type output parameter 72 /// </summary> IsValidOutputParameter(Type parameterType)73 internal static bool IsValidOutputParameter(Type parameterType) 74 { 75 return IsValueTypeOutputParameter(parameterType) || IsAssignableToITask(parameterType); 76 } 77 } 78 }