Exemple #1
0
        private void GetAttribute(ValdrAttribute attr, string name, object arg)
        {
            switch (name)
            {
            case nameof(DisplayAttribute.Name):
                attr.Name = (string)arg;
                break;

            case nameof(RequiredAttribute.ErrorMessage):
                attr.Message = (string)arg;
                break;

            case nameof(DisplayAttribute.ResourceType):
            case nameof(RequiredAttribute.ErrorMessageResourceType):
                attr.ResourceType = (Type)arg;
                break;

            case nameof(RequiredAttribute.ErrorMessageResourceName):
                attr.ResourceName = (string)arg;
                break;

            case nameof(RangeAttribute.Minimum):
            case nameof(StringLengthAttribute.MinimumLength):
                attr.Minimum = (int)arg;
                break;

            case nameof(RangeAttribute.Maximum):
            case nameof(StringLengthAttribute.MaximumLength):
                attr.Maximum = (int)arg;
                break;

            case nameof(RegularExpressionAttribute.Pattern):
                attr.Pattern = (string)arg;
                break;
            }
        }
Exemple #2
0
        private ValdrAttribute GetPropertyAttribute(PropertyInfo prop, string attributeName)
        {
            foreach (var data in prop.GetCustomAttributesData())
            {
                if (data.AttributeType.Name == attributeName)
                {
                    var attr = new ValdrAttribute();

                    if (attributeName == nameof(StringLengthAttribute) && data.ConstructorArguments.Count > 0)
                    {
                        attr.Maximum = (int)data.ConstructorArguments[0].Value;
                    }
                    else if (attributeName == nameof(RangeAttribute) && data.ConstructorArguments.Count > 1)
                    {
                        attr.Minimum = (int)data.ConstructorArguments[0].Value;
                        attr.Maximum = (int)data.ConstructorArguments[1].Value;
                    }
                    else if (attributeName == nameof(RegularExpressionAttribute) && data.ConstructorArguments.Count > 0)
                    {
                        attr.Pattern = (string)data.ConstructorArguments[0].Value;
                    }

                    if (data.NamedArguments != null)
                    {
                        foreach (var item in data.NamedArguments)
                        {
                            GetAttribute(attr, item.MemberName, item.TypedValue.Value);
                        }
                    }

                    return(attr);
                }
            }

            return(null);
        }
Exemple #3
0
 private void GetAttribute(ValdrAttribute attr, string name, object arg)
 {
     switch (name)
     {
         case nameof(DisplayAttribute.Name):
             attr.Name = (string)arg;
             break;
         case nameof(RequiredAttribute.ErrorMessage):
             attr.Message = (string)arg;
             break;
         case nameof(DisplayAttribute.ResourceType):
         case nameof(RequiredAttribute.ErrorMessageResourceType):
             attr.ResourceType = (Type)arg;
             break;
         case nameof(RequiredAttribute.ErrorMessageResourceName):
             attr.ResourceName = (string)arg;
             break;
         case nameof(RangeAttribute.Minimum):
         case nameof(StringLengthAttribute.MinimumLength):
             attr.Minimum = (int)arg;
             break;
         case nameof(RangeAttribute.Maximum):
         case nameof(StringLengthAttribute.MaximumLength):
             attr.Maximum = (int)arg;
             break;
         case nameof(RegularExpressionAttribute.Pattern):
             attr.Pattern = (string)arg;
             break;
     }
 }
Exemple #4
0
        private ValdrAttribute GetPropertyAttribute(PropertyInfo prop, string attributeName)
        {
            foreach (var data in prop.GetCustomAttributesData())
            {
                if (data.AttributeType.Name == attributeName)
                {
                    var attr = new ValdrAttribute();

                    if (attributeName == nameof(StringLengthAttribute) && data.ConstructorArguments.Count > 0)
                    {
                        attr.Maximum = (int)data.ConstructorArguments[0].Value;
                    }
                    else if (attributeName == nameof(RangeAttribute) && data.ConstructorArguments.Count > 1)
                    {
                        attr.Minimum = (int)data.ConstructorArguments[0].Value;
                        attr.Maximum = (int)data.ConstructorArguments[1].Value;
                    }
                    else if (attributeName == nameof(RegularExpressionAttribute) && data.ConstructorArguments.Count > 0)
                    {
                        attr.Pattern = (string)data.ConstructorArguments[0].Value;
                    }

                    if (data.NamedArguments != null)
                    {
                        foreach (var item in data.NamedArguments)
                        {
                            GetAttribute(attr, item.MemberName, item.TypedValue.Value);
                        }
                    }

                    return attr;
                }
            }

            return null;
        }