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.
This commit is contained in:
parent
c8df81ef91
commit
68c52adef4
|
|
@ -3,7 +3,6 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using Microsoft.AspNet.Mvc.ModelBinding;
|
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||||
using Microsoft.AspNet.Mvc.ModelBinding.Metadata;
|
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc
|
namespace Microsoft.AspNet.Mvc
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,10 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// 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.
|
// 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.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Microsoft.AspNet.Mvc.ModelBinding;
|
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||||
using Microsoft.AspNet.Mvc.ModelBinding.Metadata;
|
|
||||||
using Microsoft.Framework.Internal;
|
using Microsoft.Framework.Internal;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc.ApplicationModels
|
namespace Microsoft.AspNet.Mvc.ApplicationModels
|
||||||
|
|
|
||||||
|
|
@ -281,11 +281,12 @@ namespace Microsoft.AspNet.Mvc
|
||||||
|
|
||||||
private static ParameterDescriptor CreateParameterDescriptor(ParameterModel parameterModel)
|
private static ParameterDescriptor CreateParameterDescriptor(ParameterModel parameterModel)
|
||||||
{
|
{
|
||||||
var parameterDescriptor = new ParameterDescriptor()
|
var parameterDescriptor = new ControllerParameterDescriptor()
|
||||||
{
|
{
|
||||||
Name = parameterModel.ParameterName,
|
Name = parameterModel.ParameterName,
|
||||||
ParameterType = parameterModel.ParameterInfo.ParameterType,
|
ParameterType = parameterModel.ParameterInfo.ParameterType,
|
||||||
BindingInfo = parameterModel.BindingInfo
|
BindingInfo = parameterModel.BindingInfo,
|
||||||
|
ParameterInfo = parameterModel.ParameterInfo,
|
||||||
};
|
};
|
||||||
|
|
||||||
return parameterDescriptor;
|
return parameterDescriptor;
|
||||||
|
|
@ -293,11 +294,12 @@ namespace Microsoft.AspNet.Mvc
|
||||||
|
|
||||||
private static ParameterDescriptor CreateParameterDescriptor(PropertyModel propertyModel)
|
private static ParameterDescriptor CreateParameterDescriptor(PropertyModel propertyModel)
|
||||||
{
|
{
|
||||||
var parameterDescriptor = new ParameterDescriptor()
|
var parameterDescriptor = new ControllerBoundPropertyDescriptor()
|
||||||
{
|
{
|
||||||
BindingInfo = propertyModel.BindingInfo,
|
BindingInfo = propertyModel.BindingInfo,
|
||||||
Name = propertyModel.PropertyName,
|
Name = propertyModel.PropertyName,
|
||||||
ParameterType = propertyModel.PropertyInfo.PropertyType,
|
ParameterType = propertyModel.PropertyInfo.PropertyType,
|
||||||
|
PropertyInfo = propertyModel.PropertyInfo,
|
||||||
};
|
};
|
||||||
|
|
||||||
return parameterDescriptor;
|
return parameterDescriptor;
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A descriptor for model bound properties of a controller.
|
||||||
|
/// </summary>
|
||||||
|
public class ControllerBoundPropertyDescriptor : ParameterDescriptor
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the <see cref="PropertyInfo"/> for this property.
|
||||||
|
/// </summary>
|
||||||
|
public PropertyInfo PropertyInfo { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A descriptor for method parameters of an action method.
|
||||||
|
/// </summary>
|
||||||
|
public class ControllerParameterDescriptor : ParameterDescriptor
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the <see cref="ParameterInfo"/>.
|
||||||
|
/// </summary>
|
||||||
|
public ParameterInfo ParameterInfo { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -17,11 +17,14 @@ namespace Microsoft.AspNet.Mvc
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var applicationModel = new ApplicationModel();
|
var applicationModel = new ApplicationModel();
|
||||||
var controller = new ControllerModel(typeof(TestController).GetTypeInfo(),
|
var controller = new ControllerModel(
|
||||||
new List<object>() { });
|
typeof(TestController).GetTypeInfo(),
|
||||||
|
new List<object>() { });
|
||||||
|
|
||||||
|
var propertyInfo = controller.ControllerType.GetProperty("BoundProperty");
|
||||||
controller.ControllerProperties.Add(
|
controller.ControllerProperties.Add(
|
||||||
new PropertyModel(
|
new PropertyModel(
|
||||||
controller.ControllerType.GetProperty("BoundProperty"),
|
propertyInfo,
|
||||||
new List<object>() { })
|
new List<object>() { })
|
||||||
{
|
{
|
||||||
BindingInfo = BindingInfo.GetBindingInfo(new object[] { new FromQueryAttribute() }),
|
BindingInfo = BindingInfo.GetBindingInfo(new object[] { new FromQueryAttribute() }),
|
||||||
|
|
@ -43,8 +46,12 @@ namespace Microsoft.AspNet.Mvc
|
||||||
var descriptors = ControllerActionDescriptorBuilder.Build(applicationModel);
|
var descriptors = ControllerActionDescriptorBuilder.Build(applicationModel);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
var property = Assert.Single(descriptors.Single().BoundProperties);
|
var controllerDescriptor = Assert.Single(descriptors);
|
||||||
|
|
||||||
|
var parameter = Assert.Single(controllerDescriptor.BoundProperties);
|
||||||
|
var property = Assert.IsType<ControllerBoundPropertyDescriptor>(parameter);
|
||||||
Assert.Equal("BoundProperty", property.Name);
|
Assert.Equal("BoundProperty", property.Name);
|
||||||
|
Assert.Equal(propertyInfo, property.PropertyInfo);
|
||||||
Assert.Equal(typeof(string), property.ParameterType);
|
Assert.Equal(typeof(string), property.ParameterType);
|
||||||
Assert.Equal(BindingSource.Query, property.BindingInfo.BindingSource);
|
Assert.Equal(BindingSource.Query, property.BindingInfo.BindingSource);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue