/// <summary>
        /// Create a new TaskParameter
        /// </summary>
        public TaskParameter(object wrappedParameter)
        {
            if (wrappedParameter == null)
            {
                _parameterType    = TaskParameterType.Null;
                _wrappedParameter = null;
                return;
            }

            Type wrappedParameterType = wrappedParameter.GetType();

            if (wrappedParameter is Exception)
            {
                _parameterType    = TaskParameterType.Invalid;
                _wrappedParameter = wrappedParameter;
                return;
            }

            // It's not null or invalid, so it should be a valid parameter type.
            ErrorUtilities.VerifyThrow
            (
                TaskParameterTypeVerifier.IsValidInputParameter(wrappedParameterType) || TaskParameterTypeVerifier.IsValidOutputParameter(wrappedParameterType),
                "How did we manage to get a task parameter that isn't a valid parameter type?"
            );

            if (wrappedParameterType.IsArray)
            {
                if (wrappedParameterType == typeof(string[]))
                {
                    _parameterType    = TaskParameterType.StringArray;
                    _wrappedParameter = wrappedParameter;
                }
                else if (typeof(ITaskItem[]).GetTypeInfo().IsAssignableFrom(wrappedParameterType.GetTypeInfo()))
                {
                    _parameterType = TaskParameterType.ITaskItemArray;
                    ITaskItem[] inputAsITaskItemArray  = (ITaskItem[])wrappedParameter;
                    ITaskItem[] taskItemArrayParameter = new ITaskItem[inputAsITaskItemArray.Length];

                    for (int i = 0; i < inputAsITaskItemArray.Length; i++)
                    {
                        if (inputAsITaskItemArray[i] != null)
                        {
                            taskItemArrayParameter[i] = CreateNewTaskItemFrom(inputAsITaskItemArray[i]);
                        }
                    }

                    _wrappedParameter = taskItemArrayParameter;
                }
                else if (wrappedParameterType.GetElementType().GetTypeInfo().IsValueType)
                {
                    _parameterType    = TaskParameterType.ValueTypeArray;
                    _wrappedParameter = wrappedParameter;
                }
                else
                {
                    ErrorUtilities.ThrowInternalErrorUnreachable();
                }
            }
            else
            {
                // scalar parameter
                if (wrappedParameterType == typeof(string))
                {
                    _parameterType    = TaskParameterType.String;
                    _wrappedParameter = wrappedParameter;
                }
                else if (typeof(ITaskItem).IsAssignableFrom(wrappedParameterType))
                {
                    _parameterType    = TaskParameterType.ITaskItem;
                    _wrappedParameter = CreateNewTaskItemFrom((ITaskItem)wrappedParameter);
                }
                else if (wrappedParameterType.GetTypeInfo().IsValueType)
                {
                    _parameterType    = TaskParameterType.ValueType;
                    _wrappedParameter = wrappedParameter;
                }
                else
                {
                    ErrorUtilities.ThrowInternalErrorUnreachable();
                }
            }
        }
Beispiel #2
0
        public TaskParameter(object wrappedParameter)
        {
            if (wrappedParameter == null)
            {
                _parameterType    = TaskParameterType.Null;
                _wrappedParameter = null;
                return;
            }

            Type wrappedParameterType = wrappedParameter.GetType();

            if (wrappedParameter is Exception)
            {
                _parameterType    = TaskParameterType.Invalid;
                _wrappedParameter = wrappedParameter;
                return;
            }

            // It's not null or invalid, so it should be a valid parameter type.
            ErrorUtilities.VerifyThrow
            (
                TaskParameterTypeVerifier.IsValidInputParameter(wrappedParameterType) || TaskParameterTypeVerifier.IsValidOutputParameter(wrappedParameterType),
                "How did we manage to get a task parameter of type {0} that isn't a valid parameter type?",
                wrappedParameterType
            );

            if (wrappedParameterType.IsArray)
            {
                if (wrappedParameterType == typeof(string[]))
                {
                    _parameterType    = TaskParameterType.StringArray;
                    _wrappedParameter = wrappedParameter;
                }
                else if (typeof(ITaskItem[]).GetTypeInfo().IsAssignableFrom(wrappedParameterType.GetTypeInfo()))
                {
                    _parameterType = TaskParameterType.ITaskItemArray;
                    ITaskItem[] inputAsITaskItemArray  = (ITaskItem[])wrappedParameter;
                    ITaskItem[] taskItemArrayParameter = new ITaskItem[inputAsITaskItemArray.Length];

                    for (int i = 0; i < inputAsITaskItemArray.Length; i++)
                    {
                        if (inputAsITaskItemArray[i] != null)
                        {
                            taskItemArrayParameter[i] = CreateNewTaskItemFrom(inputAsITaskItemArray[i]);
                        }
                    }

                    _wrappedParameter = taskItemArrayParameter;
                }
                else if (wrappedParameterType.GetElementType().GetTypeInfo().IsValueType)
                {
                    _parameterType    = TaskParameterType.ValueTypeArray;
                    _wrappedParameter = wrappedParameter;
                }
                else
                {
                    ErrorUtilities.ThrowInternalErrorUnreachable();
                }
            }
            else
            {
                // scalar parameter
                if (wrappedParameterType == typeof(string))
                {
                    _parameterType    = TaskParameterType.String;
                    _wrappedParameter = wrappedParameter;
                }
                else if (typeof(ITaskItem).IsAssignableFrom(wrappedParameterType))
                {
                    _parameterType    = TaskParameterType.ITaskItem;
                    _wrappedParameter = CreateNewTaskItemFrom((ITaskItem)wrappedParameter);
                }
                // Preserve enums as strings: the enum type itself may not
                // be loaded on the other side of the serialization, but
                // we would convert to string anyway after pulling the
                // task output into a property or item.
                else if (wrappedParameterType.IsEnum)
                {
                    _parameterType    = TaskParameterType.String;
                    _wrappedParameter = (string)Convert.ChangeType(wrappedParameter, typeof(string), CultureInfo.InvariantCulture);
                }
                // Also stringify known common value types, to avoid calling
                // TranslateDotNet when they'll just be stringified on the
                // output side
                else if (wrappedParameterType == typeof(bool))
                {
                    _parameterType    = TaskParameterType.Bool;
                    _wrappedParameter = wrappedParameter;
                }
                else if (wrappedParameterType == typeof(int))
                {
                    _parameterType    = TaskParameterType.Int;
                    _wrappedParameter = wrappedParameter;
                }
                else if (wrappedParameterType.GetTypeInfo().IsValueType)
                {
                    _parameterType    = TaskParameterType.ValueType;
                    _wrappedParameter = wrappedParameter;
                }
                else
                {
                    ErrorUtilities.ThrowInternalErrorUnreachable();
                }
            }
        }