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
/
ModelMetadataProviderExtensions.cs
54 lines (47 loc) · 1.99 KB
/
ModelMetadataProviderExtensions.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
// 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.Core;
namespace Microsoft.AspNetCore.Mvc.ModelBinding
{
/// <summary>
/// Extensions methods for <see cref="IModelMetadataProvider"/>.
/// </summary>
public static class ModelMetadataProviderExtensions
{
/// <summary>
/// Gets a <see cref="ModelMetadata"/> for property identified by the provided
/// <paramref name="containerType"/> and <paramref name="propertyName"/>.
/// </summary>
/// <param name="provider">The <see cref="ModelMetadata"/>.</param>
/// <param name="containerType">The <see cref="Type"/> for which the property is defined.</param>
/// <param name="propertyName">The property name.</param>
/// <returns>A <see cref="ModelMetadata"/> for the property.</returns>
public static ModelMetadata GetMetadataForProperty(
this IModelMetadataProvider provider,
Type containerType,
string propertyName)
{
if (provider == null)
{
throw new ArgumentNullException(nameof(provider));
}
if (containerType == null)
{
throw new ArgumentNullException(nameof(containerType));
}
if (propertyName == null)
{
throw new ArgumentNullException(nameof(propertyName));
}
var containerMetadata = provider.GetMetadataForType(containerType);
var propertyMetadata = containerMetadata.Properties[propertyName];
if (propertyMetadata == null)
{
var message = Resources.FormatCommon_PropertyNotFound(containerType, propertyName);
throw new ArgumentException(message, nameof(propertyName));
}
return propertyMetadata;
}
}
}