博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#自定义Attribute的定义和获取简例
阅读量:4641 次
发布时间:2019-06-09

本文共 4482 字,大约阅读时间需要 14 分钟。

 

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; }
        }
    }
}

 

转载于:https://www.cnblogs.com/zhuawang/archive/2011/08/14/2137791.html

你可能感兴趣的文章
163镜像地址
查看>>
ehcache memcache redis 三大缓存男高音
查看>>
eclipse 快捷键Open Implementation 直接退出
查看>>
minix中管道文件和设备文件的读写
查看>>
JAXB - Annotations, Annotations for Enums: XmlEnum, XmlEnumValue
查看>>
context 插图
查看>>
文件管理器中不支持的wma歌曲也显示可以播放的音乐图标
查看>>
Java基础学习-流程控制语句
查看>>
Shell中read的常用方式
查看>>
01javascript数据类型
查看>>
asp.net实现md5加密方法详解
查看>>
AJAX
查看>>
table 的thead th 固定 tbody滚动例子
查看>>
并行计算思考----回溯法求解数独问题
查看>>
设计模式:模板模式
查看>>
和菜鸟一起学OK6410之ADC模块
查看>>
代理 模式
查看>>
[git] 细说commit (git add/commit/diff/rm/reset 以及 index 的概念)
查看>>
DOM Core和HTML DOM的区别
查看>>
SurfaceView+MediaPlay的bug们
查看>>