From 68c52adef40c8da833e4f6cd65154b121368090a Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Thu, 30 Jul 2015 10:14:40 -0700 Subject: [PATCH] Fix #2837 - Make it easier to get ParameterInfo/PropertyInfo For a typical configuration, it's now possible to cast a parameter descriptor to ControllerParameterDescriptor or ControllerBoundPropertyDescriptor to access the corresponding reflection type. --- .../ParameterDescriptor.cs | 1 - .../ApplicationModels/ParameterModel.cs | 2 -- .../ControllerActionDescriptorBuilder.cs | 8 +++++--- .../ControllerBoundPropertyDescriptor.cs | 18 ++++++++++++++++++ .../ControllerParameterDescriptor.cs | 18 ++++++++++++++++++ .../ControllerActionDescriptorBuilderTest.cs | 15 +++++++++++---- 6 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 src/Microsoft.AspNet.Mvc.Core/ControllerBoundPropertyDescriptor.cs create mode 100644 src/Microsoft.AspNet.Mvc.Core/ControllerParameterDescriptor.cs diff --git a/src/Microsoft.AspNet.Mvc.Abstractions/ParameterDescriptor.cs b/src/Microsoft.AspNet.Mvc.Abstractions/ParameterDescriptor.cs index 37d6d3d28a..fb8d068201 100644 --- a/src/Microsoft.AspNet.Mvc.Abstractions/ParameterDescriptor.cs +++ b/src/Microsoft.AspNet.Mvc.Abstractions/ParameterDescriptor.cs @@ -3,7 +3,6 @@ using System; using Microsoft.AspNet.Mvc.ModelBinding; -using Microsoft.AspNet.Mvc.ModelBinding.Metadata; namespace Microsoft.AspNet.Mvc { diff --git a/src/Microsoft.AspNet.Mvc.Core/ApplicationModels/ParameterModel.cs b/src/Microsoft.AspNet.Mvc.Core/ApplicationModels/ParameterModel.cs index 57c0f94abf..795985b58a 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ApplicationModels/ParameterModel.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ApplicationModels/ParameterModel.cs @@ -1,12 +1,10 @@ // Copyright (c) .NET Foundation. 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.Collections.Generic; using System.Diagnostics; using System.Reflection; using Microsoft.AspNet.Mvc.ModelBinding; -using Microsoft.AspNet.Mvc.ModelBinding.Metadata; using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Mvc.ApplicationModels diff --git a/src/Microsoft.AspNet.Mvc.Core/ControllerActionDescriptorBuilder.cs b/src/Microsoft.AspNet.Mvc.Core/ControllerActionDescriptorBuilder.cs index f1f892d0f7..48ffceedbb 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ControllerActionDescriptorBuilder.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ControllerActionDescriptorBuilder.cs @@ -281,11 +281,12 @@ namespace Microsoft.AspNet.Mvc private static ParameterDescriptor CreateParameterDescriptor(ParameterModel parameterModel) { - var parameterDescriptor = new ParameterDescriptor() + var parameterDescriptor = new ControllerParameterDescriptor() { Name = parameterModel.ParameterName, ParameterType = parameterModel.ParameterInfo.ParameterType, - BindingInfo = parameterModel.BindingInfo + BindingInfo = parameterModel.BindingInfo, + ParameterInfo = parameterModel.ParameterInfo, }; return parameterDescriptor; @@ -293,11 +294,12 @@ namespace Microsoft.AspNet.Mvc private static ParameterDescriptor CreateParameterDescriptor(PropertyModel propertyModel) { - var parameterDescriptor = new ParameterDescriptor() + var parameterDescriptor = new ControllerBoundPropertyDescriptor() { BindingInfo = propertyModel.BindingInfo, Name = propertyModel.PropertyName, ParameterType = propertyModel.PropertyInfo.PropertyType, + PropertyInfo = propertyModel.PropertyInfo, }; return parameterDescriptor; diff --git a/src/Microsoft.AspNet.Mvc.Core/ControllerBoundPropertyDescriptor.cs b/src/Microsoft.AspNet.Mvc.Core/ControllerBoundPropertyDescriptor.cs new file mode 100644 index 0000000000..e055ffe224 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/ControllerBoundPropertyDescriptor.cs @@ -0,0 +1,18 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Reflection; + +namespace Microsoft.AspNet.Mvc +{ + /// + /// A descriptor for model bound properties of a controller. + /// + public class ControllerBoundPropertyDescriptor : ParameterDescriptor + { + /// + /// Gets or sets the for this property. + /// + public PropertyInfo PropertyInfo { get; set; } + } +} diff --git a/src/Microsoft.AspNet.Mvc.Core/ControllerParameterDescriptor.cs b/src/Microsoft.AspNet.Mvc.Core/ControllerParameterDescriptor.cs new file mode 100644 index 0000000000..100198d98a --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/ControllerParameterDescriptor.cs @@ -0,0 +1,18 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Reflection; + +namespace Microsoft.AspNet.Mvc +{ + /// + /// A descriptor for method parameters of an action method. + /// + public class ControllerParameterDescriptor : ParameterDescriptor + { + /// + /// Gets or sets the . + /// + public ParameterInfo ParameterInfo { get; set; } + } +} diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ControllerActionDescriptorBuilderTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ControllerActionDescriptorBuilderTest.cs index 2fe4708b23..da4aa2289f 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ControllerActionDescriptorBuilderTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ControllerActionDescriptorBuilderTest.cs @@ -17,11 +17,14 @@ namespace Microsoft.AspNet.Mvc { // Arrange var applicationModel = new ApplicationModel(); - var controller = new ControllerModel(typeof(TestController).GetTypeInfo(), - new List() { }); + var controller = new ControllerModel( + typeof(TestController).GetTypeInfo(), + new List() { }); + + var propertyInfo = controller.ControllerType.GetProperty("BoundProperty"); controller.ControllerProperties.Add( new PropertyModel( - controller.ControllerType.GetProperty("BoundProperty"), + propertyInfo, new List() { }) { BindingInfo = BindingInfo.GetBindingInfo(new object[] { new FromQueryAttribute() }), @@ -43,8 +46,12 @@ namespace Microsoft.AspNet.Mvc var descriptors = ControllerActionDescriptorBuilder.Build(applicationModel); // Assert - var property = Assert.Single(descriptors.Single().BoundProperties); + var controllerDescriptor = Assert.Single(descriptors); + + var parameter = Assert.Single(controllerDescriptor.BoundProperties); + var property = Assert.IsType(parameter); Assert.Equal("BoundProperty", property.Name); + Assert.Equal(propertyInfo, property.PropertyInfo); Assert.Equal(typeof(string), property.ParameterType); Assert.Equal(BindingSource.Query, property.BindingInfo.BindingSource); }