Generate C# automatic properties with Codedom
is there a way Generate C# automatic prope开发者_StackOverflowrties with Codedom or maybe an other set of libreries that i can use ?
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);
No, it's not: C# CodeDom Automatic Property
Take a look into this article to get some useful examples
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.
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.
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);
精彩评论