コード例 #1
0
        /// <summary>
        /// Compile text to code.
        /// </summary>
        /// <param name="textToCompile">Text for compiling.</param>
        /// <param name="outputAssembly">Path where place the result of compiling, if way is not specified result place in memories.</param>
        /// <param name="outputType">Type of the result to compiling.</param>
        /// <param name="referencedAssemblies">Referenced assemblies.</param>
        /// <param name="resources">Linked resources.</param>
        /// <returns>Result of compiling.</returns>
        public static CompilerResults Compile(string textToCompile, string outputAssembly, LanguageType languageType,
                                              StiOutputType outputType, string[] referencedAssemblies, List <string> resources)
        {
            CompilerParameters parms = new CompilerParameters();

            #region Add Resources
            if (resources != null)
            {
                foreach (string resource in resources)
                {
                    parms.EmbeddedResources.Add(resource);
                }
            }
            #endregion

            parms.ReferencedAssemblies.AddRange(referencedAssemblies);

            parms.OutputAssembly = outputAssembly;
            if (outputAssembly.Length == 0)
            {
                parms.GenerateInMemory = true;
            }
            else
            {
                parms.GenerateInMemory = false;
            }

            switch (outputType)
            {
            case StiOutputType.ClassLibrary:
                parms.CompilerOptions    = parms.CompilerOptions + " /target:library";
                parms.GenerateExecutable = false;
                break;

            case StiOutputType.ConsoleApplication:
                parms.CompilerOptions    = parms.CompilerOptions + " /target:exe";
                parms.GenerateExecutable = true;
                break;

            case StiOutputType.WindowsApplication:
                parms.CompilerOptions    = parms.CompilerOptions + " /target:winexe";
                parms.GenerateExecutable = true;
                break;
            }

            CodeDomProvider provider = null;

            if (languageType == LanguageType.CSharp)
            {
                provider = new Microsoft.CSharp.CSharpCodeProvider();
            }
            else
            {
                provider = new Microsoft.VisualBasic.VBCodeProvider();
            }

            CompilerResults results = null;

            if (AssemblyVersion != null)
            {
                int namespaceIndex = textToCompile.IndexOf("namespace");

                textToCompile = textToCompile.Insert(namespaceIndex, "[assembly: System.Reflection.AssemblyVersion(\"" + AssemblyVersion + "\")]\n");
            }

            results = provider.CompileAssemblyFromSource(parms, textToCompile);
            provider.Dispose();

            return(results);
        }
コード例 #2
0
 /// <summary>
 /// Compile text to code.
 /// </summary>
 /// <param name="textToCompile">Text for compiling.</param>
 /// <param name="outputAssembly">Path where place the result of compiling, if way is not specified result place in memories.</param>
 /// <param name="outputType">Type of the result to compiling.</param>
 /// <param name="referencedAssemblies">Referenced assemblies</param>
 /// <returns>Result of compiling.</returns>
 public static CompilerResults Compile(string textToCompile, string outputAssembly, LanguageType languageType,
                                       StiOutputType outputType, string[] referencedAssemblies)
 {
     return(Compile(textToCompile, outputAssembly, languageType, outputType, referencedAssemblies, null));
 }