C# 13 扩展了 partial 的支持,实现了对 partial property 的支持,得益于 partial property 的支持,正则表达式 source generator 之前只支持 partial method,在 .NET 9 也将支持 partial property
来看简单的使用示例:
file partial class PartialPropertyClass
{
public partial int Num { get; set; }
}
file partial class PartialPropertyClass
{
private int _num = 1;
public partial int Num { get => _num; set => _num = value; }
}
file partial struct PartialPropertyStruct
{
[DisplayName("Number")]
public partial int Num { get; }
}
file partial struct PartialPropertyStruct
{
[JsonPropertyName("num")]
public partial int Num => 2;
}
partial property 既可以用于 class 也可以用于 struct
CS9250: a partial property cannot be automatically implemented
get; set;
则 get; set;
都要有实现,有一个没有实现就会报错,会出现下面这样的报错CS9252: Property accessor 'PartialPropertyClass.Num.set' must be implemented because it is declared on the definition part
CS9253: Property accessor 'PartialPropertyClass.Num.set' does not implement any accessor declared on the definition part
CS0102 The type 'PartialPropertyClass' already contains a definition for 'Num' CSharp13Samples
CS9250 A partial property may not have multiple defining declarations, and cannot be an auto-property.
...
那么问题来了,如果 attribute 不允许出现多个,只能出现一个,在声明和实现的地方分别添加了 attribute 会怎么样呢?感兴趣的朋友可以自己尝试一下试试看看
file partial class PartialIndexer
{
public partial string this[int index] { get; }
}
file partial class PartialIndexer
{
public partial string this[int index] { get => index.ToString(); }
}
正则表达式 source generator 使用示例如下:
[GeneratedRegex(@"^1[1-9]\d{9}$")]
private static partial Regex PhoneRegex();
[GeneratedRegex(@"^1[1-9]\d{9}$")]
private static partial Regex PhoneNumberRegex { get; }
var phone = "12312341234";
Console.WriteLine(PhoneRegex().IsMatch(phone));
Console.WriteLine(PhoneNumberRegex.IsMatch(phone));
二者输出的结果是一样的,只是一个正则是一个 method,另外一个是 property
partial property 的支持使得 Source Generator 支持的更加丰富,可玩性更好了
使用自动属性没有办法使用 partial property,那么我们只想通过 partial property 给属性添加 attribute 的话怎么实现呢,我们可以写一个实现
file partial class PartialPropertyClass
{
public partial int Num { get; set; }
}
file partial class PartialPropertyClass
{
private int _num;
[Display(Name = "Number")]
public partial int Num { get => _num; set => _num = value; }
}
在有了 field keyword 支持之后,我们也可以做一个简化
file partial class PartialPropertyClass
{
public partial int Num { get; set; }
}
file partial class PartialPropertyClass
{
[Display(Name = "Number")]
public partial int Num { get => field; set => field = value; }
}
最后再留一个问题
/// <summary>
/// My type
/// </summary>
partial class C
{
/// <summary>Definition part comment</summary>
/// <returns>Return value comment</returns>
public partial int Prop { get; set; }
/// <summary>Implementation part comment</summary>
public partial int Prop { get => 1; set { } }
}
这样的声明和实现上都有文档注释,生成的 xml 文档最后会是什么样子呢?感兴趣的朋友可以自己试一下