root/branches/Abonament/Wierszowki/Wierszowki.Services/DataAnnotationsValidationRunner.cs @ 854

Wersja 854, 1.8 KB (wprowadzona przez marek, 17 years temu)

re #184 - merged with latest trunk

RevLine 
[752]1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.ComponentModel.DataAnnotations;
5using System.Linq;
6using System.Text;
7using xVal.ServerSide;
8
9namespace Wierszowki.Services
10{
11    internal static class DataAnnotationsValidationRunner
12    {
13        public static IEnumerable<ErrorInfo> GetErrors(object instance)
[854]14        {   
15            List<ErrorInfo> errors = new List<ErrorInfo>();
16
17                IEnumerable<PropertyDescriptor> properties = TypeDescriptor.GetProperties(instance).Cast<PropertyDescriptor>();
18                foreach (PropertyDescriptor prop in properties) {
19                    IEnumerable<ValidationAttribute> attributes = prop.Attributes.OfType<ValidationAttribute>();
20
21                int empty = 2;
22                object pom = prop.GetValue(instance);
23                if (pom != null)
24                {
25                    object typek = pom.GetType();
26                    if (pom.GetType().Name == "Int32")
27                    {
28                        empty = Convert.ToInt32(pom);
29                    }
30                }                               
31                foreach (ValidationAttribute attribute in attributes) {
32                        if (!attribute.IsValid(prop.GetValue(instance)) || empty == 0) {
33                            errors.Add(new ErrorInfo(prop.Name, attribute.FormatErrorMessage(string.Empty), instance));
34                        }
35                    }
36                }
37                return errors;
38            /* return from prop in TypeDescriptor.GetProperties(instance).Cast<PropertyDescriptor>()
[752]39                   from attribute in prop.Attributes.OfType<ValidationAttribute>()
40                   where !attribute.IsValid(prop.GetValue(instance))
41                   select new ErrorInfo(prop.Name, attribute.FormatErrorMessage(string.Empty), instance);
[854]42            */
[752]43        }
44    }
45}
Notatka: Zobacz TracBrowser aby uzyskać więcej informacji.