That will work. Does it now?
Personally, I wouldn't do it quite like that (but close):
int?[] result = null;
var enumerable = propertyValue as IEnumerable;
if(enumerable != null && enumerable.All(i=>i is int?)
result = enumerable.Cast<int?>().ToArray()
return result;
This will return either a int?[] or null.
Personally I would add checks that throw exceptions so I can identify why I'm getting null. After all, it might be a different object, or a null int?[].
This did not work as "null is int?" always equals false.
Instead, replace with "i=>i==null || i is int"
Here is the full unit test that passes:
[TestMethod]
public void TryPart1()
{
int?[] value = new int?[10];
for (int i = 0; i < value.Length; i++)
{
value[i] = (i > 4) ? (int?)null : 0;
}
object a = value;
var result = TryPart2(a);
Assert.IsTrue(result != null);
Assert.IsTrue(result.Length == 10);
}
public int?[] TryPart2(object a)
{
int?[] result = null;
var enumerable = a as IEnumerable;
if (enumerable != null)
{
var objects = enumerable.Cast<object>().ToArray();
if (objects.All(o => o==null || o is int))
result = objects.Cast<int?>().ToArray();
}
return result;
}