如何使用 Roslyn 创建基于结构的属性
How to create struct based properties with Roslyn
我有以下代码生成 属性:
类型:
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);
但是,当结构类型的名称是参数时(例如 DateTime 或 Guid - 对于 Guid , 我什至找不到合适的特殊类型):
Unsupported SpecialType
at: Microsoft.CodeAnalysis.CSharp.CodeGeneration.CSharpSyntaxGenerator.TypeExpression(SpecialType specialType)
at: MyProject.CreateProperty(String name, String type)
我应该使用什么?
您可以根据类型的名称创建属性,因此您可以使用
等代码创建 DateTime 和 Guid 属性
// 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"))
})
)
)
);
如果我遗漏了一些明显的东西,这意味着您不能使用这种语法,请您编辑您的答案并包含一个可执行的最小重现案例好吗?
(我注意到您示例中的 "PropertyDeclaration" 方法指定的参数名称、类型、可访问性与 SyntaxFactory class 上的任何 "PropertyDeclaration" 方法签名都不对应 - 是您编写的方法一 then 调用 SyntaxFactory 方法?)
我有以下代码生成 属性:
类型:
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);
但是,当结构类型的名称是参数时(例如 DateTime 或 Guid - 对于 Guid , 我什至找不到合适的特殊类型):
Unsupported SpecialType
at: Microsoft.CodeAnalysis.CSharp.CodeGeneration.CSharpSyntaxGenerator.TypeExpression(SpecialType specialType)
at: MyProject.CreateProperty(String name, String type)
我应该使用什么?
您可以根据类型的名称创建属性,因此您可以使用
等代码创建 DateTime 和 Guid 属性// 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"))
})
)
)
);
如果我遗漏了一些明显的东西,这意味着您不能使用这种语法,请您编辑您的答案并包含一个可执行的最小重现案例好吗?
(我注意到您示例中的 "PropertyDeclaration" 方法指定的参数名称、类型、可访问性与 SyntaxFactory class 上的任何 "PropertyDeclaration" 方法签名都不对应 - 是您编写的方法一 then 调用 SyntaxFactory 方法?)