Vb.net 删除从Codedom生成的代码中的项

Vb.net 删除从Codedom生成的代码中的项,vb.net,codedom,Vb.net,Codedom,有没有办法从VB代码中删除Codedom中生成的代码中的项 例如,在我生成的所有代码的顶部,它有: '------------------------------------------------------------------------------ ' ' This code was generated by a tool. ' Runtime Version:4.0.30319.1 ' ' Changes to this file may cause inc

有没有办法从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 '------------------------------------------------------------------------------ ' '此代码是由工具生成的。 '运行时版本:4.0.30319.1 ' '对此文件的更改可能会导致不正确的行为,如果 '代码将重新生成。 ' '------------------------------------------------------------------------------ 选择严格关闭 选项显式打开 我希望这两个都消失-注释文本和选项xxx。我尝试过玩弄
codeneratoroptions
,但无法从生成的代码中删除上述内容。

对于#2,您尝试过这个吗

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

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

否,无法删除。它是硬编码到VBC编译器中的。您可以在Reflector中的system.dll中看到这一点。

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

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);
}

虽然很难看,但它很管用™

另请看《哑巴:好发现》。我已经找过了,没有找到那个。这就是答案。关于删除
选项
,您有什么想法吗?不幸的是,我没有删除它们,或者
False
或者
True
。这是一个很好的方法。特别是C#vs.VB