问题
is there a way Generate C# automatic properties with Codedom or maybe an other set of libreries that i can use ?
回答1:
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.
回答2:
No, it's not: C# CodeDom Automatic Property
Take a look into this article to get some useful examples
回答3:
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);
回答4:
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.
回答5:
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);
来源:https://stackoverflow.com/questions/2122837/generate-c-sharp-automatic-properties-with-codedom