This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
ModelBinderAttribute.cs
75 lines (63 loc) · 2.13 KB
/
ModelBinderAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// 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 Microsoft.AspNetCore.Mvc.ModelBinding;
namespace Microsoft.AspNetCore.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 BindingSource _bindingSource;
/// <summary>
/// Initializes a new instance of <see cref="ModelBinderAttribute"/>.
/// </summary>
public ModelBinderAttribute()
{
}
/// <summary>
/// Initializes a new instance of <see cref="ModelBinderAttribute"/>.
/// </summary>
/// <param name="binderType">A <see cref="Type"/> which implements <see cref="IModelBinder"/>.</param>
public ModelBinderAttribute(Type binderType)
{
if (binderType == null)
{
throw new ArgumentNullException(nameof(binderType));
}
BinderType = binderType;
}
/// <inheritdoc />
public Type BinderType { get; set; }
/// <inheritdoc />
public virtual BindingSource BindingSource
{
get
{
if (_bindingSource == null && BinderType != null)
{
return BindingSource.Custom;
}
return _bindingSource;
}
protected set
{
_bindingSource = value;
}
}
/// <inheritdoc />
public string Name { get; set; }
}
}