- How do I recognize a record using reflection ?
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
.
So I guess that there is no way to determine if a class is a record class 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 ?
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.
- 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 ?
It is not possible.
- Records proposal page does not contain any information about specifying that a generic type parameter
T
must be a record class.
- 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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…