代码之家  ›  专栏  ›  技术社区  ›  Todd Main

删除从Codedom生成的代码中的项

  •  5
  • Todd Main  · 技术社区  · 14 年前

    有没有办法从VB代码中删除Codedom生成的代码中的项?

    '------------------------------------------------------------------------------
    ' 
    '     This code was generated by a tool.
    '     Runtime Version:4.0.30319.1
    '
    '     Changes to this file may cause incorrect behavior and will be lost if
    '     the code is regenerated.
    ' 
    '------------------------------------------------------------------------------
    Option Strict Off 
    Option Explicit On 

    我希望这两个都消失-评论文本和 Option xxx . 我试过玩弄 CodeGeneratorOptions ,但无法从生成的代码中删除上述内容。

    3 回复  |  直到 14 年前
        1
  •  2
  •   Stan    14 年前

    不,不能去掉。它被硬编码到vb编译器中。您可以在Reflector的system.dll中看到这一点。

        2
  •  2
  •   Dumb Guy    14 年前

    你试过这个吗?

    CodeCompileUnit.UserData.Add("AllowLateBound", False) ' strict on
    CodeCompileUnit.UserData.Add("RequireVariableDeclaration", False) ' explicit off
    

    (其中CodeCompileUnit是CodeCompileUnit类型的变量)

        3
  •  2
  •   Maxence    12 年前

    可以使用StringWriter输出代码,然后使用StringBuilder.Remove删除第一行:

    using (var stringWriter = new StringWriter())
    using (var streamWriter = new StreamWriter(path))
    {
        codeDomProvider.GenerateCodeFromCompileUnit(unit, stringWriter, options);
        StringBuilder sb = stringWriter.GetStringBuilder();
        /* Remove the header comment (444 is for C#, use 435 for VB) */
        sb.Remove(0, 444);
        streamWriter.Write(sb);
    }
    

    虽然很难看,但很管用