I have any array of (Pilot) objects with a (Hanger) property, which may be null, which itself has a (List) property. For test
There are two things happening:
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.
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!).
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" };
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;
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" }
}
}
};
Just use default(Plane[]) instead of null.
You have to use a typed null:
(List<Plane>)null
Or
(Plane[])null
Otherwise the compiler has no idea what type you want the anonymous type's member to be.
Update
As @AakashM has rightly pointed out - this solves your problem of assigning a null to an anonymous member - but doesn't actually compile - and if it did it wouldn't allow you to refer to these members.
A fix would be to do this (unfortunately both the null and the anonymous Planes array will need casting:
var expected = new[] {
new {
PilotName = "Higgins",
Planes = (IEnumerable)null
},
new {
PilotName = "Higgins",
Planes = (IEnumerable)new [] {
new { PlaneName = "B-52" },
new { PlaneName = "F-14" }
}
}
};
So use IEnumerable as the member type. You could also use IEnumerable<object> but the effect will be the same either way.
Or - you could use IEnumerable<dynamic> as the common type - this would let you do this:
Assert.AreEqual("B-52", expected[1].Planes.First().PlaneName);