[CodonNameAttribute("MyCodon")]
public class MyCodonCodon : AbstractCodon
{
    [XmlMemberAttributeflagAttribute("testMe", IsRequired=true)]
        string testMe = null;
    [XmlMemberAttributeflagAttribute("testMeToo")]
        string testMeToo = null;
    // canonical properties :)
    public string TestMe {
        get;
        set;
    }
    public string TestMeToo {
        get;
        set;
    }
}以前没有接触过,,,
[CodonNameAttribute] 估计是程序里面自己定义的, 不是 .net 默认提供, [XmlMemberAttributeflagAttribute] 可能是 .net 自带
谁能解释一下, 或者说下这类的用法的详细资料在哪儿能查? thx~

解决方案 »

  1.   

    周末了, 大家都回家休息了???
    sigh, 我还留守在公司苦苦跟难缠的代码, 文档搏斗...
      

  2.   

    CodonNameAttribute 就是attrubite类型其定义就和普通class一样,用于反射中的,说一个实用的例子/// <summary>
        /// 数据表描述标签
        /// </summary>
        [AttributeUsage(AttributeTargets.Class, Inherited = false)]   //这句话表示DataTableDesc只能使用到类上
        public class DataTableDescAttribute : System.Attribute        //必须继承这个基类
        {
            string _tableName; //数据表名称
            string _identity; //自增字段         public DataTableDescAttribute(string tableName)
    {
    this._tableName = tableName;
    }                public string TableName
    {
    get{ return this._tableName; }
    }
    public string Identity
    {
    get{ return this._identity; }
    set{ this._identity = value;}
    }比如你数据库里有个表叫t_user,对应的实体类是User,然后用DataTableDesc来记录书库表信息:
    [DataTableDesc("t_user", Identity = "id")]
    class User
    {
        int _id;
        string _name;
        int_age;
        //属性,省略不写了
    }
    然后你可以在反射里得到User类的DataTableDesc里的信息,这样你就可以动态拼sql语句了
     DataTableDescAttribute tableDesc = Attribute.GetCustomAttribute(typeof(User), typeof(DataTableDescAttribute));
    tableDesc.TableName,这样你就在运行时拿到了User类对应的表明 t_user注意DataTableDescAttribute推荐以Attribute结尾,这样你就可以[DataTableDesc("t_user", Identity = "id")]使用了
    还有Identity = "id" Identity是属性,所以可以选择给不给这个属性赋值
      

  3.   

    Attribute一般翻译叫特性可以参考这本书框架设计(第2版):CLR VIA C#
      

  4.   

    []里的属于特性修饰,继承至Attribute 
    比如你说的这个[XmlMemberAttributeflagAttribute("testMe", IsRequired=true)]
    就是
    XmlMemberAttributeflagAttribute:Attribute 然后就好像调用它的构造方法一样
      

  5.   

    记住定义特性修饰的类必须以Attribute结尾