用自定义属性实现必需字段的检查

在持久化类时,一个对象需要保存到数据库中表,数据库中的表在设计时会根据一些设计范式的要求定义一些必要的字段
这样,在持久化时,就必须检查一下这些与数据库映射字段是否已经赋值了,如果没有,则不能保存到数据库中,这时应弹出
对话框来提示,以防止错误异常的发生.
using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Reflection;
using  System.Diagnostics;
using  System.Windows.Forms;

namespace  desz
{
    
///   <summary>
    
///  这是我的第一个属性,测试文档,这个属性只能用于Field
    
///   </summary>
    [AttributeUsage(AttributeTargets.Field, Inherited  =   true )]
    
public   class  cxyAttrib : System.Attribute
    {
        
string  name;
        
string  createdate;
        
bool  IsRequired;

        
public  cxyAttrib()
        {
        }
        
public  cxyAttrib( string  t_date, string  t_name, bool  t_req)
        {
            createdate 
=  t_date;
            name 
=  t_name;
            IsRequired 
=  t_req;
        }



        
///   <summary>
        
///  当前属性的名称
        
///   </summary>
         public   string  Name
        {
            
get  {  return  name; }
            
set  { name  =  value; }
        }
        
///   <summary>
        
///  创建日期
        
///   </summary>
         public   string  CreateDate
        {
            
get  {  return  createdate; }
            
set  { createdate  =  value; }
        }
        
///   <summary>
        
///  是否必须
        
///   </summary>
         public   bool  Required
        {
            
get  {  return  IsRequired; }
            
set  { IsRequired  =  value; }
        }

    }

    
public   class  MyPeriseObject
    {
    
// 在这里设置那一些字段是必须的,作为以后检查的根据
        [cxyAttrib(CreateDate  =   " 2007-10-9 " , Name  =   " object id " , Required  =   true )]
        
private   string  id;
        [cxyAttrib(CreateDate 
=   " 2007-10-8 " , Name  =   " object name " , Required  =   true )]
        
private   string  name;
        [cxyAttrib(CreateDate 
=   " 2007-10-7 " , Name  =   " object color " , Required  =   false )]
        
private   string  color;

    
// 以下的就是属性了
      
        
public   string  ID
        {
            
get  {  return  id; }
            
set  { id  =  value; }
        }

       
        
public   string  Name
        {
            
get  {  return  name; }
            
set  { name  =  value; }
        }

      
        
public   string  Color
        {
            
get  {  return  color; }
            
set  { color  =  value; }
        } 

    }

    
///   <summary>
    
///  用来检查当前物体在持久化之前所有的属性是否都已经全部赋值了
    
///   </summary>
     public   class  CheckAttribute
    {
        
public   static   void  Run()
        {
            MyPeriseObject componet1 
=   new  MyPeriseObject();
            componet1.ID 
=   " 100 " ;
            Type currentType 
=  componet1.GetType();
            FieldInfo[] fi 
=  currentType.GetFields(BindingFlags.NonPublic  |  BindingFlags.Instance); // 找出当前对type中有那些fields



            
foreach  (FieldInfo tf  in  fi)
            {
        
// 找出赋在这些field上的自定义属性
                cxyAttrib tpcxy  =  (cxyAttrib)Attribute.GetCustomAttribute(tf,  typeof (cxyAttrib));
                
if  (tpcxy  !=   null )
                    
if  (tpcxy.Required)  // 检查设置时值是否是必须,并检查当前值是否是必须
                         if  (tf.GetValue(componet1)  ==   null )
                        {
                            Debug.WriteLine(
" 必要属性没有被设置值 " );
                            MessageBox.Show(tf.Name 
+   " 是一个必要属性 但是没有被设置值 " );
                        }
                        
else
                        {
                            MessageBox.Show(tf.Name 
+   " 的属性值是: "   +  tf.GetValue(componet1).ToString());
                        } 
            }

        }
    }
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值