问题描述
C# 9 records feature specification包括以下内容:
记录类型包含两个复制成员:
使用记录类型的单个参数的构造函数。它是 称为“复制构造函数”。综合公众 具有编译器保留名称的无参数实例“克隆”方法
但是我似乎无法称呼这两个复制成员中的一个:
public record R(int A);
// ...
var r2 = new R(r); // ERROR: inaccessible due to protection level
var r3 = r.Clone(); // ERROR: R does not contain a definition for Clone
据此,我知道构造函数受到保护,因此无法在记录的继承层次结构之外进行访问。这样我们就剩下了这样的代码:
var r4 = r with { };
但是克隆呢?根据上面的说明,克隆方法是公开的。但是它叫什么名字呢?还是它是一个有效的随机字符串,因此不应在记录的继承层次结构之外调用它?如果是这样,深度复制记录的正确方法是什么?从规范看来,一个人能够创建自己的克隆方法。是这样吗,它将是如何工作的一个例子?
解决方法
但是克隆呢?
var r4 = r with { };
在r上执行浅表克隆。
根据上述规范,克隆方法是公共的。但是它叫什么名字?
C#编译器有一个相当普遍的技巧,它可以为生成的成员提供在C#中非法但在IL中合法的成员名称,因此,即使它们是公共的,也只能从编译器中调用它们。在这种情况下,Clone
方法的名称为<Clone>$
。
如果是这样,深度复制记录的正确方法是什么?
深复制使您不走运。但是,由于记录在理想情况下应该是不变的,因此,浅表副本,深表副本和原始实例在实践上应该没有区别。
从规范看来,一个人可以创建自己的克隆方法。是这样吗,它将是如何工作的一个例子?
不幸的是,这并没有降到C#9,但是很有可能会出现在C#10中。
,关于记录的克隆,是的,看来/* REF https://stackoverflow.com/a/49575979 */
/* REF https://stackoverflow.com/questions/26607330/css-display-none-and-opacity-animation-with-keyframes-not-working/64857102#64857102 */
body,html { /* eye candy */
background: #444; display: flex; min-height: 100vh; align-items: center; justify-content: center;
}
button { font-size: 4em; border-radius: 20px; margin-left: 60px;}
div { /* eye candy */
width: 200px; height: 100px; border-radius: 20px;
background: green; display: flex; align-items: center; justify-content: center;
font-family: sans-serif; font-size: 2em; color: white; text-align: center;
text-shadow: 0px 2px 4px rgba(0,.6);
}
/* using this extra .active class prevents that there is an animation already on loading */
.active {
animation: fadeAndHideBack 1s linear forwards;
}
.play {
opacity: 0;
/* learning curve: setting background "awaits" animation finish,setting scale prematurely jumps to it,then doing animation from there */
animation: fadeAndHide 1s linear forwards;
}
@keyframes fadeAndHide {
0% { opacity: 1; }
99.9% { opacity: 0; max-height: 100px; }
100% { opacity: 0; max-height: 0; }
}
@keyframes fadeAndHideBack {
0% { opacity: 0; max-height: 0; }
0.1% { opacity: 0; max-height: 100px; }
100% { opacity: 1; }
}
将为您创建一个深层克隆,如下例所示:
记录示例:
<div class="target"></div>
<button onclick="toggleTarget()">
Toggle
</button>
输出:
with
此外,如我们所见,如果我们明确声明了记录属性,那么我们还可以更改记录属性的开箱即用的不变性。我们也可以编写自己的构造函数。
如果我们对一个普通的类进行上述操作,则副本将是预期的浅表克隆。
课程示例:
using System;
public class Program
{
public static void Main()
{
var person1 = new Person
{
Hair = "brown",Skin = "tawny"
};
var inhabitant1 = new Inhabitant
{
Name = "Moreno",Profile = person1
};
var inhabitant2 = new Inhabitant
{
Name = "Branquinho",Profile = inhabitant1.Profile with { Skin = "sparkle" }
};
Console.WriteLine("--- Brown,Tawny ---");
Console.WriteLine(inhabitant1.ToString());
Console.WriteLine(inhabitant2.ToString());
inhabitant1.Profile.Hair = "painted";
Console.WriteLine("\n--- Painted,Tawny ---");
Console.WriteLine(inhabitant1.ToString());
Console.WriteLine(inhabitant2.ToString());
inhabitant2.Profile.Skin = "sunburn";
Console.WriteLine("\n--- Painted,Tawny ---");
Console.WriteLine(inhabitant1.ToString());
Console.WriteLine(inhabitant2.ToString());
person1.Skin = "green";
person1.Hair = "green";
Console.WriteLine("\n--- Green,Green ---");
Console.WriteLine(inhabitant1.ToString());
Console.WriteLine(inhabitant2.ToString());
inhabitant1.Profile = new Person
{
Skin = "blue",Hair = "blue"
};
Console.WriteLine("\n--- Blue,Blue ---");
Console.WriteLine(inhabitant1.ToString());
Console.WriteLine(inhabitant2.ToString());
}
public record Inhabitant()
{
public string Name { get; set; }
public Person Profile { get; set; }
}
public record Person()
{
public string Hair { get; set; }
public string Skin { get; set; }
}
}
输出:
--- Brown,Tawny ---
Inhabitant { Name = Moreno,Profile = Person { Hair = brown,Skin = tawny } }
Inhabitant { Name = Branquinho,Skin = sparkle } }
--- Painted,Profile = Person { Hair = painted,Skin = sunburn } }
--- Green,Green ---
Inhabitant { Name = Moreno,Profile = Person { Hair = green,Skin = green } }
Inhabitant { Name = Branquinho,Skin = sunburn } }
--- Blue,Blue ---
Inhabitant { Name = Moreno,Profile = Person { Hair = blue,Skin = blue } }
Inhabitant { Name = Branquinho,Skin = sunburn } }