Generate C# automatic properties with Codedom

徘徊边缘 提交于 2019-12-05 05:28:43

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.

Rubens Farias

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

Take a look into this article to get some useful examples

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);

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!