专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »DotNet » attribute:C#的特性(Attribute)的应用 »正文

attribute:C#的特性(Attribute)的应用

来源: 发布时间:星期五, 2009年1月9日 浏览:25次 评论:0
  文章内容参考了nuaaflm反射学习系列2-特性(Attribute)链接地址为:http://www.cnblogs.com/nuaalfm/archive/2008/09/07/1286195.html

  打算写个简单ORM实现所以上网上参考了些资料现在先介绍下需要用到个重要技术特性(Attribute)原文作者已经介绍得很详细了我就不自己写了直接COPY过来由于原作者使用是.net3.5我把代码改成了2.0版本

  先看个简单例子

[Table(Name="UserInfo")]
public UserInfo
{


  当C#编译器发现这个属性有个特性Table时首先会把串Attribute添加到这个名称后面形成个组合名称TableAttribute然后在其搜索路径所有命名空间中搜索有相同类名但要注意如果该特性名结尾是Attribute编译器就不会把该串加到组合名称中所有特性都是从.Attribute类型上面派生

  接着我们来看下Table特性定制格式

[AttributeUsageAttribute(AttributeTargets.Class, Inherited=true,AllowMultiple=false)]
public TalbeAttribute:Attribute
{


  在定义类型时使用.AttributeUsage特性来表明这个自定义特性使用范围这里使用了Class样式表示TableAttribute特性只能用在其它Class类型前面若放置在Interface或Struct类型前面或者放在对象成员前面则会出现编译这里还是用语句 AllowMultiple=false 语句来表明对于个类型该特性只能用个Class类型前面出现多个TableAttribute则会出现编译若设置AllowMultiple=true则该特性可以多次定义也就是个Class类型前面可以出现多个相同类型特性不过这里我们假设个对象只能映射到个数据表上没有多重映射因此就指明对同个类型该特性不能多次使用Inherited参数设定为true就表示应用到类或接口上特性也可以自动应用到所派生类或接口上

  我们再看下定制TalbeAttribute特性完整例子:

Code
[AttributeUsage(AttributeTargets.Class,AllowMultiple=false,Inherited=false)]
  public TableAttribute : Attribute
  {
    private tableName;
    public TableName
    {
      get { tableName; }
       { tableName = value; }
    }
    public TableAttribute
    {
      this.tableName = null;
    }
    public TableAttribute( tableName)
    {
      this.tableName = tableName;
    }
  }


  特性也是个Class类型可以有多个构造就像C#语句我们向类型附加特性时可以使用区别化参数来指明使用特性那个构造我们附加特性时还可以使用“属性名=属性值”思路方法来直接指明特性属性值该特性中定义了个TableName属性该属性就是被修饰对象所映射数据库表名称

  下面我们举个使用特性来进行O/RMapping例子

  用户类:

Code
  [Table("UserInfo")]
  public UserInfo
  {
    private userId;
    [Column("UserId",DbType.Int32)]
    public UserId
    {
      get { userId; }
       { userId = value; }
    }
    private userName;
    [Column(UserName,DbType.String)]
    public UserName
    {
      get { userName; }
       { userName = value; }
    }  
  }


  列特性:

Code
[AttributeUsage(AttributeTargets.Property,AllowMultiple=false,Inherited=false)]
  public ColumnAttribute : Attribute
  {
    public ColumnAttribute
    {
      this.columnName = null;
      this.dbType = DbType.String;
    }
    public ColumnAttribute( columnName)
    {
      this.columnName = columnName;
    }
    public ColumnAttribute( columnName, DbType dbType)
      : this(columnName)
    {
      this.dbType = dbType;
    }
    private columnName;
    public ColumnName
    {
      get { columnName; }
       { columnName = value; }
    }
    private DbType dbType;
    public DbType DbType
    {
      get { dbType; }
       { dbType = value; }
    }
  
  }


  运行类:

Code
Program
  {
     void Main( args)
    {
      
      UserInfo userInfo = UserInfo;
      Type type = userInfo.GetType;
      
      TableAttribute ta = (TableAttribute)type.GetCustomAttributes(false)[0];
      Console.WriteLine("数据表名:" + ta.TableName);
      PropertyInfo infos = type.GetProperties;
      foreach (PropertyInfo info in infos)
      {
        object attributes = info.GetCustomAttributes(false);
        foreach (object att in attributes)
        {
           (att is ColumnAttribute)
          {
            ColumnAttribute ca = att as ColumnAttribute;
             cn = ca.ColumnName null ? info.Name : ca.ColumnName;
            Console.WriteLine("字段名:" + cn);
            Console.WriteLine("字段类型:" + ca.DbType);
          }
        }
      }
    }




  下面是运行结果截图:





0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: