删除 Codedom 生成的代码中的项目
有没有办法从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于#2,你尝试过这个吗?
(其中 CodeCompileUnit 是 CodeCompileUnit 类型的变量)
For #2, have you tried this?
(where CodeCompileUnit is a variable of type CodeCompileUnit)
您可以使用 StringWriter 输出代码,然后使用 StringBuilder.Remove 删除第一行:
它很难看,但它有效™
You can use a StringWriter to output your code, then use StringBuilder.Remove in order to delete the first lines:
It's ugly, but it works ™
不,那是无法删除的。它被硬编码到 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.
这是我的提案,灵感来自 Maxence 的提案,但可能有点“干净”,因为我使用正则表达式而不是可能随时间变化的索引。这应该适用于 C# 和 VB.net。
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.