1.自定义 表名属性 TableNameAttribute
2.自定义 主键属性 PrimaryKeyAttribute
3.自定义 列名属性 TableColumnAttribute 
4.数据表person对应的实体类person.cs
5.获得person.cs类型实体 对应的表名及字段名
=========================================
下面的属性代码文件 都直接建立在App_Code下 以方便使用
1.自定义 表名属性 TableNameAttribute
------------------------------------


 using System;
using System;

 /**//// <summary>
/**//// <summary> /// TableNameAttribute 的摘要说明
/// TableNameAttribute 的摘要说明 /// </summary>
/// </summary>
 [AttributeUsage( AttributeTargets.Class, AllowMultiple=false, Inherited=true )]
[AttributeUsage( AttributeTargets.Class, AllowMultiple=false, Inherited=true )] public sealed class TableNameAttribute : Attribute
public sealed class TableNameAttribute : Attribute

 {
{ private string name;
    private string name; private string schema;
    private string schema; //private CacheStrategy cacheStrategy = GentleSettings.DefaultCacheStrategy;
    //private CacheStrategy cacheStrategy = GentleSettings.DefaultCacheStrategy;

 /**//// <summary>
    /**//// <summary> /// The constructor for the TableName attribute.
    /// The constructor for the TableName attribute. /// </summary>
    /// </summary> /// <param name="name">The name of the database table used to store instances of this class.</param>
    /// <param name="name">The name of the database table used to store instances of this class.</param> public TableNameAttribute( string name )
    public TableNameAttribute( string name )
 
     {
{ this.name = name;
        this.name = name; }
    }

 /**//// <summary>
    /**//// <summary> /// The constructor for the TableName attribute.
    /// The constructor for the TableName attribute. /// </summary>
    /// </summary> /// <param name="name">The name of the database table used to store instances of this class.</param>
    /// <param name="name">The name of the database table used to store instances of this class.</param> /// <param name="strategy">The cache stratgey to use for instances of this type. <see
    /// <param name="strategy">The cache stratgey to use for instances of this type. <see  /// cref="CacheStrategy"/> for a list of available options.</param>
    /// cref="CacheStrategy"/> for a list of available options.</param> //public TableNameAttribute( string name, CacheStrategy strategy )
    //public TableNameAttribute( string name, CacheStrategy strategy ) //{
    //{ //    this.name = name;
    //    this.name = name; //    this.cacheStrategy = strategy;
    //    this.cacheStrategy = strategy; //}
    //}

 /**//// <summary>
    /**//// <summary> /// The name of the database table used to store instances of this class.
    /// The name of the database table used to store instances of this class. /// </summary>
    /// </summary> public string Name
    public string Name
 
     {
{
 get
        get  { return name; }
{ return name; } }
    }

 /**//// <summary>
    /**//// <summary> /// The optional schema name with which to prefix the table name in queries.
    /// The optional schema name with which to prefix the table name in queries. /// This value overrides the default schema definition (if present) in the
    /// This value overrides the default schema definition (if present) in the /// configuration file. Note: this property is currently unused.
    /// configuration file. Note: this property is currently unused.  /// </summary>
    /// </summary> public string Schema
    public string Schema
 
     {
{
 get
        get  { return schema; }
{ return schema; }
 set
        set  { schema = value; }
{ schema = value; } }
    }

 /**////// <summary>
    /**////// <summary> ///// The cache behavior for objects of this type. <see cref="CacheStrategy"/>
    ///// The cache behavior for objects of this type. <see cref="CacheStrategy"/>  ///// for a list of available options.
    ///// for a list of available options. ///// </summary>
    ///// </summary> //public CacheStrategy CacheStrategy
    //public CacheStrategy CacheStrategy //{
    //{ //    get { return cacheStrategy; }
    //    get { return cacheStrategy; } //    set { cacheStrategy = value; }
    //    set { cacheStrategy = value; } //}
    //} }
}
2.自定义 主键属性 PrimaryKeyAttribute
------------------------------------


 using System;
using System;

 /**//// <summary>
/**//// <summary> /// PrimaryKeyAttribute 的摘要说明
/// PrimaryKeyAttribute 的摘要说明 /// </summary>
/// </summary> [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true, Inherited = true)]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true, Inherited = true)] public sealed class PrimaryKeyAttribute : Attribute
public sealed class PrimaryKeyAttribute : Attribute

 {
{ private bool autoGenerated = false;
    private bool autoGenerated = false;

 /**//// <summary>
    /**//// <summary> /// Set this property to true for primary keys that are automatically assigned
    /// Set this property to true for primary keys that are automatically assigned /// by the database on insert (identity columns in SQL server terminology).
    /// by the database on insert (identity columns in SQL server terminology).  /// </summary>
    /// </summary> public bool AutoGenerated
    public bool AutoGenerated
 
     {
{
 get
        get  { return autoGenerated; }
{ return autoGenerated; }
 set
        set  { autoGenerated = value; }
{ autoGenerated = value; } }
    } }
}
3.自定义 列名属性 TableColumnAttribute
--------------------------------------


 using System;
using System; using System.Data;
using System.Data;

 /**//// <summary>
/**//// <summary> /// TableColumnAttribute 的摘要说明
/// TableColumnAttribute 的摘要说明 /// </summary>
/// </summary> [AttributeUsage( AttributeTargets.Property | AttributeTargets.Field, AllowMultiple=false, Inherited=true )]
[AttributeUsage( AttributeTargets.Property | AttributeTargets.Field, AllowMultiple=false, Inherited=true )] public class TableColumnAttribute : Attribute
public class TableColumnAttribute : Attribute

 {
{ private string name;
    private string name; private bool notNull;
    private bool notNull; private int size;
    private int size; private bool hasDbType = false; // true when DbType property has been set
    private bool hasDbType = false; // true when DbType property has been set private long dbType;
    private long dbType; private object nullValue = null;
    private object nullValue = null; private bool handleEnumAsString = false;
    private bool handleEnumAsString = false; private bool isReadOnly = false;
    private bool isReadOnly = false; private bool isUpdateAfterWrite = false;
    private bool isUpdateAfterWrite = false;

 /**//// <summary>
    /**//// <summary> /// Constructor for table columns that are named after their property counterpart
    /// Constructor for table columns that are named after their property counterpart /// and whose value cannot be null.
    /// and whose value cannot be null. /// </summary>
    /// </summary> public TableColumnAttribute()
    public TableColumnAttribute() : this(null, true)
        : this(null, true)
 
     {
{ }
    }

 /**//// <summary>
    /**//// <summary> /// Constructor for table columns that are named after their property counterpart.
    /// Constructor for table columns that are named after their property counterpart. /// </summary>
    /// </summary> /// <param name="notNull">A boolean telling whether null values are allowed in the database</param>
    /// <param name="notNull">A boolean telling whether null values are allowed in the database</param> public TableColumnAttribute(bool notNull)
    public TableColumnAttribute(bool notNull) : this(null, notNull)
        : this(null, notNull)
 
     {
{ }
    }

 /**//// <summary>
    /**//// <summary> /// Constructor for table columns whose value cannot be null.
    /// Constructor for table columns whose value cannot be null. /// </summary>
    /// </summary> /// <param name="name">The name of the database column</param>
    /// <param name="name">The name of the database column</param> public TableColumnAttribute(string name)
    public TableColumnAttribute(string name) : this(name, true)
        : this(name, true)
 
     {
{ }
    }

 /**//// <summary>
    /**//// <summary> /// Constructor for table columns.
    /// Constructor for table columns. /// </summary>
    /// </summary> /// <param name="name">The name of the database column</param>
    /// <param name="name">The name of the database column</param> /// <param name="notNull">A boolean telling whether null values are allowed in the database</param>
    /// <param name="notNull">A boolean telling whether null values are allowed in the database</param> public TableColumnAttribute(string name, bool notNull)
    public TableColumnAttribute(string name, bool notNull)
 
     {
{ this.name = name;
        this.name = name; this.notNull = notNull;
        this.notNull = notNull; }
    }

 /**//// <summary>
    /**//// <summary> /// The name of the database column for storing the property decorated with this attribute.
    /// The name of the database column for storing the property decorated with this attribute. /// </summary>
    /// </summary> public string Name
    public string Name
 
     {
{
 get
        get  { return name; }
{ return name; } }
    }

 /**//// <summary>
    /**//// <summary> /// This property (defaults to true) can be used to specify whether NULL values are
    /// This property (defaults to true) can be used to specify whether NULL values are /// allowed in the database. This allows the framework to fail early if a constraint
    /// allowed in the database. This allows the framework to fail early if a constraint /// is violated.
    /// is violated. /// </summary>
    /// </summary> public bool NotNull
    public bool NotNull
 
     {
{
 get
        get  { return notNull; }
{ return notNull; }
 set
        set  { notNull = value; }
{ notNull = value; } }
    }

 /**//// <summary>
    /**//// <summary> /// The database type of the field in the database. Beware that the DbType enumeration
    /// The database type of the field in the database. Beware that the DbType enumeration /// values are NOT the ones used by the individual providers. Gentle does NOT convert
    /// values are NOT the ones used by the individual providers. Gentle does NOT convert /// the DbType to a "best match" for the provider. It is therefore recommended that
    /// the DbType to a "best match" for the provider. It is therefore recommended that  /// you use the DatabaseType below until a better type definition system is available.
    /// you use the DatabaseType below until a better type definition system is available. /// </summary>
    /// </summary> [Obsolete("Please use DatabaseType instead.")]
    [Obsolete("Please use DatabaseType instead.")] public DbType DbType
    public DbType DbType
 
     {
{
 get
        get  { return (DbType)dbType; }
{ return (DbType)dbType; } set
        set
 
         {
{ hasDbType = true;
            hasDbType = true; dbType = (long)value;
            dbType = (long)value; }
        } }
    }

 /**//// <summary>
    /**//// <summary> /// The database type of the field in the database. Convert the actual database type
    /// The database type of the field in the database. Convert the actual database type /// enumeration to a long by casting it in the declaration.
    /// enumeration to a long by casting it in the declaration. /// </summary>
    /// </summary> public long DatabaseType
    public long DatabaseType
 
     {
{
 get
        get  { return dbType; }
{ return dbType; } set
        set
 
         {
{ hasDbType = true;
            hasDbType = true; dbType = value;
            dbType = value; }
        } }
    }

 /**//// <summary>
    /**//// <summary> /// The size or length of the field in the database. String properties will be clipped
    /// The size or length of the field in the database. String properties will be clipped /// to fit.
    /// to fit. /// This feature will obsoleted once Gentle is capable of extracting type and size
    /// This feature will obsoleted once Gentle is capable of extracting type and size  /// information directly from the database. If specified, the values must match
    /// information directly from the database. If specified, the values must match /// those extracted from the database (when implemented).
    /// those extracted from the database (when implemented). /// </summary>
    /// </summary> public int Size
    public int Size
 
     {
{
 get
        get  { return size; }
{ return size; }
 set
        set  { size = value; }
{ size = value; } }
    }

 /**//// <summary>
    /**//// <summary> /// This property indicates whether a DbType was specified. This construct is necessary
    /// This property indicates whether a DbType was specified. This construct is necessary /// because the DbType enum has no value for undefined.
    /// because the DbType enum has no value for undefined. /// </summary>
    /// </summary> public bool HasDbType
    public bool HasDbType
 
     {
{
 get
        get  { return hasDbType; }
{ return hasDbType; } }
    }

 /**//// <summary>
    /**//// <summary> /// Obsolete, use NullValue instead.
    /// Obsolete, use NullValue instead. /// </summary>
    /// </summary> [Obsolete("Use NullValue instead.")]
    [Obsolete("Use NullValue instead.")] public object MagicValue
    public object MagicValue
 
     {
{
 get
        get  { return nullValue; }
{ return nullValue; }
 set
        set  { nullValue = value; }
{ nullValue = value; } }
    }

 /**//// <summary>
    /**//// <summary> /// This value of this property is used when a column is NotNull and the property value
    /// This value of this property is used when a column is NotNull and the property value /// is null.  If this is undefined the framework will throw an error for NotNull columns
    /// is null.  If this is undefined the framework will throw an error for NotNull columns /// whose values are null.
    /// whose values are null. /// </summary>
    /// </summary> public object NullValue
    public object NullValue
 
     {
{
 get
        get  { return nullValue; }
{ return nullValue; }
 set
        set  { nullValue = value; }
{ nullValue = value; } }
    }

 NullValue Helper Properties for VB.NET Users#region NullValue Helper Properties for VB.NET Users
    NullValue Helper Properties for VB.NET Users#region NullValue Helper Properties for VB.NET Users
 /**//// <summary>
    /**//// <summary> /// This property allows type-safe setting of the NullValue for VB users.
    /// This property allows type-safe setting of the NullValue for VB users. /// </summary>
    /// </summary> public int NullValue_int
    public int NullValue_int
 
     {
{
 set
        set  { NullValue = value; }
{ NullValue = value; } }
    }
 /**//// <summary>
    /**//// <summary> /// This property allows type-safe setting of the NullValue for VB users.
    /// This property allows type-safe setting of the NullValue for VB users. /// </summary>
    /// </summary> public NullOption NullValue_opt
    public NullOption NullValue_opt
 
     {
{
 set
        set  { NullValue = value; }
{ NullValue = value; } }
    } #endregion
    #endregion

 /**//// <summary>
    /**//// <summary> /// This value indicates that the column should not be set on insert and update. It is
    /// This value indicates that the column should not be set on insert and update. It is /// primarily useful for columns that are set internally by the database.
    /// primarily useful for columns that are set internally by the database. /// </summary>
    /// </summary> public bool IsReadOnly
    public bool IsReadOnly
 
     {
{
 get
        get  { return isReadOnly; }
{ return isReadOnly; }
 set
        set  { isReadOnly = value; }
{ isReadOnly = value; } }
    }

 /**//// <summary>
    /**//// <summary> /// This value indicates that the column must be read after each insert and update. It is
    /// This value indicates that the column must be read after each insert and update. It is /// primarily useful for columns that are set internally by the database. Note that using
    /// primarily useful for columns that are set internally by the database. Note that using /// this feature (by setting this to true for any column) will significantly impact
    /// this feature (by setting this to true for any column) will significantly impact  /// performance for the worse, as for every update/insert another select will be
    /// performance for the worse, as for every update/insert another select will be  /// performed. Also, fields will be updated using reflection after select, which is also
    /// performed. Also, fields will be updated using reflection after select, which is also /// quite slow (depending on the number of columns).
    /// quite slow (depending on the number of columns). /// </summary>
    /// </summary> public bool IsUpdateAfterWrite
    public bool IsUpdateAfterWrite
 
     {
{
 get
        get  { return isUpdateAfterWrite; }
{ return isUpdateAfterWrite; }
 set
        set  { isUpdateAfterWrite = value; }
{ isUpdateAfterWrite = value; } }
    }

 /**//// <summary>
    /**//// <summary> /// If member which has this attribute attached is enum then this property
    /// If member which has this attribute attached is enum then this property /// indicates wheter framework saves it as string or as integer.
    /// indicates wheter framework saves it as string or as integer. /// Default is false, ie enums are saved as integers
    /// Default is false, ie enums are saved as integers /// </summary>
    /// </summary> public bool HandleEnumAsString
    public bool HandleEnumAsString
 
     {
{
 get
        get  { return handleEnumAsString; }
{ return handleEnumAsString; }
 set
        set  { handleEnumAsString = value; }
{ handleEnumAsString = value; } }
    } }
}
 public enum NullOption
public enum NullOption

 {
{
 /**//// <summary>
    /**//// <summary> /// <type>.MinValue will be stored as NULL, and NULL will be read as <type>.MinValue.
    /// <type>.MinValue will be stored as NULL, and NULL will be read as <type>.MinValue. /// </summary>
    /// </summary> MinValue,
    MinValue,
 /**//// <summary>
    /**//// <summary> /// <type>.MaxValue will be stored as NULL, and NULL will be read as <type>.MaxValue.
    /// <type>.MaxValue will be stored as NULL, and NULL will be read as <type>.MaxValue. /// </summary>
    /// </summary> MaxValue,
    MaxValue,
 /**//// <summary>
    /**//// <summary> /// 0 (or the equivalent for other numeric types) will be stored as NULL, and NULL will be read as 0.
    /// 0 (or the equivalent for other numeric types) will be stored as NULL, and NULL will be read as 0. /// </summary> This value can only be used with numeric types (such as decimal).
    /// </summary> This value can only be used with numeric types (such as decimal). Zero,
    Zero,
 /**//// <summary>
    /**//// <summary> /// Guid.Empty will be stored as NULL, and NULL will be read as Guid.Empty. This value can only be
    /// Guid.Empty will be stored as NULL, and NULL will be read as Guid.Empty. This value can only be /// used with Guid fields.
    /// used with Guid fields. /// </summary>
    /// </summary> EmptyGuid
    EmptyGuid }
}4.数据表person对应的实体类person.cs
------------------------------------


 using System;
using System; using System.Data;
using System.Data; using System.Configuration;
using System.Configuration; using System.Web;
using System.Web; using System.Web.Security;
using System.Web.Security; using System.Web.UI;
using System.Web.UI; using System.Web.UI.WebControls;
using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
using System.Web.UI.HtmlControls;

 /**//// <summary>
/**//// <summary> /// person 的摘要说明
/// person 的摘要说明 /// </summary>
/// </summary>
 [TableName("person")]
[TableName("person")] public class person
public class person

 {
{ public person()
    public person()
 
     {
{ }
    }
 //[TableColumn("personID", NotNull = true), PrimaryKey]
    //[TableColumn("personID", NotNull = true), PrimaryKey] protected int _personID;
    protected int _personID; //[TableColumn("personName", NotNull = true)]
    //[TableColumn("personName", NotNull = true)] protected string _personName = String.Empty;
    protected string _personName = String.Empty;
 [TableColumn("personID", NotNull = true), PrimaryKey]
    [TableColumn("personID", NotNull = true), PrimaryKey] public int PersonID
    public int PersonID
 
     {
{
 get
        get  { return _personID; }
{ return _personID; }
 set
        set  { _personID = value; }
{ _personID = value; } }
    }
 [TableColumn("personName", NotNull = true)]
    [TableColumn("personName", NotNull = true)] public string PersonName
    public string PersonName
 
     {
{
 get
        get  { return _personName; }
{ return _personName; }
 set
        set  { _personName = value; }
{ _personName = value; } }
    } }
}
5.获得person.cs类型实体 对应的表名及字段名
------------------------------------------


 protected void Button1_Click(object sender, EventArgs e)
protected void Button1_Click(object sender, EventArgs e)

 {
{ person aPerson = new person();
    person aPerson = new person(); aPerson.PersonID = 22;
    aPerson.PersonID = 22; aPerson.PersonName = "zhangsan";
    aPerson.PersonName = "zhangsan";
 Type aType = aPerson.GetType();
    Type aType = aPerson.GetType();
 //类实体公开属性对应的列名属性
    //类实体公开属性对应的列名属性 PropertyInfo[] aPropertyInfos = aType.GetProperties();
    PropertyInfo[] aPropertyInfos = aType.GetProperties(); string strPublicProperty = " ";
    string strPublicProperty = " "; for (int i = 0; i < aPropertyInfos.Length; i++)
    for (int i = 0; i < aPropertyInfos.Length; i++)
 
     {
{ PropertyInfo aPropertyInfo = (PropertyInfo)aPropertyInfos[i];
        PropertyInfo aPropertyInfo = (PropertyInfo)aPropertyInfos[i]; strPublicProperty += i.ToString() + " : 实体属性名 " + aPropertyInfo.Name;
        strPublicProperty += i.ToString() + " : 实体属性名 " + aPropertyInfo.Name; strPublicProperty += " 对应值 " + aPropertyInfo.GetValue(aPerson, null).ToString() + " ";
        strPublicProperty += " 对应值 " + aPropertyInfo.GetValue(aPerson, null).ToString() + " "; object[] attrs = aPropertyInfo.GetCustomAttributes(typeof(TableColumnAttribute), true);
        object[] attrs = aPropertyInfo.GetCustomAttributes(typeof(TableColumnAttribute), true); for (int m = 0; m < attrs.Length; m++)
        for (int m = 0; m < attrs.Length; m++)
 
         {
{ strPublicProperty += " 对应列名 " + ((TableColumnAttribute)attrs[m]).Name;
            strPublicProperty += " 对应列名 " + ((TableColumnAttribute)attrs[m]).Name; }
        } }
    } this.TextBox1.Text = strPublicProperty;
    this.TextBox1.Text = strPublicProperty;

 //FieldInfo[] aFieldInfos = aType.GetFields();
    //FieldInfo[] aFieldInfos = aType.GetFields(); //string strPublicField = " ";
    //string strPublicField = " "; //for (int j = 0; j < aFieldInfos.Length; j++)
    //for (int j = 0; j < aFieldInfos.Length; j++) //{
    //{ //    FieldInfo aFieldInfo = (FieldInfo)aFieldInfos[j];
    //    FieldInfo aFieldInfo = (FieldInfo)aFieldInfos[j]; //    strPublicField += j.ToString() + " : " + aFieldInfo.Name + " ";
    //    strPublicField += j.ToString() + " : " + aFieldInfo.Name + " "; //}
    //} //this.TextBox2.Text = strPublicField;
    //this.TextBox2.Text = strPublicField; 
    
 //类实体对应的表名属性
    //类实体对应的表名属性 string strTablePKName = " ";
    string strTablePKName = " "; object[] attrsAA = aType.GetCustomAttributes(typeof(TableNameAttribute), true);
    object[] attrsAA = aType.GetCustomAttributes(typeof(TableNameAttribute), true); for (int n = 0; n < attrsAA.Length; n++)
    for (int n = 0; n < attrsAA.Length; n++)
 
     {
{ strTablePKName += " " + ((TableNameAttribute)attrsAA[n]).Name;
        strTablePKName += " " + ((TableNameAttribute)attrsAA[n]).Name; }
    }
 //类实体对应的主键属性
    //类实体对应的主键属性 MemberInfo[] aMemberInfos = aType.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
    MemberInfo[] aMemberInfos = aType.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); string strMemberInfo = " ";
    string strMemberInfo = " "; for (int k = 0; k < aMemberInfos.Length; k++)
    for (int k = 0; k < aMemberInfos.Length; k++)
 
     {
{ MemberInfo aM = aMemberInfos[k];
        MemberInfo aM = aMemberInfos[k]; object[] attrs = aM.GetCustomAttributes(typeof(PrimaryKeyAttribute), true);
        object[] attrs = aM.GetCustomAttributes(typeof(PrimaryKeyAttribute), true); for (int m = 0; m < attrs.Length; m++)
        for (int m = 0; m < attrs.Length; m++)
 
         {
{ strTablePKName += " 主键 " + aM.Name;
            strTablePKName += " 主键 " + aM.Name; }
        } }
    }
 this.TextBox2.Text = strTablePKName;
    this.TextBox2.Text = strTablePKName; 
    }
}