C#自定义Attribute的定义和获取简例

 

using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Configuration;
using  System.Reflection;

namespace  WebApplication4
{
    
public   partial   class  _Default : System.Web.UI.Page
    {
        
protected   void  Page_Load( object  sender, EventArgs e)
        {

            abc ta  =   new  abc();
            Type type 
=  ta.GetType();
            GetTableName(type);

            PropertyInfo[] propertys 
=  type.GetProperties(BindingFlags.Public  |  BindingFlags.Instance);
            FieldInfo[] fields 
=  type.GetFields(BindingFlags.Public  |  BindingFlags.Instance);

            
foreach  (PropertyInfo p  in  propertys)
            {
                //此处用了PropertyInfo.GetCustomAttributes方法,该方法继承至MemberInfo
                
object [] objs  =  p. GetCustomAttributes( false );
                foreach  ( object  obj  in  objs)
                {
                    
// string PK = GetPrimaryKey(obj);
                     string  ColumnName  =  GetColumnName(obj);
                }
            }
        }

        
public   static   string  GetTableName(Type classType)
        {
            
string  strTableName  =   string .Empty;
            
string  strEntityName  =   string .Empty;
            
string  strTableTel  =   string .Empty;

            strEntityName 
=  classType.FullName; // 类的全名
            // 此处用了System.Type.GetCustomAttributes 方法,该方法继承至MemberInfo
             object  classAttr  =  classType. GetCustomAttributes( false )[ 0 ];
             if  (classAttr  is  TableAttribute)
            {
                TableAttribute tableAttr 
=  classAttr  as  TableAttribute;
                strTableName 
=  tableAttr.Name;
                strTableTel 
=  tableAttr.Tel;
            }
            
if  ( string .IsNullOrEmpty(strTableName))
            {
                
throw   new  Exception( " 实体类: "   +  strEntityName  +   " 的属性配置[Table(name=\"tablename\")]错误或未配置 " );
            }
            
if  ( string .IsNullOrEmpty(strTableTel))
            {
                
throw   new  Exception( " 实体类: "   +  strEntityName  +   " 的属性配置[Table(tel=\"telname\")]错误或未配置 " );
            }


            
return  strTableName;
        }

        
public   static   string  GetPrimaryKey( object  attribute)
        {
            
string  strPrimary  =   string .Empty;
            IdAttribute attr 
=  attribute  as  IdAttribute;
            
switch  (attr.Strategy)
            {
                
case  GenerationType.INDENTITY:
                    
break ;
                
case  GenerationType.SEQUENCE:
                    strPrimary 
=  System.Guid.NewGuid().ToString();
                    
break ;
                
case  GenerationType.TABLE:
                    
break ;
            }

            
return  strPrimary;
        }

        
public   static   string  GetColumnName( object  attribute)
        {
            
string  columnName  =   string .Empty;
            
if  (attribute  is  ColumnAttribute)
            {
                ColumnAttribute columnAttr 
=  attribute  as  ColumnAttribute;
                columnName 
=  columnAttr.Name;
            }
            
else   if  (attribute  is  IdAttribute)
            {
                IdAttribute idAttr 
=  attribute  as  IdAttribute;
                columnName 
=  idAttr.Name;
            }

            
return  columnName;
        }
    }

    [Table(Name 
=   " Student " ,Tel = " ew " )]
    
public   class  abc
    {
        
private   string  _a  =   string .Empty;
        [Id(Name 
=   " studentid " , Strategy  =  GenerationType.INDENTITY)]
        
public   string  a
        {
            
get  {  return  _a; }
            
set  { _a  =  value; }
        }
        
private   string  _b  =   string .Empty;
        [Column(Name 
=   " studentno " )]
        
public   string  b
        {
            
get  {  return  _b; }
            
set  { _b  =  value; }
        }

        
public   string  c  =   string .Empty;
    }

    [AttributeUsage(AttributeTargets.Class, AllowMultiple 
=   false , Inherited  =   false )]
    
public   class  TableAttribute : Attribute
    {
        
public  TableAttribute() { }

        
private   string  _Name  =   string .Empty;
        
public   string  Name
        {
            
get  {  return  _Name; }
            
set  { _Name  =  value; }
        }

        
private   string  _Tel  =   string .Empty;
        
public   string  Tel
        {
            
get  {  return  _Tel; }
            
set  { _Tel  =  value; }
        }
    }

    [AttributeUsage(AttributeTargets.Field 
|  AttributeTargets.Property, AllowMultiple  =   false , Inherited  =   false )]
    
public   class  IdAttribute : Attribute
    {
        
private   string  _Name  =   string .Empty;
        
private   int  _Strategy  =  GenerationType.INDENTITY;

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

        
public   int  Strategy
        {
            
get  {  return  _Strategy; }
            
set  { _Strategy  =  value; }
        }
    }

    
public   class  GenerationType
    {
        
public   const   int  INDENTITY  =   1 ; // 自动增长
         public   const   int  SEQUENCE  =   2 ; // 序列
         public   const   int  TABLE  =   3 ; // TABLE

        
private  GenerationType() { } // 私有构造函数,不可被实例化对象
    }

    [AttributeUsage(AttributeTargets.Field 
|  AttributeTargets.Property,
        AllowMultiple 
=   false , Inherited  =   false )]
    
public   class  ColumnAttribute : Attribute
    {
        
public  ColumnAttribute() { }

        
private   string  _Name  =   string .Empty; // 列名        
         private   bool  _IsUnique  =   false ; // 是否唯一        
         private   bool  _IsNull  =   true ; // 是否允许为空
         private   bool  _IsInsert  =   true ; // 是否插入到表中
         private   bool  _IsUpdate  =   true ; // 是否修改到表中

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

        
public   bool  IsUnique
        {
            
get  {  return  _IsUnique; }
            
set  { _IsUnique  =  value; }
        }

        
public   bool  IsNull
        {
            
get  {  return  _IsNull; }
            
set  { _IsNull  =  value; }
        }

        
public   bool  IsInsert
        {
            
get  {  return  _IsInsert; }
            
set  { _IsInsert  =  value; }
        }

        
public   bool  IsUpdate
        {
            
get  {  return  _IsUpdate; }
            
set  { _IsUpdate  =  value; }
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值