构造函数,是在初始化对象时,把数据传给对象。那Deconstruct正好相反,是从对象中把想要的数据返回。
下面看一个使用场景,下面是定义一个record的Order,我们可以用元数据的方式,从Order对象中把no,orderTime取出来,这是怎么做到的呢?
//Order定义成record
record Order(string No, DateTime OrderTime, string Address);
//使用Order
var (no, orderTime, _) = new Order("T000001", DateTime.Now, "北京市海淀区");
Console.WriteLine(no);
Console.WriteLine(orderTime);
其实实现很简单,只要在类内部定义一个Deconstruct的方法就可以了,下面是另一个Order为 class的实例。除了基本的属性,还实现了一个Deconstruct,参数是两个out的参数,这个方法只返回No和经过简化的Goods的ID集合。这里只是给出一个例子,对于想把什么数据解构出来,以什么形式解构出来,可以根据自己的需要。同时Deconstruct可以有重载,但是,参数一样时,函数就有二义性了。【其确我觉得可以通过(string no,List<int> goodsIds)来解决二义性,但现在不好用】
classOrder
{
publicOrder(string no, DateTime orderTime, string address)
{
No = no;
OrderTime = orderTime;
Address = address;
}
publicstring No { get; set; }
public DateTime OrderTime { get; set; }
publicstring Address { get; set; }
public List<Goods> Goodses { get; set; } = new List<Goods>();
publicvoidDeconstruct(outstring no, out List<int> goodsIds)
{
no = No;
goodsIds = Goodses.Select(a => a.ID).ToList();
}
publicvoidDeconstruct(outstring no, out DateTime orderTime)
{
no = No;
orderTime = OrderTime;
}
}
var (no, goodsIds) = new Order("T000001", DateTime.Now, "北京市海淀区")
{
Goodses = new List<Goods>
{
new Goods { ID = 1, Name = "商品A", Price = 10m },
new Goods { ID = 2, Name = "商品B", Price = 15m },
}
};
Console.WriteLine($"OrderNo:{no}");
foreach (var goodsId in goodsIds)
{
Console.WriteLine($" GoodsId:{goodsId}");
}
另外,Deconstruct可以通过扩展的方式,给一些类型增加解构功能,下面是对Exception的解构,取出Message和InnerException,当然这只是一个Demo,只提供了一条路子,具体应用可以灵活定义。
static classExceptionExtensions
{
publicstatic void Deconstruct(this Exception? exception, out string? message, out Exception? innerException)
{
message = exception?.Message;
innerException = exception?.InnerException;
}
publicstatic void Deconstruct(this Exception? exception, out string? message, out string? innerMessage, out Exception? innerInnerException)
{
message = exception?.Message;
innerMessage = exception?.InnerException?.Message;
innerInnerException = exception?.InnerException?.InnerException;
}
}
try
{
thrownewException("1级错误", newException("2级错误"));
}
catch (Exception exc)
{
var (msg, (innerMsg, _)) = exc;
Console.WriteLine(msg);
Console.WriteLine(innerMsg);
}
奇奇怪怪的C#知识又增加了一点。