60 lines
1.5 KiB
C#
60 lines
1.5 KiB
C#
using System;
|
|
using System.Reflection;
|
|
|
|
namespace Zhaizj.Framework.ORM
|
|
{
|
|
|
|
/// <summary>
|
|
/// 检查对象属性是否已经赋值,如果是字符串类型,empty的字符串也是不合法的
|
|
/// </summary>
|
|
[Serializable]
|
|
public class NotZeroAttribute : ValidationAttribute
|
|
{
|
|
|
|
public NotZeroAttribute()
|
|
{
|
|
}
|
|
|
|
public NotZeroAttribute(String msg)
|
|
{
|
|
base.Message = msg;
|
|
}
|
|
|
|
public override void Validate(String action, IEntity target, EntityPropertyInfo info, Result result)
|
|
{
|
|
Object obj = target.get(info.Name);
|
|
Boolean isInt = false;
|
|
if (info.Type == typeof(int))
|
|
{
|
|
if (obj == null)
|
|
{
|
|
isInt = true;
|
|
}
|
|
else if (Convert.ToInt32(obj) == 0)
|
|
{
|
|
isInt = true;
|
|
}
|
|
}
|
|
else if (Convert.ToInt32(obj) == 0)
|
|
{
|
|
isInt = true;
|
|
}
|
|
if (isInt)
|
|
{
|
|
if (strUtil.HasText(this.Message))
|
|
{
|
|
result.Add(this.Message);
|
|
}
|
|
else
|
|
{
|
|
EntityInfo ei = Entity.GetInfo(target);
|
|
String str = "[" + ei.FullName + "] : property \"" + info.Name + "\" ";
|
|
result.Add(str + "can not be null");
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|