C# CodeDom Automatic Property

2019-06-16 06:51发布

问题:

I have a property created with CodeDom. How can I set it to being an automatic property instead of adding CodeFieldReferenceExpressions against a private member?

回答1:

IIRC, CodeDom simply doesn't have a way of expressing this. Automatically implemented properties are just compiler sugar, but since it doesn't map (cleanly) to all languages, it doesn't fit cleanly into CodeDom (besides, CodeDom would have needed an update).



回答2:

Yes you can.

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