Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
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;
}
}
}