无法将null分配给类型为array的匿名属性 [英] Cannot assign null to anonymous property of type array

查看:81
本文介绍了无法将null分配给类型为array的匿名属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有(Pilot)个对象的任何数组,它们具有(Hanger)属性,该属性可以为null,其本身具有(List<Plane>)属性.出于测试目的,我想将此简化为具有属性PilotName(字符串)和Planes(数组)的匿名对象,并将其展平"为一个匿名对象,但不确定如何处理null Hanger属性或空的PlanesList .

I have any array of (Pilot) objects with a (Hanger) property, which may be null, which itself has a (List<Plane>) property. For testing purposes, I want to simplify and 'flatten' this to an anonymous object with properties PilotName (string) and Planes (array) but not sure how to handle a null Hanger property or an empty PlanesList.

(为什么要使用匿名对象?因为我正在测试的API对象是只读的,并且我希望测试是声明性的":自包含,简单且易读...但是我愿意接受其他建议.另外,我正在尝试了解有关LINQ的更多信息.)

(Why anonymous objects? Because the objects of the API I'm testing are read only and I want the test to be 'declarative': self contained, simple and readable... but I'm open to other suggestions. Also I'm trying to learn more about LINQ.)

示例

class Pilot
{
    public string Name;
    public Hanger Hanger;
}

class Hanger
{
    public string Name;
    public List<Plane> PlaneList;
}

class Plane
{
    public string Name;
}

[TestFixture]
class General
{
    [Test]
    public void Test()
    {
        var pilots = new Pilot[]
        {
            new Pilot() { Name = "Higgins" },
            new Pilot()
            {
                Name = "Jones", Hanger = new Hanger()
                {
                    Name = "Area 51",
                    PlaneList = new List<Plane>()
                    {
                        new Plane { Name = "B-52" },
                        new Plane { Name = "F-14" }
                    }
                }
            }
        };

        var actual = pilots.Select(p => new
        {
            PilotName = p.Name,
            Planes = (p.Hanger == null || p.Hanger.PlaneList.Count == 0) ? null : p.Hanger.PlaneList.Select(h => ne
            {
                PlaneName = h.Name
            }).ToArray()
        }).ToArray();

        var expected = new[] {
            new { PilotName = "Higgins", Planes = null },
            new
            {
                PilotName = "Jones",
                Planes = new[] {
                    new { PlaneName = "B-52" },
                    new { PlaneName = "F-14" }
                }
            }
        };

        Assert.That(actual, Is.EqualTo(expected));
    }

直接的问题是expected... Planes = null行与

不能分配给匿名类型属性,但要承认潜在的问题可能是,在actual中使用null并不是首先是使用null的最佳方法.

Cannot assign to anonymous type property but admit the underlying problem may be that using null in actual is using null is not the best approach in the first place.

有什么想法如何在expected中分配空数组或采用与actual中的null不同的方法吗?

Any ideas how to either assign the null array in expected or take a different approach than null in actual?

推荐答案

发生了两件事:

首先,当您使用new { Name = Value}构造匿名类型的实例时,为了构建该类型,编译器需要能够计算出Value type .只是null本身没有类型,因此编译器将不知道为您的Planes成员提供哪种类型.

Firstly, when you construct an instance of an anonymous type using new { Name = Value}, in order to build the type the compiler needs to be able to work out the type of Value. Just null on its own doesn't have a type, so the compiler wouldn't know what type to give your Planes member.

现在,如果您使用的是命名类型的值,则只需说出(type)null即可,但是因为您想要另一个匿名类型的数组,所以无法 refer 到(是匿名的!).

Now, if you were using a named type for the value, you could just say (type)null and be done, BUT because you want an array of another anonymous type, there's no way to refer to is (it's anonymous!).

那么如何获取null作为匿名类型数组的类型?好吧,C#规范保证成员具有相同名称和类型(以相同顺序!)的匿名类型被统一;也就是说,如果我们说

So how do you get null typed as array of an anonymous type? Well, the C# spec guarantees that anonymous types with members the same names and types (in the same order!) are unified; that is, if we say

var a = new { Foo = "Bar" };
var b = new { Foo = "Baz" };

然后ab具有相同的类型.我们可以利用这个事实来得到我们合适类型的null:

then a and b have the same type. We can use this fact to get our suitably-typed null thus:

var array = (new[] { new { PlaneName = "" } });
array = null;

虽然不漂亮,但是可以工作-现在array具有正确的 type 类型,但是具有null .如此编译:

It's not pretty but it works - now array has the right type but a null value. So this compiles:

        var array = new[] { new { PlaneName = "" } };
        array = null;

        var expected = new[]
                           {
                               new
                                   {
                                       PilotName = "Higgins",
                                       Planes = array
                                   },
                               new
                                   {
                                       PilotName = "Higgins",
                                       Planes = new[]
                                                    {
                                                        new { PlaneName = "B-52" },
                                                        new { PlaneName = "F-14" }
                                                    }
                                   }
                           };

这篇关于无法将null分配给类型为array的匿名属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆