代码改变世界

.NET Attribute使用实例

2009-02-28 20:03  Iron  阅读(426)  评论(0编辑  收藏  举报

这里介绍一下.NET的Attribute,在早些时候被翻译作属性,和property混淆了。现在一般都翻译为特性或特征,它可以给一个类或者类的成员附加一些额外的特征或者功能。在.NET里面的System.Arribute作为其基类,所有继承自他的,并且类名以Attribute结尾的类型都可以作为Attribute实用,这里有一个约束,定义的时侯类名要以Attribute结尾,但是使用的时候要去掉Attibute。.NET类库预定义了很多的特性来实现很多的功能,这里我们通过Attribute类来标示类成员的特性,并且通过反射来获取来标示类成员与数据库的映射。

首先我们通过一个例子来看看Attribute的特性

class TestAttribute:System.Attribute

{

    public TestAttribute(string message)

    {

        throw new Exception("error:"+message);

    }

}

class Tester

{

    [Test("Can not run this method!")]

    public void Cannotrun()

    {

    }

    public static Main(sting[] args)

    {

        Tester t=new Tester();

        t.Cannotrun();

    }

}

我们现在就是来构造一个Attribute的类来存储一个属性的类型,长度,映射字段等数据

 

.NET Attribute使用实例 - hxf829 - hxf829的博客namespace Alexander.Xbase.Interface

.NET Attribute使用实例 - hxf829 - hxf829的博客.NET Attribute使用实例 - hxf829 - hxf829的博客.NET Attribute使用实例 - hxf829 - hxf829的博客{

.NET Attribute使用实例 - hxf829 - hxf829的博客    [AttributeUsage(AttributeTargets.All, AllowMultiple = true)]

.NET Attribute使用实例 - hxf829 - hxf829的博客    public class ParamAttribute:Attribute

.NET Attribute使用实例 - hxf829 - hxf829的博客.NET Attribute使用实例 - hxf829 - hxf829的博客    .NET Attribute使用实例 - hxf829 - hxf829的博客{

.NET Attribute使用实例 - hxf829 - hxf829的博客        private string _parameterType;

.NET Attribute使用实例 - hxf829 - hxf829的博客

.NET Attribute使用实例 - hxf829 - hxf829的博客        public string ParameterType

.NET Attribute使用实例 - hxf829 - hxf829的博客.NET Attribute使用实例 - hxf829 - hxf829的博客        .NET Attribute使用实例 - hxf829 - hxf829的博客{

.NET Attribute使用实例 - hxf829 - hxf829的博客.NET Attribute使用实例 - hxf829 - hxf829的博客            get .NET Attribute使用实例 - hxf829 - hxf829的博客{ return _parameterType; }

.NET Attribute使用实例 - hxf829 - hxf829的博客.NET Attribute使用实例 - hxf829 - hxf829的博客            set .NET Attribute使用实例 - hxf829 - hxf829的博客{ _parameterType = value; }

.NET Attribute使用实例 - hxf829 - hxf829的博客        }

.NET Attribute使用实例 - hxf829 - hxf829的博客        private int _parameterLength;

.NET Attribute使用实例 - hxf829 - hxf829的博客

.NET Attribute使用实例 - hxf829 - hxf829的博客        public int ParameterLength

.NET Attribute使用实例 - hxf829 - hxf829的博客.NET Attribute使用实例 - hxf829 - hxf829的博客        .NET Attribute使用实例 - hxf829 - hxf829的博客{

.NET Attribute使用实例 - hxf829 - hxf829的博客.NET Attribute使用实例 - hxf829 - hxf829的博客            get .NET Attribute使用实例 - hxf829 - hxf829的博客{ return _parameterLength; }

.NET Attribute使用实例 - hxf829 - hxf829的博客.NET Attribute使用实例 - hxf829 - hxf829的博客            set .NET Attribute使用实例 - hxf829 - hxf829的博客{ _parameterLength = value; }

.NET Attribute使用实例 - hxf829 - hxf829的博客        }

.NET Attribute使用实例 - hxf829 - hxf829的博客        private string _srccolumn;

.NET Attribute使用实例 - hxf829 - hxf829的博客

.NET Attribute使用实例 - hxf829 - hxf829的博客        public string Srccolumn

.NET Attribute使用实例 - hxf829 - hxf829的博客.NET Attribute使用实例 - hxf829 - hxf829的博客        .NET Attribute使用实例 - hxf829 - hxf829的博客{

.NET Attribute使用实例 - hxf829 - hxf829的博客.NET Attribute使用实例 - hxf829 - hxf829的博客            get .NET Attribute使用实例 - hxf829 - hxf829的博客{ return _srccolumn; }

.NET Attribute使用实例 - hxf829 - hxf829的博客.NET Attribute使用实例 - hxf829 - hxf829的博客            set .NET Attribute使用实例 - hxf829 - hxf829的博客{ _srccolumn = value; }

.NET Attribute使用实例 - hxf829 - hxf829的博客        }

.NET Attribute使用实例 - hxf829 - hxf829的博客

.NET Attribute使用实例 - hxf829 - hxf829的博客        public ParamAttribute(string ptype, int len)

.NET Attribute使用实例 - hxf829 - hxf829的博客.NET Attribute使用实例 - hxf829 - hxf829的博客        .NET Attribute使用实例 - hxf829 - hxf829的博客{

.NET Attribute使用实例 - hxf829 - hxf829的博客            _parameterType = ptype;

.NET Attribute使用实例 - hxf829 - hxf829的博客            _parameterLength = len;

.NET Attribute使用实例 - hxf829 - hxf829的博客            //throw new Exception("can not use");

.NET Attribute使用实例 - hxf829 - hxf829的博客        }

.NET Attribute使用实例 - hxf829 - hxf829的博客        public ParamAttribute(string ptype, int len,string src)

.NET Attribute使用实例 - hxf829 - hxf829的博客.NET Attribute使用实例 - hxf829 - hxf829的博客        .NET Attribute使用实例 - hxf829 - hxf829的博客{

.NET Attribute使用实例 - hxf829 - hxf829的博客            _parameterType = ptype;

.NET Attribute使用实例 - hxf829 - hxf829的博客            _parameterLength = len;

.NET Attribute使用实例 - hxf829 - hxf829的博客            _srccolumn = src;

.NET Attribute使用实例 - hxf829 - hxf829的博客            //throw new Exception("can not use");

.NET Attribute使用实例 - hxf829 - hxf829的博客        }

.NET Attribute使用实例 - hxf829 - hxf829的博客

.NET Attribute使用实例 - hxf829 - hxf829的博客    }

.NET Attribute使用实例 - hxf829 - hxf829的博客}

使用的时候

定义一个实体类:

.NET Attribute使用实例 - hxf829 - hxf829的博客class tb

.NET Attribute使用实例 - hxf829 - hxf829的博客.NET Attribute使用实例 - hxf829 - hxf829的博客    .NET Attribute使用实例 - hxf829 - hxf829的博客{

.NET Attribute使用实例 - hxf829 - hxf829的博客        private string _aaa;

.NET Attribute使用实例 - hxf829 - hxf829的博客

.NET Attribute使用实例 - hxf829 - hxf829的博客        [Param("NChar",10)]

.NET Attribute使用实例 - hxf829 - hxf829的博客        public string aaa

.NET Attribute使用实例 - hxf829 - hxf829的博客.NET Attribute使用实例 - hxf829 - hxf829的博客        .NET Attribute使用实例 - hxf829 - hxf829的博客{

.NET Attribute使用实例 - hxf829 - hxf829的博客.NET Attribute使用实例 - hxf829 - hxf829的博客            get .NET Attribute使用实例 - hxf829 - hxf829的博客{ return _aaa; }

.NET Attribute使用实例 - hxf829 - hxf829的博客.NET Attribute使用实例 - hxf829 - hxf829的博客            set .NET Attribute使用实例 - hxf829 - hxf829的博客{ _aaa = value; }

.NET Attribute使用实例 - hxf829 - hxf829的博客        }

.NET Attribute使用实例 - hxf829 - hxf829的博客        private string _bbb;

.NET Attribute使用实例 - hxf829 - hxf829的博客

.NET Attribute使用实例 - hxf829 - hxf829的博客        [Param("NChar", 10)]

.NET Attribute使用实例 - hxf829 - hxf829的博客        public string bbb

.NET Attribute使用实例 - hxf829 - hxf829的博客.NET Attribute使用实例 - hxf829 - hxf829的博客        .NET Attribute使用实例 - hxf829 - hxf829的博客{

.NET Attribute使用实例 - hxf829 - hxf829的博客.NET Attribute使用实例 - hxf829 - hxf829的博客            get .NET Attribute使用实例 - hxf829 - hxf829的博客{ return _bbb; }

.NET Attribute使用实例 - hxf829 - hxf829的博客.NET Attribute使用实例 - hxf829 - hxf829的博客            set .NET Attribute使用实例 - hxf829 - hxf829的博客{ _bbb = value; }

.NET Attribute使用实例 - hxf829 - hxf829的博客        }

.NET Attribute使用实例 - hxf829 - hxf829的博客    }

这样子就把映射的类型,长度都存储到特征里面。

感谢:http://www.cnblogs.com/Alexander-Lee/archive/2007/01/24/hbh-orm-06.html