is there a way Generate C# automatic properties with Codedom or maybe an other set of libreries that i can use ?
Asked
Active
Viewed 6,862 times
5 Answers
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
-
so are there any other libraries that i can use ? – Hannoun Yassir Jan 23 '10 at 10:39
-
@Yassir It's really not that hard to create a backing field and use them in getter/setter. – chakrit Jan 23 '10 at 10:44
-
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) – Rubens Farias Jan 23 '10 at 10:44
-
actually i'm not compiling the generated code i add it to a project so i need the generated classes to have automatic properties – Hannoun Yassir Jan 23 '10 at 10:47
-
1In that case, you could to use a `CodeSnippetStatement` and hardcode that property – Rubens Farias Jan 23 '10 at 11:36
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
-
1This 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