1

MSDN の記事を使用して、独自の小さなカスタム C# コンパイラを作成しました。

しかし、サンプル コンパイラを使用して新しい Windows フォーム アプリケーションを作成すると、MSDOS ウィンドウも表示され、DOS ウィンドウを閉じると、WinForms アプリも閉じます。コンパイラにどのように伝えることができますか? MSDOS ウィンドウをまったく表示しないのですか?

ありがとうございました :)

これが私のコードです:

using System;

namespace JTS
{
    public class CSCompiler
    {
        protected string ot,
            rt,
            ss, es;

        protected bool rg, cg;

        public string Compile(String se, String fe, String[] rdas, String[] fs, Boolean rn)
        {
            System.CodeDom.Compiler.CodeDomProvider CODEPROV = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp");
            ot =
                fe;

            System.CodeDom.Compiler.CompilerParameters PARAMS = new System.CodeDom.Compiler.CompilerParameters();
            // Ensure the compiler generates an EXE file, not a DLL.
            PARAMS.GenerateExecutable = true;
            PARAMS.OutputAssembly = ot;
            PARAMS.CompilerOptions = "/target:winexe"; PARAMS.ReferencedAssemblies.Add(typeof(System.Xml.Linq.Extensions).Assembly.Location);
            PARAMS.LinkedResources.Add("this.ico");

            foreach (String ay in rdas)
            {
                if (ay.Contains(".dll"))
                    PARAMS.ReferencedAssemblies.Add(ay);
                else
                {
                    string refd = ay;
                    refd = refd + ".dll";
                    PARAMS.ReferencedAssemblies.Add(refd);
                }

            }

            System.CodeDom.Compiler.CompilerResults rs = CODEPROV.CompileAssemblyFromFile(PARAMS, fs);

            if (rs.Errors.Count > 0)
            {
                foreach (System.CodeDom.Compiler.CompilerError COMERR in rs.Errors)
                {
                    es = es +
                        "Line number: " + COMERR.Line +
                        ", Error number: " + COMERR.ErrorNumber +
                        ", '" + COMERR.ErrorText + ";" +
                        Environment.NewLine + Environment.NewLine;
                }
            }
            else
            {
                // Compilation succeeded.
                es = "Compilation Succeeded.";

                if (rn) System.Diagnostics.Process.Start(ot);
            }
            return es;
        }
    }
}
4

2 に答える 2

4

C# コンパイラでは、/target スイッチが exe の場合にコンソール ウィンドウが表示されます。/target=winexe の場合、コンソール ウィンドウは表示されません。 http://msdn.microsoft.com/en-us/library/6h25dztx.aspx

これを試して:

System.CodeDom.Compiler.CompilerParameters PARAMS = new System.CodeDom.Compiler.CompilerParameters();

PARAMS->CompilerOptions = "/target:winexe";

参照: http://msdn.microsoft.com/en-us/library/system.codedom.compiler.compilerparameters.compileroptions.aspx

于 2010-04-18T04:58:39.567 に答える
1

あなたが参照しているMSDN記事はわかりませんが、AssemblyBuilderを使用する場合、「魔法」はSetEntryPointへの呼び出しにあります。

Windows フォーム アプリケーションがある場合は、PEFileKinds.WindowApplication を指定する必要があります。

var asm = AppDomain.CurrentDomain.DefineDynamicAssembly(
    new AssemblyName(assemblyName), AssemblyBuilderAccess.Save);
var mod = asm.DefineDynamicModule(assemblyName, fileName);        
var type = mod.DefineType("Program",
    TypeAttributes.Class | TypeAttributes.Sealed | TypeAttributes.Public);
var mainMethod = type.DefineMethod("Main",
    MethodAttributes.Public | MethodAttributes.Static);
// ... Code for Main method and the rest ...
type.CreateType();
asm.SetEntryPoint(mainMethod,PEFileKinds.WindowApplication);
asm.Save(fileName);

他の PEFileKinds は ConsoleApplication と Dll ですが、EntryPoint を指定しない場合、AssemblyBuilder は自動的に Dll であると想定すると思います。

于 2010-04-18T05:07:08.870 に答える