Unwrap nullable types in PropertyHelper prior to looking up properties
Fixes #721
This commit is contained in:
parent
0f0d44c6b3
commit
e7c2faff32
|
|
@ -198,6 +198,10 @@ namespace Microsoft.AspNet.Mvc
|
||||||
Func<PropertyInfo, PropertyHelper> createPropertyHelper,
|
Func<PropertyInfo, PropertyHelper> createPropertyHelper,
|
||||||
ConcurrentDictionary<Type, PropertyHelper[]> cache)
|
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.
|
// Using an array rather than IEnumerable, as target will be called on the hot path numerous times.
|
||||||
PropertyHelper[] helpers;
|
PropertyHelper[] helpers;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
// 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.
|
// 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.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
@ -114,6 +115,32 @@ namespace Microsoft.AspNet.Mvc
|
||||||
Assert.Equal("Prop6", helper.Name);
|
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]
|
[Fact]
|
||||||
public void PropertyHelper_WorksForStruct()
|
public void PropertyHelper_WorksForStruct()
|
||||||
{
|
{
|
||||||
|
|
@ -350,5 +377,10 @@ namespace Microsoft.AspNet.Mvc
|
||||||
|
|
||||||
public static string NotVisible4 { get; set; }
|
public static string NotVisible4 { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private struct MyStruct
|
||||||
|
{
|
||||||
|
public string Foo { get; set; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue