检查泛型类型的参数范围

时间:2020-03-06 11:04:29

标签: c# generics compare range

是否有一种方法可以执行类似于“ string.Compare()”的操作,但适用于泛型类型。我想检查一些属性值的范围。
这是我正在做的工作,但是非常难看:

public class SomeClass<T>
{

    public T MinValue { get; set; }
    public T MaxValue { get; set; }

    private T _value;
    public T Value
    {
        get { return _value; }
        set
        {
            _value = value;

            // Restrict range to Min/Max
            if (Comparer<T>.Default.Compare(MaxValue, value) < 0)
                _value = MaxValue;
            if (Comparer<T>.Default.Compare(MinValue, value) > 0)
                _value = MinValue;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

此代码演示了我在评论中所说的内容。当然,您必须对其进行修改以适合您的精确范例,以便在比较器中使用它,但这应该足够清楚...

public class Program
    {
        public static void Main(string[] args)
        {
            System.Console.WriteLine("Hello World!");

            TestObject testObject = new TestObject(15);

            TestObject testObject2 = new TestObject(9);

            TestObject testObject3 = new TestObject(31);

            System.Console.ReadLine();
        }
    }

public class TestObject
    {
        [ValidateIntMin(Min = 10)]
        [ValidateIntMax(30)]
        public int SomeInt { get; set; }

        public TestObject(int value)
        {
            SomeInt = value;

            if (!Validator.Validate(this))
            {
                System.Console.WriteLine("Invalid Value assigned: " + value);
            }
            else
            {
                System.Console.WriteLine("" + SomeInt + " was a valid value");
            }

        }
    }

public class ValidateIntMax : Attribute
    {
        public int Max { get; set; }

        public ValidateIntMax(int MaxValue)
        {
            Max = MaxValue;
        }
    }

public class ValidateIntMin: Attribute
    {
        public int Min { get; set; }
    }

public static class Validator
    {
        public static bool Validate<T>(T input) {
            var attrType = typeof(T);
            var properties = attrType.GetProperties();
            bool isValid = true;
            foreach (PropertyInfo propertyInfo in properties)
            {
                var customerMaxValueInt = propertyInfo.GetCustomAttributes(typeof(ValidateIntMax), false).FirstOrDefault();
                var customerMinValueInt = propertyInfo.GetCustomAttributes(typeof(ValidateIntMin), false).FirstOrDefault();

                if (customerMaxValueInt != null)
                {
                    if (propertyInfo.PropertyType == typeof(int))
                    {
                        var currentPropertyInfoBeingTested = (int)propertyInfo.GetValue(input);
                        var currentMaxValueToVerifyAgainst = ((ValidateIntMax)customerMaxValueInt).Max;

                        if (currentPropertyInfoBeingTested > currentMaxValueToVerifyAgainst)
                        {
                            isValid = false;
                        }
                    }
                }

                if (customerMinValueInt != null)
                {
                    if (propertyInfo.PropertyType == typeof(int))
                    {
                        var currentPropertyInfoBeingTested = (int)propertyInfo.GetValue(input);
                        var currentMaxValueToVerifyAgainst = ((ValidateIntMin)customerMinValueInt).Min;

                        if (currentPropertyInfoBeingTested < currentMaxValueToVerifyAgainst)
                        {
                            isValid = false;
                        }
                    }
                }

            }
            return isValid;
        }
    }

应提供输出:

Hello World!

15 was a valid value

Invalid Value assigned: 9

Invalid Value assigned: 31

当然,您可以为不同类型等添加验证。 这只是为了展示一种完全自定义的属性设置方式。

但是,我建议您阅读ValidationAttribute,以查看是否无法使用实现的功能。

但这只是PoC片。

相关问题