Hishop.Components.Validation/ValidationReflectionHelper.cs

(开头部分) 3KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

namespace Hishop.Components.Validation
{
    using Hishop.Components.Validation.Properties;
    using System;
    using System.Globalization;
    using System.Reflection;
    using Hishop.Components.Validation.Validators;

    internal static class ValidationReflectionHelper
    {
        public static T ExtractValidationAttribute<T>(ICustomAttributeProvider attributeProvider, string ruleset) where T: BaseValidationAttribute
        {
            if (attributeProvider != null)
            {
                foreach (T local in attributeProvider.GetCustomAttributes(typeof(T), false))
                {
                    if (ruleset.Equals(local.Ruleset))
                    {
                        return local;
                    }
                }
            }
            return default(T);
        }

        public static FieldInfo GetField(Type type, string fieldName, bool throwIfInvalid)
        {
            if (string.IsNullOrEmpty(fieldName))
            {
                throw new ArgumentNullException("fieldName");
            }
            FieldInfo field = type.GetField(fieldName, BindingFlags.Public | BindingFlags.Instance);
            if (IsValidField(field))
            {
                return field;
            }
            if (throwIfInvalid)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.ExceptionInvalidField, new object[] { fieldName, type.FullName }));
            }
            return null;
        }

        public static MethodInfo GetMethod(Type type, string methodName, bool throwIfInvalid)
        {
            if (string.IsNullOrEmpty(methodName))
            {
                throw new ArgumentNullException("methodName");
            }
            MethodInfo methodInfo = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, null);
            if (IsValidMethod(methodInfo))
            {
                return methodInfo;
            }
            if (throwIfInvalid)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.ExceptionInvalidMethod, new object[] { methodName, type.FullName }));
            }
            return null;
        }
后面还有 36 行代码,购买后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。