C# CodeDom Automatic Property
Asked Answered
S

2

11

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?

Schiedam answered 24/2, 2009 at 20:46 Comment(0)
S
12

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

Skater answered 24/2, 2009 at 20:52 Comment(0)
O
8

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);
Otes answered 26/5, 2014 at 20:4 Comment(2)
This is dangerous and not recommended, because all of the validation that comes with using CodeDom essentially goes out the window.Bettis
I guess I like to live dangerously.Otes

© 2022 - 2024 — McMap. All rights reserved.