9

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

Hannoun Yassir
  • 20,583
  • 23
  • 77
  • 112

5 Answers5

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);
bigkahunaburger
  • 426
  • 5
  • 10
6

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

Take a look into this article to get some useful examples

Community
  • 1
  • 1
Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
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.

Lucero
  • 59,176
  • 9
  • 122
  • 152
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.

GilShalit
  • 6,175
  • 9
  • 47
  • 68
-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);
Mahsa Hassankashi
  • 2,086
  • 1
  • 15
  • 25
  • 1
    This does not work, as it generates two empty `set` and `get` methods which will result in compiling errors. The `CodeSnippetTypeMember` (http://stackoverflow.com/a/23876973/191148) is the solution – Mohsen Afshin Feb 19 '16 at 09:39