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
/
ApiConventionMethodAttribute.cs
76 lines (67 loc) · 3.16 KB
/
ApiConventionMethodAttribute.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
76
// 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.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Core;
namespace Microsoft.AspNetCore.Mvc
{
/// <summary>
/// API conventions to be applied to a controller action.
/// <para>
/// API conventions are used to influence the output of ApiExplorer.
/// <see cref="ApiConventionMethodAttribute"/> can be used to specify an exact convention method that applies
/// to an action. <seealso cref="ApiConventionTypeAttribute"/> for details about applying conventions at
/// the assembly or controller level.
/// </para>
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class ApiConventionMethodAttribute : Attribute
{
/// <summary>
/// Initializes an <see cref="ApiConventionMethodAttribute"/> instance using <paramref name="conventionType"/> and
/// the specified <paramref name="methodName"/>.
/// </summary>
/// <param name="conventionType">
/// The <see cref="Type"/> of the convention.
/// <para>
/// Conventions must be static types. Methods in a convention are
/// matched to an action method using rules specified by <see cref="ApiConventionNameMatchAttribute" />
/// that may be applied to a method name or it's parameters and <see cref="ApiConventionTypeMatchAttribute"/>
/// that are applied to parameters.
/// </para>
/// </param>
/// <param name="methodName">The method name.</param>
public ApiConventionMethodAttribute(Type conventionType, string methodName)
{
ConventionType = conventionType ?? throw new ArgumentNullException(nameof(conventionType));
ApiConventionTypeAttribute.EnsureValid(conventionType);
if (string.IsNullOrEmpty(methodName))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(methodName));
}
Method = GetConventionMethod(conventionType, methodName);
}
private static MethodInfo GetConventionMethod(Type conventionType, string methodName)
{
var methods = conventionType.GetMethods(BindingFlags.Public | BindingFlags.Static)
.Where(method => method.Name == methodName)
.ToArray();
if (methods.Length == 0)
{
throw new ArgumentException(Resources.FormatApiConventionMethod_NoMethodFound(methodName, conventionType), nameof(methodName));
}
else if (methods.Length > 1)
{
throw new ArgumentException(Resources.FormatApiConventionMethod_AmbiguousMethodName(methodName, conventionType), nameof(methodName));
}
return methods[0];
}
/// <summary>
/// Gets the convention type.
/// </summary>
public Type ConventionType { get; }
internal MethodInfo Method { get; }
}
}