Permalink
Cannot retrieve contributors at this time
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?
NHail.ComponentModel.DataAnnotations.Fluent/AttributeProvider.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
50 lines (44 sloc)
1.61 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace NHail.ComponentModel.DataAnnotations.Fluent | |
{ | |
public class AttributeProvider<TSource> : TypeDescriptionProvider | |
{ | |
private readonly IList<Attribute> _validations = new List<Attribute>(); | |
private readonly IList<KeyValuePair<string, Attribute>> _propValidations = | |
new List<KeyValuePair<string, Attribute>>(); | |
public AttributeProvider() : base(TypeDescriptor.GetProvider(typeof(TSource))) | |
{ | |
TypeDescriptor.AddProvider(this, typeof(TSource)); | |
} | |
public void AddAttribute(Attribute validation) | |
{ | |
if (validation == null) | |
{ | |
throw new ArgumentNullException(nameof(validation)); | |
} | |
_validations.Add(validation); | |
} | |
public void AddPropertyAttribute(string property, Attribute validation) | |
{ | |
if (property == null) | |
{ | |
throw new ArgumentNullException(nameof(property)); | |
} | |
if (validation == null) | |
{ | |
throw new ArgumentNullException(nameof(validation)); | |
} | |
_propValidations.Add(new KeyValuePair<string, Attribute>(property, validation)); | |
} | |
public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance) | |
{ | |
return new AttributeTypeDescriptor(_validations, _propValidations, | |
base.GetTypeDescriptor(objectType, instance)); | |
} | |
} | |
} |