Unwrap nullable types in PropertyHelper prior to looking up properties

Fixes #721
This commit is contained in:
Pranav K 2014-06-27 15:03:51 -07:00
parent 0f0d44c6b3
commit e7c2faff32
2 changed files with 36 additions and 0 deletions

View File

@ -198,6 +198,10 @@ namespace Microsoft.AspNet.Mvc
Func<PropertyInfo, PropertyHelper> createPropertyHelper,
ConcurrentDictionary<Type, PropertyHelper[]> cache)
{
// Unwrap nullable types. This means Nullable<T>.Value and Nullable<T>.HasValue will not be
// part of the sequence of properties returned by this method.
type = Nullable.GetUnderlyingType(type) ?? type;
// Using an array rather than IEnumerable, as target will be called on the hot path numerous times.
PropertyHelper[] helpers;

View File

@ -1,6 +1,7 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Linq;
using System.Reflection;
using Xunit;
@ -114,6 +115,32 @@ namespace Microsoft.AspNet.Mvc
Assert.Equal("Prop6", helper.Name);
}
[Theory]
[InlineData(typeof(int?))]
[InlineData(typeof(DayOfWeek?))]
public void PropertyHelper_WorksForNullablePrimitiveAndEnumTypes(Type nullableType)
{
// Act
var properties = PropertyHelper.GetProperties(nullableType);
// Assert
Assert.Empty(properties);
}
[Fact]
public void PropertyHelper_UnwrapsNullableTypes()
{
// Arrange
var myType = typeof(MyStruct?);
// Act
var properties = PropertyHelper.GetProperties(myType);
// Assert
var property = Assert.Single(properties);
Assert.Equal("Foo", property.Name);
}
[Fact]
public void PropertyHelper_WorksForStruct()
{
@ -350,5 +377,10 @@ namespace Microsoft.AspNet.Mvc
public static string NotVisible4 { get; set; }
}
private struct MyStruct
{
public string Foo { get; set; }
}
}
}