50 lines
1.4 KiB
C#
50 lines
1.4 KiB
C#
using System;
|
|
using System.Reflection;
|
|
|
|
namespace Zhaizj.Framework.ORM {
|
|
|
|
/// <summary>
|
|
/// 检查对象属性是否已经赋值,如果是字符串类型,empty的字符串也是不合法的
|
|
/// </summary>
|
|
[Serializable]
|
|
public class NotNullAttribute : ValidationAttribute {
|
|
|
|
public NotNullAttribute() {
|
|
}
|
|
|
|
public NotNullAttribute( String msg ) {
|
|
base.Message = msg;
|
|
}
|
|
|
|
public override void Validate( String action, IEntity target, EntityPropertyInfo info, Result result ) {
|
|
Object obj = target.get( info.Name );
|
|
|
|
Boolean isNull = false;
|
|
if (info.Type == typeof( String )) {
|
|
if (obj == null) {
|
|
isNull = true;
|
|
}
|
|
else if (strUtil.IsNullOrEmpty( obj.ToString() )) {
|
|
isNull = true;
|
|
}
|
|
}
|
|
else if (obj == null) {
|
|
isNull = true;
|
|
}
|
|
|
|
if (isNull) {
|
|
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" );
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|