0

I'm generating a struct using CodeDom

var type = new CodeTypeDeclaration();
type.Name = "MyStructure";
type.IsStruct = true;
type.TypeAttributes = TypeAttributes.Public;
type.Members.

type.Members.Add(new CodeMemberField{
    Attributes = System.CodeDom.MemberAttributes.Public | System.CodeDom.MemberAttributes.Const,
    Name = "CreatedBy",
    Type = new CodeTypeReference(typeof (String)),
    InitExpression = new CodePrimitiveExpression("createdby"), 
});

type.Members.Add(new CodeMemberField{
    Attributes = System.CodeDom.MemberAttributes.Public | System.CodeDom.MemberAttributes.Const,
    Name = "ModifiedBy",
    Type = new CodeTypeReference(typeof (String)),
    InitExpression = new CodePrimitiveExpression("modifiedby")
});

And when it generates it looks like this:

public struct MyStructure
{

  public const string CreatedBy = "createdby";

  public const string ModifiedBy = "modifiedby";
}

But I'd prefer it to not have the newlines in it:

public struct MyStructure
{
   public const string CreatedBy = "createdby";  
   public const string ModifiedBy = "modifiedby";
}

Is it possible to get the generation to not include the new lines?

svick
  • 236,525
  • 50
  • 385
  • 514
Daryl
  • 18,592
  • 9
  • 78
  • 145
  • Check out http://stackoverflow.com/questions/238002/replace-line-breaks-in-a-string-c-sharp for an example of programmatically removing newlines in a string. – calmond Apr 09 '15 at 17:33
  • @calmond I'd have to let the CodeDom Generate the File, then read the file, then remove only the newlines I want to remove, the write the file back to disk. Not something that would work very well... – Daryl Apr 09 '15 at 17:37

2 Answers2

0

Found out that the CodeGeneratorOptions allows you to specify NoLineBreaksBetweenMembers. I don't want to apply it to everything, but I can create just my struct, and then add it as a CodeSnippetTypeMember:

type.Members.Insert(0, GenerateTypeWithoutEmptyLines(@struct));

...

/// <summary>
/// Removes the blank lines spaces by generating the code as a string without BlankLinesBetweenMembers
/// </summary>
/// <param name="type">The type.</param>
/// <returns></returns>
private static CodeSnippetTypeMember GenerateTypeWithoutEmptyLines(CodeTypeDeclaration type)
{
    var provider = CodeDomProvider.CreateProvider("CSharp");
    using (var sourceWriter = new StringWriter())
    using (var tabbedWriter = new IndentedTextWriter(sourceWriter, "\t"))
    {
        tabbedWriter.Indent = 2;
        provider.GenerateCodeFromType(type, tabbedWriter, new CodeGeneratorOptions()
        {
            BracingStyle = "C",
            IndentString = "\t",
            BlankLinesBetweenMembers = false
        });
        return new CodeSnippetTypeMember("\t\t" + sourceWriter);
    }
}
Daryl
  • 18,592
  • 9
  • 78
  • 145
0

There's also a nice nuget package for C# code generation. Maybe it matches your requirements. It's just an idea because in my opinion, removing empty rowns in the result string of your codedom object is a hack.

http://www.codeproject.com/Articles/892114/WuffProjects-CodeGeneration

Pad
  • 47
  • 1
  • 6
  • I'm using a tool that only accepts plugins that work with CodeDom, so your suggestion won't work. – Daryl Apr 15 '15 at 19:18