我正在try 这样做:
image.Layers
它为除Parent
层之外的所有层返回IEnumerable<Layer>
,但在某些情况下,我只想这样做:
image.Layers.With(image.ParentLayer);
因为它只在少数几个地方使用,而通常使用的image.Layers
个是image.Layers
个.这就是为什么我不想创建另一个也返回Parent
层的属性.
我正在try 这样做:
image.Layers
它为除Parent
层之外的所有层返回IEnumerable<Layer>
,但在某些情况下,我只想这样做:
image.Layers.With(image.ParentLayer);
因为它只在少数几个地方使用,而通常使用的image.Layers
个是image.Layers
个.这就是为什么我不想创建另一个也返回Parent
层的属性.
一种方法是从项(例如数组)中创建一个单例序列,然后将其添加到原始项上:
image.Layers.Concat(new[] { image.ParentLayer } )
如果你经常这样做,考虑写一个Append
(或类似)的扩展方法,比如listed here个,让你这样做:
image.Layers.Append(image.ParentLayer)
.NET Core Update(per the "best" answer below):
现在,
Append
和Prepend
已添加到.NET标准框架,因此您不再需要编写自己的.只需这样做:image.Layers.Append(image.ParentLayer)