C#9.0记录-反射和一般约束 [英] c# 9.0 records - reflection and generic constraints

查看:122
本文介绍了C#9.0记录-反射和一般约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于新记录功能的两个问题:

Two questions regarding the new records feature :


  1. 如何使用反射识别记录?寻找这里也许有
    是检测<$办法c $ c> EqualityContract ,但我不确定这是否可行?

  1. How do I recognize a record using reflection ? looking here maybe there is a way to detect the EqualityContract but I am not sure if that is the way to go ?

有可能吗?是否具有泛型约束,即泛型类型是记录?也就是说,是否有可能使用约束条件来指示类型参数T必须是记录类?

Is it possible to have a generic constraint that a generic type is a record ? that is if it is possible to indicate that type parameter T must be a record class using a constraint ?

Rationale-我正在尝试编写代码以测试类型是否为数据。从功能上讲-(1)不可变的(2)具有值语义。

Rationale - I am trying to write code to test if a type is "Data" in a functional sense - that is is (1) Immutable and (2) has value semantics.

我从一小部分基本/核心/白名单开始数据类型(即 int或 DateTime)。为了检查不变性,我可以检查所有字段和自动道具是否为只读/无设置器,并且是否为数据。本身(从上面的列表开始)等。

I start from a small list of basic/core/whitelisted types that are "Data" (ie 'int' or 'DateTime'). To check for immutability I can check that all fields and auto props are readonly/no setter and are "Data" themselves (starting with above list) etc.

检查值语义比较困难,但是我可以只允许上述类型+包含它们的记录作为 record 保证值语义(我无法检查用户定义的'Equals'和'GetHashCode')。这就是为什么我需要检查类型是否为记录的原因。

Checking for Value semantics is harder, however I am ok to only allow above types + records containing them as record guarantee value semantics (I can check for no user defined 'Equals' and 'GetHashCode'). This why I need to check if a type is a record.

推荐答案



  1. 如何使用反射识别记录?


如果尝试在 sharplab.io 你会看到记录类是实现 IEquatable< T> 接口的常用类,并且包含用于比较和克隆记录类实例的其他成员。没有特殊的属性指示该类是记录类

If you try record classes in sharplab.io you'll see that record classes are usual classes that implement IEquatable<T> interface and contain additional members that are used to compare and clone instances of the record class. There is no special attributes that indicate that the class is a record class.

所以我想


看起来此处也许有一种方法来检测所述
EqualityContract 但我不确定这是否可行?

looking here maybe there is a way to detect the EqualityContract but I am not sure if that is the way to go ?

可以使用反射来确定类是否具有这种属性,但这不是100%保证具有此类属性的类是记录类。

It is possible to determine using reflection if a class has such property, but this is not a 100% guarantee that the class with such property is a record class.



  1. 是否有一个泛型约束,泛型类型是
    记录?也就是说,是否有可能使用约束指示类型参数T
    必须是记录类?


不可能。


  1. 记录提案页面不包含任何有关指定通用类型参数 T 必须为记录的信息

  2. 如果您在冠军记录页面上的注释> ,您将了解到无法指定类似的内容,其中T:记录 C#9 中的$ c>。此外,还计划消除 C#10 中的记录和类之间的任何有意义的语义差异。这样记录的功能,例如 with 也将适用于类。添加记录约束将使该目标无法实现。

  1. Records proposal page does not contain any information about specifying that a generic type parameter T must be a record class.
  2. If you read discussion under this comment at Champion records page you'll learn that there is no way to specify something like where T : record in C# 9. Moreover there are plans to eliminate any meaningful semantic difference between a record and a class in C# 10. So that records' features like with will be available for classes too. Adding record constraint will make this goal not achievable.

这篇关于C#9.0记录-反射和一般约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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