Esempio n. 1
0
        /// <summary>
        /// Used to determine if a fitness function will correctly compile.  If it
        /// doesn't, errors are returned through the Errors parameters.
        /// </summary>
        /// <param name="FitnessCode">Fitness class to test</param>
        /// <param name="Errors">Array of errors, if any</param>
        /// <returns>True/False upon success/failure</returns>
        public bool ValidateFitnessClass(String FitnessCode, out String[] Errors)
        {
            GPFitnessCustomBase func = CompileFitnessClass(FitnessCode, out Errors);

            if (func == null)
            {
                return(false);
            }

            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Take the CSharp code, compile it to an assembly and then return any
        /// errors that came up during compilation.
        /// </summary>
        /// <param name="CSharpCode">The C# code to compile</param>
        /// <param name="Errors">List of compilation errors</param>
        /// <returns>in-memory instance of the code ready for execution</returns>
        public GPFitnessCustomBase CompileFitnessClass(String CSharpCode, out String[] Errors)
        {
            CSharpCodeProvider csProvider = new CSharpCodeProvider();
            CompilerParameters csParams   = new CompilerParameters();

            csParams.GenerateExecutable    = false;
            csParams.GenerateInMemory      = true;
            csParams.TreatWarningsAsErrors = false;

            //
            // We derive from GPFitnessCustomBase, so need to reference the assembly
            csParams.ReferencedAssemblies.Add(Application.StartupPath + "\\GPServer.exe");

            //
            // Compile
            CompilerResults results = csProvider.CompileAssemblyFromSource(csParams, CSharpCode);

            //
            // See if there are any errors
            Errors = null;
            if (results.Errors.Count > 0)
            {
                //
                // Convert the errors into a string array that is send back
                Errors = new String[results.Errors.Count];
                for (int Item = 0; Item < results.Errors.Count; Item++)
                {
                    Errors[Item] = (Item + 1).ToString() + ": " + results.Errors[Item].ErrorText;
                }
                return(null);
            }
            //
            // Given the assembly, create an instance of the object from it
            GPFitnessCustomBase codeInstance = (GPFitnessCustomBase)results.CompiledAssembly.CreateInstance("GPStudio.Server.GPFitnessCustom");

            return(codeInstance);
        }