/// <summary>
        /// Validates primitive types
        /// </summary>
        /// <param name="property"></param>
        /// <param name="value"></param>
        /// <returns>A list of all errors encountered.</returns>
        private static IEnumerable <ValidationError> ValidatePropertyRestrictions(object instance, PropertyInfo property, object value)
        {
            var errors = new List <ValidationError>();

            // Get all attributes on property
            var attributes = AttributeUtil.GetAll(property);

            foreach (var attribute in attributes)
            {
                switch (attribute)
                {
                case LengthAttribute attr:
                    if (value != null && value.ToString().Length != attr.Length)
                    {
                        errors.Add(new LengthError(instance, property.Name, value.ToString().Length, attr.Length));
                    }
                    break;

                case MinLengthAttribute attr:
                    if (value != null && value.ToString().Length < attr.Length)
                    {
                        errors.Add(new MinLengthError(instance, property.Name, value.ToString().Length, attr.Length));
                    }
                    break;

                case MaxLengthAttribute attr:
                    if (value != null && value.ToString().Length > attr.Length)
                    {
                        errors.Add(new MaxLengthError(instance, property.Name, value.ToString().Length, attr.Length));
                    }
                    break;

                case MinInclusiveAttribute attr:
                    if (value != null && (int)value < attr.Minimum)
                    {
                        errors.Add(new MinInclusiveError(instance, property.Name, (int)value, attr.Minimum));
                    }
                    break;

                case MaxInclusiveAttribute attr:
                    if (value != null && (int)value > attr.Maximum)
                    {
                        errors.Add(new MaxInclusiveError(instance, property.Name, (int)value, attr.Maximum));
                    }
                    break;

                case RegularExpressionAttribute attr:
                    if (value != null && !Regex.IsMatch(value.ToString(), attr.Pattern))
                    {
                        errors.Add(new PatternError(instance, property.Name, value.ToString(), attr.Pattern));
                    }

                    break;
                }
            }

            return(errors);
        }
예제 #2
0
        public void TestAttributeUtilGetAll()
        {
            TestClass2 item = new TestClass2(7, "test", 0);

            object[] objvalues = AttributeUtil.GetAll(() => item.Id, typeof(StringValueAttribute), "Value");
            Assert.IsNotNull(objvalues);
            List <string> values = new List <string>();

            foreach (object obj in objvalues)
            {
                values.Add((string)obj);
            }
            Assert.IsTrue(values.IndexOf("IdTest1") >= 0);
            Assert.IsTrue(values.IndexOf("IdTest2") >= 0);
            Assert.IsTrue(values.IndexOf("IdTest3") >= 0);
        }
예제 #3
0
        /// <summary>
        /// Returns all the child attributes and elements in serialized form for an object
        /// </summary>
        /// <param name="type"></param>
        /// <param name="instance"></param>
        /// <returns></returns>
        private List <XObject> GetElementContentsForInstance(Type type, object instance)
        {
            var contents = new List <XObject>();

            // Get all properties that are marked with XmlElementAttribute or XmlAttributeAttribute
            var properties = type.GetProperties()
                             .Where(prop => AttributeUtil.GetAll(prop).Any(attr => attr is XmlElementAttribute || attr is XmlAttributeAttribute));

            foreach (var property in properties)
            {
                if (IsPropertySpecified(property, type, instance))
                {
                    var elementAttr   = AttributeUtil.Get <XmlElementAttribute>(property);
                    var attributeAttr = AttributeUtil.Get <XmlAttributeAttribute>(property);

                    // Handle property if it's an XML Element
                    if (elementAttr != null)
                    {
                        var propertyValue = property.GetValue(instance);

                        // If property value is a list, create an element for each entry. Else just create an element for the value.
                        var isList = property.PropertyType.GetInterfaces()
                                     .Any(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IList <>));

                        if (isList)
                        {
                            foreach (var entry in (IEnumerable)propertyValue)
                            {
                                contents.Add(SerializeElement(property, entry, elementAttr));
                            }
                        }
                        else
                        {
                            contents.Add(SerializeElement(property, propertyValue, elementAttr));
                        }
                    }
                    // Handle property if it's an XML Attribute
                    else if (attributeAttr != null)
                    {
                        contents.Add(SerializeAttribute(property, property.GetValue(instance), attributeAttr));
                    }
                }
            }

            return(contents);
        }