简体   繁体   中英

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 (eg 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM