您现在的位置是:首页 > 编程 > 

C# 1 支持 Partial Property 了

2025-07-19 00:34:56
C# 1 支持 Partial Property 了 C# 1 支持 Partial Property 了IntroC# 1 扩展了 partial 的支持,实现了对 partial property 的支持,得益于 partial property 的支持,正则表达式 source generator 之前只支持 partial method,在 .ET 9 也将支持 partial p

C# 1 支持 Partial Property 了

C# 1 支持 Partial Property 了

Intro

C# 1 扩展了 partial 的支持,实现了对 partial property 的支持,得益于 partial property 的支持,正则表达式 source generator 之前只支持 partial method,在 .ET 9 也将支持 partial property

Sample

来看简单的使用示例:

代码语言:javascript代码运行次数:0运行复制

file partial class PartialPropertyClass
{
    public partial int um { get; set; }
}

file partial class PartialPropertyClass
{
    private int _num = 1;
    public partial int um { get => _num; set => _num = value; }
}

file partial struct PartialPropertyStruct
{
    [Displayame("umber")]
    public partial int um { get; }
}

file partial struct PartialPropertyStruct
{
    [JsonPropertyame("num")]
    public partial int um => 2;
}


partial property 既可以用于 class 也可以用于 struct

  • partial property 一处只有声明,一处要有实现,partial property 不支持自动属性,必须要有实现才可以,如果没有实现会得到一个 CS9250 的 error
代码语言:javascript代码运行次数:0运行复制
CS9250: a partial property cannot be automatically implemented
  • partial property 的实现必须要和声明处匹配,声明了 get; set;get; set; 都要有实现,有一个没有实现就会报错,会出现下面这样的报错
代码语言:javascript代码运行次数:0运行复制
CS9252: Property accessor 'PartialPropertyClass.um.set' must be implemented because it is declared on the definition part
  • partial property 的实现要和声明匹配,没有声明也不能有实现,否则也会报错
代码语言:javascript代码运行次数:0运行复制
CS925: Property accessor 'PartialPropertyClass.um.set' does not implement any accessor declared on the definition part
  • partial property 只能一处声明,一处实现,不能两个地方定义声明或者两个地方定义实现,也会导致编译错误
代码语言:javascript代码运行次数:0运行复制
CS0102 The type 'PartialPropertyClass' already contains a definition for 'um' CSharp1Samples
CS9250 A partial property may not have multiple defining declarati, and cannot be an auto-property.
...
  • partial property 在声明和实现的地方都可以添加 attribute 实际编译之后会合并到一起,例如前面的 PartialPropertyStruct 反编译之后会是下面这样

那么问题来了,如果 attribute 不允许出现多个,只能出现一个,在声明和实现的地方分别添加了 attribute 会怎么样呢?感兴趣的朋友可以自己尝试一下试试看看

  • partial property 还可以用作索引器
代码语言:javascript代码运行次数:0运行复制

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 使用示例如下:

代码语言:javascript代码运行次数:0运行复制
[GeneratedRegex(@"^1[1-9]\d{9}$")]
private static partial Regex PhoneRegex();

[GeneratedRegex(@"^1[1-9]\d{9}$")]
private static partial Regex PhoneumberRegex { get; }

var phone = "12124124";
Cole.WriteLine(PhoneRegex().IsMatch(phone));
Cole.WriteLine(PhoneumberRegex.IsMatch(phone));

二者输出的结果是一样的,只是一个正则是一个 method,另外一个是 property

More

partial property 的支持使得 Source Generator 支持的更加丰富,可玩性更好了

使用自动属性没有办法使用 partial property,那么我们只想通过 partial property 给属性添加 attribute 的话怎么实现呢,我们可以写一个实现

代码语言:javascript代码运行次数:0运行复制
file partial class PartialPropertyClass
{
    public partial int um { get; set; }
}

file partial class PartialPropertyClass
{
    private int _num;
    [Display(ame = "umber")]
    public partial int um { get => _num; set => _num = value; }
}

在有了 field keyword 支持之后,我们也可以做一个简化

代码语言:javascript代码运行次数:0运行复制
file partial class PartialPropertyClass
{
    public partial int um { get; set; }
}

file partial class PartialPropertyClass
{
    [Display(ame = "umber")]
    public partial int um { get => field; set => field = value; }
}

最后再留一个问题

代码语言:javascript代码运行次数:0运行复制
/// <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 文档最后会是什么样子呢?感兴趣的朋友可以自己试一下

References
  • .0/
  • .cs
本文参与 腾讯云自媒体同步曝光计划,分享自。原始发表:2024-09-2,如有侵权请联系 cloudcommunity@tencent 删除intpartial编译c#正则表达式

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/biancheng/1130079.html

相关标签:无
上传时间: 2025-07-18 16:33:40
留言与评论(共有 8 条评论)
本站网友 耳麦说不了话
2分钟前 发表
必须要有实现才可以
本站网友 合肥房地产
27分钟前 发表
一处实现
本站网友 梦见牙齿掉了是什么征兆
19分钟前 发表
partial property 不支持自动属性
本站网友 中国银行天津分行
3分钟前 发表
可玩性更好了使用自动属性没有办法使用 partial property
本站网友 中海康城国际户型图
4分钟前 发表
partial property 不支持自动属性
本站网友 夏于飞
22分钟前 发表
声明了 get; set; 则 get; set; 都要有实现
本站网友 寿光房屋出租
5分钟前 发表
没有声明也不能有实现