Removing items in code created from Codedom

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

For example, at the top of all the code that 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 would like both of them to leave - comment and text Option xxx. I tried combining with CodeGeneratorOptions, but could not remove the above from the generated code.

+5
source share
3 answers

No, this cannot be deleted. It is hardcoded in VBCompiler. You can see this in system.dll in Reflector.

+2
source

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)

+2
source

You can use StringWriter to output your code, and then use StringBuilder.Remove to remove 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 works ™

+2
source

All Articles