public static GenerationPreferences Load(string filename)
        {
            var pref = new GenerationPreferences();

            ReflectionConfig.LoadFromXml(filename, pref);
            return(pref);
        }
Example #2
0
        public override ActionResult Execute(ISpecialExecutionTaskTestAction testAction)
        {
            var        action     = testAction as SpecialExecutionTaskTestAction;
            var        module     = action.TCItem.GetModule();
            XModule    xMod       = module as XModule;
            var        xParams    = xMod.GetXParams();
            ModuleType moduleType = ModuleType.Method;
            //if (!Enum.TryParse(xParams.Single(x => x.Name.ToLower() == "type").Value, out moduleType))
            //{
            //    throw new InvalidOperationException("Invalid value in 'Type' Technical Parameter");
            //}

            ReflectionConfig config = new ReflectionConfig
            {
                ClassName   = xParams.Single(x => x.Name.ToLower() == "classname").Value,
                LibraryFile = xParams.Single(x => x.Name.ToLower() == "libraryfile").Value,
                MethodName  = xParams.Single(x => x.Name.ToLower() == "methodname").Value
            };

            var          classObject = ActivateObject(config);
            ActionResult result      = null;

            switch (moduleType)
            {
            case ModuleType.Class:
                result = ExecuteClass(action, classObject);
                break;

            case ModuleType.Method:
                result = ExecuteMethod(action, classObject, config);
                break;
            }
            return(result);
        }
Example #3
0
        /// <summary>
        /// Deserializes the configuration from an XML file.
        /// </summary>
        private static Configuration Load(string fileName)
        {
            Configuration cfg = new Configuration();

            ReflectionConfig.LoadFromXml(fileName, cfg);
            cfg.ValidateValues();

            return(cfg);
        }
Example #4
0
        private object ActivateObject(ReflectionConfig config)
        {
            Assembly assembly = null;
            Type     type     = null;

            //Load the assembly and execute
            if (!string.IsNullOrEmpty(config.LibraryFile))
            {
                assembly = Assembly.LoadFrom(config.LibraryFile);
                type     = assembly.GetType(config.ClassName);
            }
            else
            {
                //Search loaded assemblies for the type
                type = Type.GetType(config.ClassName, true);
            }
            //Check if it has a parameterless constructor
            if (!type.GetConstructors().Any(x => x.GetParameters().Length == 0))
            {
                throw new InvalidOperationException(string.Format("Unable to create the class {0} as it doesn't have a parameterless constructor", config.ClassName));
            }
            return(InstanceManager.Instance.GetInstance(type));
        }
 public void Save(string filename)
 {
     ReflectionConfig.SaveToXml(filename, this);
 }
Example #6
0
 /// <summary>
 /// Serializes the configuration into an XML file.
 /// </summary>
 public void Save() => ReflectionConfig.SaveToXml(Program.ConfigFileName, this);
Example #7
0
        private ActionResult ExecuteMethod(SpecialExecutionTaskTestAction action, object classObject, ReflectionConfig config)
        {
            var          parameters = action.Parameters;
            ActionResult r          = null;
            Type         type       = classObject.GetType();
            //Check for a method with the same name, number of parameters, and parameter names match the Technical ID[Name] value of the attached module
            IEnumerable <MethodInfo> methods = type.GetMethods()
                                               .Where(x => x.GetParameters().Count() == parameters.Where(p => !IsResultParameter(p)).Count() &&
                                                      x.Name == config.MethodName &&
                                                      x.GetParameters().All(p => parameters.Any(xp => GetParameterCodeName(xp) == p.Name)));
            MethodInfo methodInfo = null;

            if (!methods.Any())
            {
                throw new InvalidOperationException($"No method found with name {config.MethodName} and parameters matching the input parameters");
            }
            if (methods.Count() > 1)
            {
                if (action.Children.Any())
                {
                    if (methods.Where(x => x.ReturnType != typeof(void)).Count() == 1)
                    {
                        methodInfo = methods.First(x => x.ReturnType != typeof(void));
                    }
                }
            }
            else
            {
                methodInfo = methods.First();
            }
            if (methodInfo == null)
            {
                throw new InvalidOperationException($"Unable to locate method {config.MethodName}, with the correct signature. Type overload on method footprints is currently not supported.");
            }

            ParameterInfo[] methodParameters = methodInfo.GetParameters();

            List <object> instanceParameters = new List <object>();

            foreach (var parameter in methodParameters)
            {
                IParameter testParameter = parameters.FirstOrDefault(p => GetParameterCodeName(p) == parameter.Name);
                if (testParameter == null)
                {
                    throw new Exception($"Unable to find Test value for method parameter {parameter.Name}");
                }
                object result = GetObjectFromParameter(testParameter, parameter.ParameterType);
                instanceParameters.Add(result);
            }
            try
            {
                object methodResult = methodInfo.Invoke(classObject, instanceParameters.ToArray());
                var    resultAction = parameters.FirstOrDefault(x => IsResultParameter(x));
                if (resultAction == null)
                {
                    return(new PassedActionResult($"Executed method {methodInfo.Name} successfuly. No result expected."));
                }
                else
                {
                    if (!resultAction.Parameters.Any())
                    {
                        HandleActualValue(action, resultAction, methodResult);
                    }
                    else
                    {
                        var resultType = methodResult.GetType();
                        foreach (var property in resultType.GetProperties().Where(x => x.SetMethod.IsPublic))
                        {
                            var childParameter = resultAction.Parameters.FirstOrDefault(x => GetParameterCodeName(x) == property.Name);
                            if (childParameter != null)
                            {
                                HandleActualValue(action, childParameter, property.GetValue(methodResult));
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                string message = e.Message;
                if (e.InnerException != null)
                {
                    message = e.InnerException.Message;
                }
                r = new UnknownFailedActionResult(message);
                return(r);
            }
            return(new PassedActionResult());
        }