Пример #1
0
        /// <summary>
        /// Yrittää kääntää assemblyn, jos virheitä ilmenee, logger
        /// näyttää errorit userille.
        /// </summary>
        /// <param name="fullname">Scriptin koko nimi (path + filename + extension)</param>
        /// <returns>Käännetty assembly tai null jos kääntäminen ei onnistu.</returns>
        public ScriptAssembly CompileScript(string fullname, string scriptName)
        {
            CompilerResults compilerResults = null;

            using (CSharpCodeProvider csharpCompiler = new CSharpCodeProvider())
            {
                compilerResults = csharpCompiler.CompileAssemblyFromFile(GenerateCompilerOptions(), fullname);

                // Jos kääntämisen yhteydessä ilmenee virheitä, annetaan loggerin handlata errorit
                // ja asetetaan resultit nulliksi.
                if (compilerResults.Errors.HasErrors)
                {
                    compilerErrorLogger.ShowErrors(compilerResults.Errors, fullname);

                    compilerResults = null;
                }
                else
                {
                    List <Type> scripts = compilerResults.CompiledAssembly
                                          .GetTypes()
                                          .Where(c => c.GetInterface("IScript", true) != null)
                                          .ToList();

                    for (int i = 0; i < scripts.Count; i++)
                    {
                        ScriptAttribute attribute = scripts[i].GetCustomAttributes(false)
                                                    .FirstOrDefault(a => a.GetType() == typeof(ScriptAttribute))
                                                    as ScriptAttribute;

                        if (attribute != null && !attribute.IsHidden)
                        {
                            string[] methods = scripts[i].GetMethods().Select <MethodInfo, string>(m => m.Name).ToArray();
                            string[] members = scripts[i].GetMembers().Select <MemberInfo, string>(m => m.Name).ToArray();

                            for (int j = 0; j < ScriptTypes.Count; j++)
                            {
                                string[] myMethods = ScriptTypes[j].GetMethods().Select <MethodInfo, string>(m => m.Name).ToArray();
                                string[] myMembers = ScriptTypes[j].GetMembers().Select <MemberInfo, string>(m => m.Name).ToArray();

                                if (Array.TrueForAll(methods, m => myMethods.Contains(m)) && Array.TrueForAll(members, m => myMembers.Contains(m)))
                                {
                                    return(new ScriptAssembly(ScriptTypes.Find(t => t.Name == scriptName), scriptName, fullname));
                                }
                            }
                        }
                    }
                }
            }

            return(compilerResults == null ? null : new ScriptAssembly(compilerResults.CompiledAssembly, scriptName, fullname));
        }