如何使用 Roslyn 创建基于结构的属性 [英] How to create struct based properties with Roslyn

查看:34
本文介绍了如何使用 Roslyn 创建基于结构的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来生成一个属性:

I have the following code to generate a property:

类型:

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

但是,当结构类型的名称是参数时,我总是会遇到异常(例如 DateTimeGuid - 对于 Guid,我什至找不到正确的特殊类型):

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

不支持的特殊类型

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

我应该用什么?

推荐答案

您可以根据类型的名称创建属性,因此您可以使用诸如

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?

(我注意到示例中的PropertyDeclaration"方法指定了与 SyntaxFactory 类上的任何PropertyDeclaration"方法签名都不对应的参数名称、类型、可访问性 - 是您编写的那个方法 then 调用 SyntaxFactory 方法?)

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

这篇关于如何使用 Roslyn 创建基于结构的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆