-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResourceTypeModelMetadataProvider.cs
39 lines (37 loc) · 1.51 KB
/
ResourceTypeModelMetadataProvider.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
using CSTruter.Mvc.Samples.Sample2.Attributes;
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
namespace CSTruter.Mvc.Samples.Sample2.Providers
{
public class ResourceTypeModelMetadataProvider : DataAnnotationsModelMetadataProvider
{
protected void EnsureResourceTypeSet(ICustomTypeDescriptor customTypeDescriptor, Type resourceType)
{
var properties = customTypeDescriptor.GetProperties();
foreach (var property in properties.OfType<PropertyDescriptor>())
{
foreach (var attribute in property.Attributes.OfType<Attribute>())
{
if (attribute is ValidationAttribute)
((ValidationAttribute)attribute).ErrorMessageResourceType = resourceType;
else if (attribute is DisplayAttribute)
((DisplayAttribute)attribute).ResourceType = resourceType;
}
}
}
protected override ICustomTypeDescriptor GetTypeDescriptor(Type type)
{
var customTypeDescriptor = base.GetTypeDescriptor(type);
var resourceTypeAttribute = type.GetCustomAttribute<ResourceTypeAttribute>();
if (resourceTypeAttribute != null)
{
EnsureResourceTypeSet(customTypeDescriptor, resourceTypeAttribute.ResourceType);
}
return customTypeDescriptor;
}
}
}