-
Notifications
You must be signed in to change notification settings - Fork 10
/
DefaultDisplayNameResolver.cs
37 lines (34 loc) · 1.35 KB
/
DefaultDisplayNameResolver.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
using System;
using System.Linq.Expressions;
using System.Reflection;
using ReactiveValidation.Attributes;
using ReactiveValidation.Helpers;
using ReactiveValidation.Resources.StringSources;
namespace ReactiveValidation.Internal
{
/// <summary>
/// Display name resolver, which uses <see cref="DisplayNameAttribute" />.
/// </summary>
internal class DefaultDisplayNameResolver : IDisplayNameResolver
{
/// <inheritdoc />
public IStringSource? GetPropertyNameSource(PropertyInfo propertyInfo)
{
var displayNameAttribute = propertyInfo.GetCustomAttribute<DisplayNameAttribute>();
return displayNameAttribute == null
? null
: new DisplayNamePropertySource(displayNameAttribute);
}
/// <inheritdoc />
public IStringSource? GetPropertyNameSource(Type objectType, LambdaExpression expression)
{
var propertyName = ReactiveValidationHelper.GetPropertyName(objectType, expression);
if (string.IsNullOrEmpty(propertyName))
return null;
var displayNameAttribute = objectType.GetProperty(propertyName)?.GetCustomAttribute<DisplayNameAttribute>();
return displayNameAttribute == null
? null
: new DisplayNamePropertySource(displayNameAttribute);
}
}
}