HimBromBeere HimBromBeere - 1 year ago 126
C# Question

Generate fields instead of properties with CodeDOM

I created some code from an XSD-schema-file using CodeDOM:

XmlSchemaImporter importer = new XmlSchemaImporter(schemas);
CodeNamespace code = new CodeNamespace(targetNamespace);
XmlCodeExporter exporter = new XmlCodeExporter(code);

foreach (XmlSchemaElement element in schema.Elements.Values)
XmlTypeMapping mapping = importer.ImportTypeMapping(element.QualifiedName);

Now within my post-processing I realized that this code will generate properties like this:

bool prop1Field;
/// <remarks/>
public bool Prop1
return prop1Field;
prop1Field = value;

But I want the generator to simply produce fields instead. Is there a way to achieve this? I know
also produces fields when using the

EDIT: Afterwards I want to replace those by auto-properties. To do so with the current approach I´d have to delete the backing-field from the property and all its occurences within the generated code. If CodeDOM however generates a
field in the first place all I had to do is to delete this field, create a new property with the same name using
as shown in this answer. Thus I won´t need to search the codes for occurences of the private backing-field and replace them by calls to the property.

Answer Source

No, there´s no option to be set to allow this as it shouldn´t matter for consuming code if your attribute is a field or a property (assuming we don´t use reflection) and public fields are considered bad practice.

Anyway I found a way to achieve this by copying all the comments and attributes of the property to the private backing-field and make the field public. However remember that doing so is mostly a bad design-idea.

    public void Process(CodeNamespace code, XmlSchema schema)
        foreach (var type in code.Types.Cast<CodeTypeDeclaration>().Where(x => !x.IsEnum))
            var result = new List<CodeMemberField>();
            var properties = type.Members.OfType<CodeMemberProperty>().ToList();
            foreach (var property in properties)
                ReplacePropertyByField(type, property);

    private static void ReplacePropertyByField(CodeTypeDeclaration type, CodeMemberProperty property)
        var backingField = GetBackingField(property, type);
        backingField.Attributes = property.Attributes;
        backingField.CustomAttributes = property.CustomAttributes;
        backingField.Name = property.Name;

    private static CodeMemberField GetBackingField(CodeMemberProperty property, CodeTypeDeclaration type)
        var getterExpression = ((CodeMethodReturnStatement)property.GetStatements[0]).Expression;
        var backingFieldName = ((CodeFieldReferenceExpression)getterExpression).FieldName;
        return type.Members.OfType<CodeMemberField>().Single(x => x.Name == backingFieldName);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download