braces and curly brackets when generating interface with codeDom

590 Views Asked by At

I am using codeDom to generate an interface and I am getting braces and curly brackets in places I don't want. I am decorating a method with [OperationContract()] but I don't want the braces there. Here is the code I have written

toStringMethod.CustomAttributes.Add(new CodeAttributeDeclaration("OperationContract"));

Also, the methods that are generated have curly brackets added. I don't want that. Since this is an interface, I want just a semicolon. Here is what it looks like now.

[OperationContract()]
System.Collections.Generic.List<Aristotle.P6.Model.KeyIssue.Issue> GetAllIssues()
{
}

Below lies the majority of the code I have written;

  foreach (var dll in dlls)
        {
            Assembly assembly = Assembly.LoadFrom(dll);

            foreach (var type in assembly.ExportedTypes)
            {
                var methodInfo = type.GetMethods();
                CodeCompileUnit targetUnit;
                CodeTypeDeclaration targetClass;
                targetUnit = new CodeCompileUnit();
                CodeNamespace samples = new CodeNamespace("CodeDOMSample");
                samples.Imports.Add(new CodeNamespaceImport("System"));
                targetClass = new CodeTypeDeclaration("CodeDOMCreatedClass");
                targetClass.IsClass = true;
                targetClass.TypeAttributes =
                    TypeAttributes.Public | TypeAttributes.Sealed;
                samples.Types.Add(targetClass);
                targetUnit.Namespaces.Add(samples);




                foreach (var method in methodInfo)
                {
                    CodeMemberMethod toStringMethod = new CodeMemberMethod();
                    toStringMethod.Attributes =
                        MemberAttributes.AccessMask;
                    toStringMethod.Name = method.Name;
                    toStringMethod.CustomAttributes.Add(new CodeAttributeDeclaration("OperationContract"));
                    foreach (var item in method.GetParameters())
                    {
                        toStringMethod.Parameters.Add(new CodeParameterDeclarationExpression(item.ParameterType, item.Name));
                    }

                    toStringMethod.ReturnType =
                            new CodeTypeReference(method.ReturnType);

                    targetClass.Members.Add(toStringMethod);


                } 

                Program program = new Program();
                program.GenerateCSharpCode(type.Name, targetUnit);
            }

        }

Update

This is what my GenerateCSharpCode method looks like:

  public void GenerateCSharpCode(string fileName, CodeCompileUnit targetUnit)
    {
        CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
        CodeGeneratorOptions options = new CodeGeneratorOptions();
        options.BracingStyle = "C";
        using (StreamWriter sourceWriter = new StreamWriter(fileName))
        {
            provider.GenerateCodeFromCompileUnit(
                targetUnit, sourceWriter, options);
        }
    }
1

There are 1 best solutions below

2
On

When you use CodeDom, I understand you have some options on how code is created. You have to use the CodeGeneratorOptions and set the BracingStyle = "C"; if you want to change the default behavior.

Take a look on the Examples section on the following article.

http://msdn.microsoft.com/en-us/library/system.codedom.compiler.codegeneratoroptions(v=vs.110).aspx

Hope this helps.

EDIT

Are you generated code creating classes or interfaces? I understand you want interfaces, so you should replace the IsClass = true to IsInterface = true. Take a look at this question: how to create an interface method with CodeDom