gpt4 book ai didi

linq - 使用 LINQ 在一行中对一维实例数组应用操作

转载 作者:行者123 更新时间:2023-12-02 02:31:36 25 4
gpt4 key购买 nike

考虑以下结构:

internal struct Coordinate
{
public Double Top { get; set; }
public Double Left { get; set; }
}

internal struct Dimension
{
public Double Height { get; set; }
public Double Width { get; set; }
}

internal struct Property
{
public Boolean Visible { get; set; }
internal String Label { get; set; }
public String Value { get; set; }
internal Coordinate Position { get; set; }
public Dimension Dimensions { get; set; }
}

我需要操作 20 个左右的 Property 实例。我想尽可能干净地这样做...是否可以在一行代码中对 Property 数组应用多个操作?

我在想一些事情:

new []
{
InstanceOfProperty,
InstanceOfProperty,
InstanceOfProperty ...
}.Each(p => p.Dimensions.Height = 100.0);

最佳答案

如果你自己写了Each采取Action<T>的方法代表,你可以。

编辑:

实际上它不会起作用。您正在使用值类型。为什么它不能是 class

两者都是 DimensionProperty必须是该分配的引用类型才能在处理列表后反射(reflect)出来。

关于linq - 使用 LINQ 在一行中对一维实例数组应用操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3617439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com