コード例 #1
0
        /// <summary>
        /// Determines if the given script is valid.
        /// </summary>
        /// <param name="p_scpScript">The script to validate.</param>
        /// <returns><c>true</c> if the given script is valid;
        /// <c>false</c> otherwise.</returns>
        public bool ValidateScript(IScript p_scpScript)
        {
            CSharpScriptCompiler    sccCompiler = new CSharpScriptCompiler();
            CompilerErrorCollection cecErrors   = null;

            sccCompiler.Compile(((CSharpScript)p_scpScript).Code, BaseScriptType, out cecErrors);
            return(cecErrors == null);
        }
コード例 #2
0
        /// <summary>
        /// Compiles the given C# script code.
        /// </summary>
        /// <remarks>
        /// The compiled script is not loaded into the current domain.
        /// </remarks>
        /// <param name="p_strCode">The code to compile.</param>
        /// <returns>The bytes of the assembly containing the script to execute.</returns>
        protected byte[] Compile(string p_strCode)
        {
            CSharpScriptCompiler    sccCompiler = new CSharpScriptCompiler();
            CompilerErrorCollection cecErrors   = null;

            string strBaseScriptClassName = m_regScriptClass.Match(p_strCode).Groups[2].ToString();
            string strCode = m_regScriptClass.Replace(p_strCode, "using " + BaseScriptType.Namespace + ";\r\n$1" + BaseScriptType.Name);
            Regex  regOtherScriptClasses = new Regex(string.Format(@"(class\s+\S+\s*:.*?)(?<!\w){0}", strBaseScriptClassName));

            strCode = regOtherScriptClasses.Replace(strCode, "$1" + BaseScriptType.Name);
            strCode = m_regFommUsing.Replace(strCode, "");
            byte[] bteAssembly = sccCompiler.Compile(strCode, BaseScriptType, out cecErrors);

            if (cecErrors != null)
            {
                StringBuilder stbErrors = new StringBuilder();
                if (cecErrors.HasErrors)
                {
                    stbErrors.Append("<h3 style='color:red'>Errors</h3><ul>");
                    foreach (CompilerError cerError in cecErrors)
                    {
                        if (!cerError.IsWarning)
                        {
                            stbErrors.AppendFormat("<li><b>{0},{1}:</b> {2} <i>(Error {3})</i></li>", cerError.Line, cerError.Column, cerError.ErrorText, cerError.ErrorNumber);
                        }
                    }
                    stbErrors.Append("</ul>");
                }
                if (cecErrors.HasWarnings)
                {
                    stbErrors.Append("<h3 style='color:#ffd700;'>Warnings</h3><ul>");
                    foreach (CompilerError cerError in cecErrors)
                    {
                        if (cerError.IsWarning)
                        {
                            stbErrors.AppendFormat("<li><b>{0},{1}:</b> {2} <i>(Error {3})</i></li>", cerError.Line, cerError.Column, cerError.ErrorText, cerError.ErrorNumber);
                        }
                    }
                    stbErrors.Append("</ul>");
                }
                if (cecErrors.HasErrors)
                {
                    string strMessage = "Could not compile script; errors were found.";
                    m_csfFunctions.ExtendedMessageBox(strMessage, "Error", stbErrors.ToString());
                    return(null);
                }
            }
            return(bteAssembly);
        }