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

发布于 2024-09-14 00:18:16 字数 567 浏览 5 评论 0原文

有没有办法从 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,但无法从生成的代码中删除上述内容。

Is there a way to remove items in code generated in Codedom from VB code?

For example at the top of all the code I generate, it has:

'------------------------------------------------------------------------------
' 
'     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 

I'd like both of these to go away - the commented text and the both the Option xxx. I've tried toying around with CodeGeneratorOptions, but have not been able to remove the above from generated code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

剧终人散尽 2024-09-21 00:18:16

对于#2,你尝试过这个吗?

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

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

For #2, have you tried this?

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

(where CodeCompileUnit is a variable of type CodeCompileUnit)

↘人皮目录ツ 2024-09-21 00:18:16

您可以使用 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);
}

它很难看,但它有效™

You can use a StringWriter to output your code, then use StringBuilder.Remove in order to delete the first lines:

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

It's ugly, but it works ™

在你怀里撒娇 2024-09-21 00:18:16

不,那是无法删除的。它被硬编码到 VBCompiler 中。您可以在 Reflector 的 system.dll 中看到这一点。

No, that can't be removed. It is hard-coded into the VBCompiler. You can see this in system.dll in Reflector.

小镇女孩 2024-09-21 00:18:16

这是我的提案,灵感来自 Maxence 的提案,但可能有点“干净”,因为我使用正则表达式而不是可能随时间变化的索引。这应该适用于 C# 和 VB.net。

using (var stringWriter = new StringWriter())
using (var sourceWriter = new StreamWriter(fileName, false, Encoding.UTF8))
{
   codeDomProvider.GenerateCodeFromCompileUnit(CodeUnit, stringWriter, generatorOptions);
   var newContent = Regex.Replace(stringWriter.ToString(), @"^//.*Runtime Version:.*$", "//\r", RegexOptions.Multiline);
   sourceWriter.Write(newContent);
}

Here is my proposal inspired from Maxence's one, but maybe a bit "cleaner", as I'm using a regular expression instead of indexes that may vary over time. This should work for both C# and VB.net.

using (var stringWriter = new StringWriter())
using (var sourceWriter = new StreamWriter(fileName, false, Encoding.UTF8))
{
   codeDomProvider.GenerateCodeFromCompileUnit(CodeUnit, stringWriter, generatorOptions);
   var newContent = Regex.Replace(stringWriter.ToString(), @"^//.*Runtime Version:.*
quot;, "//\r", RegexOptions.Multiline);
   sourceWriter.Write(newContent);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文