Nestor
Nestor

Reputation: 8384

How to create struct based properties with Roslyn

I have the following code to generate a property:

Types:

types = new Dictionary<string, SpecialType>();
types.Add("Guid", SpecialType.System_Object);
types.Add("DateTime", SpecialType.System_DateTime);
types.Add("String", SpecialType.System_String);
types.Add("Int32", SpecialType.System_Int32);
types.Add("Boolean", SpecialType.System_Boolean);  

generator.PropertyDeclaration(name, generator.TypeExpression(types["DateTime"]), Accessibility.Public);

However, I always get an exception when the name of a struct type is the parameter (e.g. DateTime or Guid - for Guid, I can't even find a proper special type):

Unsupported SpecialType

  at: Microsoft.CodeAnalysis.CSharp.CodeGeneration.CSharpSyntaxGenerator.TypeExpression(SpecialType specialType)
  at: MyProject.CreateProperty(String name, String type)

What should I use?

Upvotes: 2

Views: 1495

Answers (1)

Dan Roberts
Dan Roberts

Reputation: 2329

You can create properties based upon the name of the type, so you could create DateTime and Guid properties with code such as

// Create an auto-property
var idProperty =
    SyntaxFactory.PropertyDeclaration(
        SyntaxFactory.ParseTypeName("Guid"),
        "Id"
    )
    .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
    .AddAccessorListAccessors(
        SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
        SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
    );

// Create a read-only property, using a backing field
var createdAtProperty =
    SyntaxFactory.PropertyDeclaration(
        SyntaxFactory.ParseTypeName("DateTime"),
        "CreatedAt"
    )
    .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
    .AddAccessorListAccessors(
        SyntaxFactory.AccessorDeclaration(
            SyntaxKind.GetAccessorDeclaration,
            SyntaxFactory.Block(
                SyntaxFactory.List(new[] {
                    SyntaxFactory.ReturnStatement(SyntaxFactory.IdentifierName("_createdAt"))
                })
            )
        )
    );

If I've missed something obvious that means that you can't use this sort of syntax, would you please edit your answer and include an executable minimum reproduce case?

(I noticed that the "PropertyDeclaration" method in your sample specifies parameters name, type, accessibility which do not correspond to any "PropertyDeclaration" method signatures on the SyntaxFactory class - is that method one that you've written that then calls a SyntaxFactory method?)

Upvotes: 4

Related Questions