Generate C# automatic properties with Codedom

我与影子孤独终老i 提交于 2019-12-12 09:38:20

问题


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

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