Knocks/BackEnd/Knoks.Framework/Extentions/NotZeroAttribute.cs

48 lines
1.6 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace Knoks.Framework.Extentions
{
public class NotZeroAttribute : RangeAttribute
{
public NotZeroAttribute() : base(typeof(Decimal), 1e-8m.ToString(), Decimal.MaxValue.ToString())
{
ErrorMessage = "Please provide value for {0}";
}
public NotZeroAttribute(Type operandType) : base(operandType, GetMinimum(operandType), GetMaximum(operandType))
{
ErrorMessage = "Please provide value for {0}";
}
private static string GetMinimum(Type operandType)
{
if (operandType == typeof(int) || operandType==typeof(long))
return "1";
if (operandType == typeof(float))
return float.Epsilon.ToString();
if (operandType == typeof(double))
return double.Epsilon.ToString();
if (operandType == typeof(decimal))
return 1e-8.ToString();
return "1";
}
private static string GetMaximum(Type operandType)
{
if (operandType == typeof(int))
return Int32.MaxValue.ToString();
if (operandType == typeof(long))
return long.MaxValue.ToString();
if (operandType == typeof(float))
return float.MaxValue.ToString();
if (operandType == typeof(double))
return double.MaxValue.ToString();
if (operandType == typeof(decimal))
return 1e-8.ToString();
return Int32.MaxValue.ToString();
}
}
}