Ejemplo n.º 1
0
        /// <summary>
        /// Returns the specified operation.
        /// </summary>
        /// <param name="method">The method.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="source">The source.</param>
        /// <param name="target">The target.</param>
        /// <returns>The operation of the specified method.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// The method is null.
        /// or
        /// The source is null.
        /// or
        /// The method requires parameters which are not specified.
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// The specified source type is not supported by the method.
        /// or
        /// The specified method is not available.
        /// or
        /// The parameters do not contain a required parameter value.
        /// or
        /// The type of a parameter does not match the type specified by the method.
        /// or
        /// The value of a parameter is not within the expected range.
        /// or
        /// The specified source and result are the same objects, but the method does not support in-place operations.
        /// </exception>
        public IOperation GetOperation(OperationMethod method, IDictionary <OperationParameter, Object> parameters, Object source, Object target)
        {
            if (method == null)
            {
                throw new ArgumentNullException("method", "The method is null.");
            }
            if (source == null)
            {
                throw new ArgumentNullException("source", "The source is null.");
            }
            if (source.GetType().Equals(method.SourceType) && !source.GetType().IsSubclassOf(method.SourceType) && !source.GetType().GetInterfaces().Contains(method.SourceType))
            {
                throw new ArgumentException("The specified source type is not supported by the method.", "source");
            }

            Type operationType;

            if (!_operations.TryGetValue(method, out operationType))
            {
                throw new ArgumentException("The specified method is not available.", "method");
            }

            return(Activator.CreateInstance(operationType, source, target, parameters) as IOperation);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Executes the specified operation.
        /// </summary>
        /// <param name="method">The operation method.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="source">The source.</param>
        /// <param name="target">The target.</param>
        /// <returns>The result of the operation.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// The method is null.
        /// or
        /// The source is null.
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// The specified source type is not supported by the method.
        /// or
        /// The specified method is not available.
        /// </exception>
        public void ExecuteOperation(OperationMethod method, IDictionary <OperationParameter, Object> parameters, Object source, Object target)
        {
            IOperation operation = GetOperation(method, parameters, source, target);

            operation.Execute();
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Returns the specified operation.
 /// </summary>
 /// <param name="method">The method.</param>
 /// <param name="parameters">The parameters.</param>
 /// <param name="source">The source.</param>
 /// <returns>The operation of the specified method.</returns>
 /// <exception cref="System.ArgumentNullException">
 /// The method is null.
 /// or
 /// The source is null.
 /// or
 /// The method requires parameters which are not specified.
 /// </exception>
 /// <exception cref="System.ArgumentException">
 /// The specified source type is not supported by the method.
 /// or
 /// The specified method is not available.
 /// or
 /// The parameters do not contain a required parameter value.
 /// or
 /// The type of a parameter does not match the type specified by the method.
 /// or
 /// The value of a parameter is not within the expected range.
 /// </exception>
 public IOperation GetOperation(OperationMethod method, IDictionary <OperationParameter, Object> parameters, Object source)
 {
     return(GetOperation(method, parameters, source, null));
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Operation{SourceType, ResultType}" /> class.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="target">The target.</param>
        /// <param name="method">The method.</param>
        /// <param name="parameters">The parameters.</param>
        /// <exception cref="System.ArgumentNullException">
        /// The source is null.
        /// or
        /// The method is null.
        /// or
        /// The method requires parameters which are not specified.
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// The source is invalid.
        /// or
        /// The target is invalid.
        /// or
        /// The specified source and result are the same objects, but the method does not support in-place operations.
        /// or
        /// The parameters do not contain a required parameter value.
        /// or
        /// The type of a parameter does not match the type specified by the method.
        /// or
        /// A parameter value does not satisfy the conditions of the parameter.
        /// </exception>
        protected Operation(SourceType source, ResultType target, OperationMethod method, IDictionary <OperationParameter, Object> parameters)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source", "The source is null.");
            }
            if (method == null)
            {
                throw new ArgumentNullException("method", "The method is null.");
            }
            if (target != null && ReferenceEquals(source, target) && method.SupportedModes.Contains(ExecutionMode.InPlace))
            {
                throw new ArgumentException("The specified source and result are the same objects, but the method does not support in-place operations.", "target");
            }

            if (parameters == null && method.Parameters != null && method.Parameters.Any(parameter => !parameter.IsOptional))
            {
                throw new ArgumentNullException("parameters", "The method requires parameters which are not specified.");
            }

            if (parameters != null && method.Parameters != null)
            {
                foreach (OperationParameter parameter in method.Parameters)
                {
                    // check parameter existence
                    if (!parameter.IsOptional && (!parameters.ContainsKey(parameter) || parameters[parameter] == null))
                    {
                        throw new ArgumentException("The parameters do not contain a required parameter value (" + parameter.Name + ").", "parameters");
                    }

                    if (parameters.ContainsKey(parameter) && parameters[parameter] != null)
                    {
                        // check parameter type
                        if (!(parameter.Type.GetInterfaces().Contains(typeof(IConvertible)) && parameters[parameter] is IConvertible) &&
                            !parameter.Type.Equals(parameters[parameter].GetType()) &&
                            !parameters[parameter].GetType().IsSubclassOf(parameter.Type) &&
                            !parameters[parameter].GetType().GetInterfaces().Contains(parameter.Type))
                        {
                            throw new ArgumentException("The type of a parameter value (" + parameter.Name + ") does not match the type specified by the method.", "parameters");
                        }

                        // check parameter value
                        if (!parameter.IsValid(parameters[parameter]))
                        {
                            throw new ArgumentException("A parameter value (" + parameter.Name + ") does not satisfy the conditions of the parameter.", "parameters");
                        }
                    }
                }
            }

            _source = source;
            _result = target;
            _method = method;

            if (parameters != null)
            {
                _parameters = new Dictionary <OperationParameter, Object>(parameters);
            }
            else
            {
                _parameters = new Dictionary <OperationParameter, Object>();
            }

            _state = OperationState.Initialized;
        }