aspnetcore/src/Microsoft.AspNet.Mvc.ModelB.../BinderMetadata/ModelBinderAttribute.cs

77 lines
2.1 KiB
C#

// 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 Microsoft.AspNet.Mvc.ModelBinding;
namespace Microsoft.AspNet.Mvc
{
/// <summary>
/// An attribute that can specify a model name or type of <see cref="IModelBinder"/> to use for binding.
/// </summary>
[AttributeUsage(
// Support method parameters in actions.
AttributeTargets.Parameter |
// Support properties on model DTOs.
AttributeTargets.Property |
// Support model types.
AttributeTargets.Class |
AttributeTargets.Enum |
AttributeTargets.Struct,
AllowMultiple = false,
Inherited = true)]
public class ModelBinderAttribute : Attribute, IModelNameProvider, IBinderTypeProviderMetadata
{
private Type _binderType;
private BindingSource _bindingSource;
/// <inheritdoc />
public Type BinderType
{
get
{
return _binderType;
}
set
{
if (value != null)
{
if (!typeof(IModelBinder).IsAssignableFrom(value))
{
throw new InvalidOperationException(
Resources.FormatBinderType_MustBeIModelBinder(
value.FullName,
typeof(IModelBinder).FullName));
}
}
_binderType = value;
}
}
/// <inheritdoc />
public BindingSource BindingSource
{
get
{
if (_bindingSource == null && _binderType != null)
{
return BindingSource.Custom;
}
return _bindingSource;
}
set
{
_bindingSource = value;
}
}
/// <inheritdoc />
public string Name { get; set; }
}
}