尝试使用Codedom生成代码时出错

时间:2011-07-06 21:09:35

标签: c# winforms codedom

我的目标:通过使用CodeDom创建一个Windows窗体应用程序(可执行文件)。我的意思是我想要一个表单(在表单后面有一些代码)并将其转换为可执行文件(就好像我已经进入Visual Studio并单击“Build”>“Build File”)。不可否认,我在网上找到了这个例子,之后我对它进行了轻微的修改。我在尝试生成这段代码时遇到错误 - 我以前从未见过的错误。此外,我在Google上找不到这些错误(奇怪的是对吗?)...

这是我得到的错误: 不支持元素类型System.CodeDom.CodeExpression。 参数名称:e

我在以下行收到此错误:

CodeProvider.GenerateCodeFromCompileUnit(Unit, writer, new CodeGeneratorOptions());

这是我的完整代码:

{

     CodeDomProvider CodeProvider = CodeDomProvider.CreateProvider("CSharp");

         // Create the Unit
     CodeCompileUnit Unit = new CodeCompileUnit();

    // Define a namespace and add Imports statements
    CodeNamespace Namespaces = new CodeNamespace("Test.CreateForm");
        Namespaces.Imports.Add(new CodeNamespaceImport("System"));
        Namespaces.Imports.Add(new CodeNamespaceImport("System.Drawing"));
        Namespaces.Imports.Add(new CodeNamespaceImport("System.Windows.Forms"));
        Namespaces.Imports.Add(new CodeNamespaceImport("System.Xml"));
        Namespaces.Imports.Add(new CodeNamespaceImport("System.Data"));

        Unit.Namespaces.Add(Namespaces);

    // Declare the type including base type
    CodeTypeDeclaration MyType = new CodeTypeDeclaration("Form1");
        MyType.IsClass = true;
        MyType.TypeAttributes = System.Reflection.TypeAttributes.Public;
        MyType.BaseTypes.Add("System.Windows.Forms.Form");

        Namespaces.Types.Add(MyType);

    // Create the constructor and add code
     CodeConstructor Constructor = new CodeConstructor();

    Constructor.Statements.Add(
      new CodeMethodInvokeExpression(
      new CodeThisReferenceExpression(),"InitializeComponent", new CodeExpression() {}));

        Constructor.Attributes = MemberAttributes.Public ;

    MyType.Members.Add(Constructor);

    // Declare component container
    MyType.Members.Add(new CodeMemberField("System.ComponentModel.IContainer", "components"));



   //  Implement the Dispose method
    CodeMemberMethod DisposeMethod = new CodeMemberMethod();

    DisposeMethod.Name = "Dispose";
    DisposeMethod.Attributes = MemberAttributes.Family;

    DisposeMethod.Parameters.Add(
        new CodeParameterDeclarationExpression(
        typeof(Boolean), "disposing"));

    CodeConditionStatement Statement = new CodeConditionStatement();
    Statement.Condition = new CodeArgumentReferenceExpression("disposing");

    CodeConditionStatement TrueStatement = new CodeConditionStatement();
    TrueStatement.Condition = 
      new CodeBinaryOperatorExpression(
        new CodeArgumentReferenceExpression("components"),
        CodeBinaryOperatorType.IdentityInequality,
        new CodePrimitiveExpression(null));

    TrueStatement.TrueStatements.Add(
      new CodeMethodInvokeExpression(
        new CodeFieldReferenceExpression(null,
            "components"), "Dispose", new CodeExpression() {}));

    Statement.TrueStatements.Add(TrueStatement);

    DisposeMethod.Statements.Add(Statement);

    DisposeMethod.Statements.Add(new CodeMethodInvokeExpression( new CodeBaseReferenceExpression(), "Dispose",  new CodeArgumentReferenceExpression[] 
        {new CodeArgumentReferenceExpression("disposing")}));

   MyType.Members.Add(DisposeMethod);


    // InitializeComponent
   CodeMemberMethod InitializeMethod = new CodeMemberMethod();

   InitializeMethod.Name = "InitializeComponent";
   InitializeMethod.Attributes = MemberAttributes.Private;

   InitializeMethod.CustomAttributes.Add(
     new CodeAttributeDeclaration(
       "System.Diagnostics.DebuggerStepThrough"));

   InitializeMethod.Statements.Add(
     new CodeAssignStatement(
       new CodeFieldReferenceExpression(
         new CodeThisReferenceExpression(), "components"),
           new CodeObjectCreateExpression(
             new CodeTypeReference(
               typeof(System.ComponentModel.Container)),
               new CodeExpression() { })));

   MyType.Members.Add(InitializeMethod);

    // Main entry point
    CodeEntryPointMethod MainMethod = new CodeEntryPointMethod();
    MainMethod.Name = "Main";
    MyType.Members.Add(MainMethod);

    //Add mouse move event
    CodeMemberEvent eventstate = new CodeMemberEvent();
    eventstate.Name = "MouseMove";
    eventstate.Attributes = MemberAttributes.Final | MemberAttributes.Public;
    eventstate.Type = new CodeTypeReference("System.Windows.Forms.MouseEventHandler");
    MyType.Members.Add(eventstate);

    string OutputName = "Some.cs";

    try
    {

        CodeGeneratorOptions options = new CodeGeneratorOptions();
        options.BlankLinesBetweenMembers = true;
        options.ElseOnClosing = false;
        options.BracingStyle = "C";

        // This is what we'll write the generated code to
        IndentedTextWriter writer = new IndentedTextWriter(new StreamWriter(OutputName, false), "  ");
        try
        {
            CodeProvider.GenerateCodeFromCompileUnit(Unit, writer, new CodeGeneratorOptions());
            writer.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            writer.Close();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

    // Create the compiler options
    // Include referenced assemblies
    CompilerParameters Options = new CompilerParameters();
    Options.GenerateExecutable = true;
    Options.OutputAssembly = "TestForm1.exe";
    Options.CompilerOptions = "/target:winexe";
    Options.MainClass = "Test.CreateForm.Form1";

    string[] referenceAssemblies = { "System.dll", "System.Data.dll", "System.Drawing.dll", "System.Windows.Forms.dll", "System.XML.dll" };

    Options.ReferencedAssemblies.AddRange(referenceAssemblies);

    //Build and look for compiler errors
    CompilerResults Result = CodeProvider.CompileAssemblyFromFile(Options, "Some.cs");

    if (Result.Errors.Count > 0) 
    {
        foreach(CompilerError ce in Result.Errors)
        {
            Console.WriteLine(ce.ErrorText);
        }

    }
    else
    {
        Console.WriteLine("compiled successfully");
    }

    Console.WriteLine("press enter to continue");
    Console.ReadLine();
    }

4 个答案:

答案 0 :(得分:1)

您有许多实例可以创建一个空的CodeExpression,例如在构造函数代码中:

        // Create the constructor and add code
        CodeConstructor Constructor = new CodeConstructor();

        Constructor.Statements.Add(
          new CodeMethodInvokeExpression(
          new CodeThisReferenceExpression(), "InitializeComponent", new CodeExpression() { }));

如果你不想将任何参数传递给方法(在上面的代码中是这种情况),只是不传递任何东西(CodeMethodInvokeExpression构造函数采用params []数组,所以如果你不通过什么,这意味着它收到一个空数组):

        // Create the constructor and add code
        CodeConstructor Constructor = new CodeConstructor();

        Constructor.Statements.Add(
          new CodeMethodInvokeExpression(
            new CodeThisReferenceExpression(),
            "InitializeComponent"));

答案 1 :(得分:1)

如果上面的代码未吐出所需的代码,请确保您已关闭IndentedTextWriter,例如writer.Close()享受..

答案 2 :(得分:1)

CodeMemberMethod DisposeMethod = new CodeMemberMethod();
DisposeMethod.Name = "Dispose";
DisposeMethod.ReturnType = new CodeTypeReference(typeof(void));
DisposeMethod.Attributes = MemberAttributes.Override | MemberAttributes.Private;
DisposeMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(bool), "disposing"));
myDesignerClass.Members.Add(DisposeMethod);

CodeConditionStatement cstif2 = new CodeConditionStatement();
CodeExpression dis = new CodeVariableReferenceExpression("disposing");
CodeExpression comp = new CodeVariableReferenceExpression("components");

cstif2.Condition = new CodeBinaryOperatorExpression(dis, CodeBinaryOperatorType.BooleanAnd, new CodeBinaryOperatorExpression(dis, CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(null)));
CodeExpression dispos = new CodeMethodInvokeExpression(comp, "Dispose", new CodeExpression[] { });
cstif2.TrueStatements.Add(dispos);
DisposeMethod.Statements.Add(cstif2);
CodeExpression bdispos = new CodeMethodInvokeExpression(new CodeBaseReferenceExpression(), "Dispose", new CodeExpression[] { });
DisposeMethod.Statements.Add(bdispos);

答案 3 :(得分:0)

()替换为CodeExpression [],错误将消失。