예제 #1
0
        public void Validate(Type type, object dto)
        {
            IDictionary <string, IEnumerable <string> > valid = new Dictionary <string, IEnumerable <string> >();
            PropValidAttribute  temp;
            PropValidateContext _context = new PropValidateContext(_provider, type, dto);

            foreach (var item in type.GetProperties())
            {
                temp = (PropValidAttribute)item.GetCustomAttributes(false).FirstOrDefault(x => x.GetType() == typeof(PropValidAttribute));
                if (temp != null)
                {
                    if (_context.Valid == null)
                    {
                        _context.Valid = new List <string>();
                    }
                    _context.PropName = char.ToLower(item.Name[0]) + item.Name.Substring(1);

                    foreach (var s in temp.FuncIdsAtributes)
                    {
                        if (!_validateFunctions.ContainsKey(s))
                        {
                            throw new ClientException("v-func-no");
                        }
                        else
                        {
                            try
                            {
                                if (item.GetValue(dto) is IEnumerable <object> )
                                {
                                    var m = (IEnumerable <object>)item.GetValue(dto);
                                    foreach (var r in m)
                                    {
                                        ((Delegate)_validateFunctions[s]).DynamicInvoke(r, _context);
                                    }
                                }
                                else
                                {
                                    ((Delegate)_validateFunctions[s]).DynamicInvoke(item.GetValue(dto), _context);
                                }
                            }
                            catch (TargetInvocationException e)
                            {
                                throw e.InnerException;
                            }
                        }
                    }

                    if (_context.Valid.Count > 0)
                    {
                        valid.Add(item.Name, _context.Valid);
                        _context.Valid = null;
                    }
                }
            }

            if (valid.Count > 0)
            {
                throw new ClientException("v-dto-invalid", valid);
            }
        }
예제 #2
0
        public void Validate(string[] attrs, object obj, string objName)
        {
            IDictionary <string, IEnumerable <string> > valid = new Dictionary <string, IEnumerable <string> >();
            PropValidateContext _context = new PropValidateContext(_provider, null, null);

            _context.Valid    = new List <string>();
            _context.PropName = char.ToLower(objName[0]) + objName.Substring(1);
            foreach (var s in attrs)
            {
                if (!_validateFunctions.ContainsKey(s))
                {
                    throw new ClientException("v-func-no");
                }
                else
                {
                    try
                    {
                        if (obj is IEnumerable <object> )
                        {
                            var m = (IEnumerable <object>)obj;
                            foreach (var item in m)
                            {
                                ((Delegate)_validateFunctions[s]).DynamicInvoke(item, _context);
                            }
                        }
                        else
                        {
                            ((Delegate)_validateFunctions[s]).DynamicInvoke(obj, _context);
                        }
                    }
                    catch (TargetInvocationException e)
                    {
                        throw e.InnerException;
                    }
                }
            }

            if (_context.Valid.Count > 0)
            {
                valid.Add(objName, _context.Valid);
                _context.Valid = null;
            }

            if (valid.Count > 0)
            {
                throw new ClientException("v-dto-invalid", valid);
            }
        }