C# CodeDom Automatic Property

烈酒焚心 提交于 2019-12-08 15:41:05

问题


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


来源:https://stackoverflow.com/questions/583516/c-sharp-codedom-automatic-property

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