Generate C# automatic properties with Codedom
Asked Answered
R

5

9

is there a way Generate C# automatic properties with Codedom or maybe an other set of libreries that i can use ?

Raiment answered 23/1, 2010 at 10:34 Comment(0)
V
2

CodeDom is supposed to be some sort of AST which can be converted to multiple languages (typically C# and VB.NET). Therefore, you'll not find features which are syntactic sugar of a specific language in CodeDom.

Verdun answered 23/1, 2010 at 10:58 Comment(0)
G
8

You can use CodeSnippetTypeMember class for that purpose.

For example:

    CodeTypeDeclaration newType = new CodeTypeDeclaration("TestType");
    CodeSnippetTypeMember snippet = new CodeSnippetTypeMember();
    snippet.Comments.Add(new CodeCommentStatement("this is integer property", true));
    snippet.Text="public int IntergerProperty { get; set; }";
    newType.Members.Add(snippet);
Glanders answered 26/5, 2014 at 20:1 Comment(0)
T
6

No, it's not: C# CodeDom Automatic Property

Take a look into this article to get some useful examples

Tu answered 23/1, 2010 at 10:37 Comment(5)
so are there any other libraries that i can use ?Raiment
@Yassir It's really not that hard to create a backing field and use them in getter/setter.Eliason
you dont need; as Marc Gravell said, you need to implement it yourself, as they are just a compiler trick (i.e. .net compiler creates a private variable to hold your automatic property value)Tu
actually i'm not compiling the generated code i add it to a project so i need the generated classes to have automatic propertiesRaiment
In that case, you could to use a CodeSnippetStatement and hardcode that propertyTu
V
2

CodeDom is supposed to be some sort of AST which can be converted to multiple languages (typically C# and VB.NET). Therefore, you'll not find features which are syntactic sugar of a specific language in CodeDom.

Verdun answered 23/1, 2010 at 10:58 Comment(0)
M
1

Actually the comments about it being easy to use a CodeSnippetStatement are misleading because CodeTypeDeclaration has no statements collection that you can add those snippets to.

Metchnikoff answered 6/10, 2011 at 11:34 Comment(0)
U
-2

You can do this: According to How to: Create a Class Using CodeDOM

        // Declare the ID Property.
        CodeMemberProperty IDProperty = new CodeMemberProperty();
        IDProperty.Attributes = MemberAttributes.Public;
        IDProperty.Name = "Id";
        IDProperty.HasGet = true;
        IDProperty.HasSet = true;
        IDProperty.Type = new CodeTypeReference(typeof(System.Int16));
        IDProperty.Comments.Add(new CodeCommentStatement(
        "Id is identity"));
        targetClass.Members.Add(IDProperty);
Ulises answered 7/3, 2015 at 17:55 Comment(1)
This does not work, as it generates two empty set and get methods which will result in compiling errors. The CodeSnippetTypeMember (https://mcmap.net/q/908418/-generate-c-automatic-properties-with-codedom) is the solutionNightfall

© 2022 - 2024 — McMap. All rights reserved.