1.自定义 表名属性 TableNameAttribute
2.自定义 主键属性 PrimaryKeyAttribute
3.自定义 列名属性 TableColumnAttribute
4.数据表person对应的实体类person.cs
5.获得person.cs类型实体 对应的表名及字段名
=========================================
下面的属性代码文件 都直接建立在App_Code下 以方便使用
1.自定义 表名属性 TableNameAttribute
------------------------------------
using System;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// TableNameAttribute 的摘要说明
/// </summary>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
[AttributeUsage( AttributeTargets.Class, AllowMultiple=false, Inherited=true )]
public sealed class TableNameAttribute : Attribute
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
private string name;
private string schema;
//private CacheStrategy cacheStrategy = GentleSettings.DefaultCacheStrategy;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// The constructor for the TableName attribute.
/// </summary>
/// <param name="name">The name of the database table used to store instances of this class.</param>
public TableNameAttribute( string name )
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.name = name;
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// The constructor for the TableName attribute.
/// </summary>
/// <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
/// cref="CacheStrategy"/> for a list of available options.</param>
//public TableNameAttribute( string name, CacheStrategy strategy )
//{
// this.name = name;
// this.cacheStrategy = strategy;
//}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// The name of the database table used to store instances of this class.
/// </summary>
public string Name
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return name; }
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// The optional schema name with which to prefix the table name in queries.
/// This value overrides the default schema definition (if present) in the
/// configuration file. Note: this property is currently unused.
/// </summary>
public string Schema
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return schema; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ schema = value; }
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////// <summary>
///// The cache behavior for objects of this type. <see cref="CacheStrategy"/>
///// for a list of available options.
///// </summary>
//public CacheStrategy CacheStrategy
//{
// get { return cacheStrategy; }
// set { cacheStrategy = value; }
//}
}
2.自定义 主键属性 PrimaryKeyAttribute
------------------------------------
using System;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// PrimaryKeyAttribute 的摘要说明
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true, Inherited = true)]
public sealed class PrimaryKeyAttribute : Attribute
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
private bool autoGenerated = false;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Set this property to true for primary keys that are automatically assigned
/// by the database on insert (identity columns in SQL server terminology).
/// </summary>
public bool AutoGenerated
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return autoGenerated; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ autoGenerated = value; }
}
}
3.自定义 列名属性 TableColumnAttribute
--------------------------------------
using System;
using System.Data;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// TableColumnAttribute 的摘要说明
/// </summary>
[AttributeUsage( AttributeTargets.Property | AttributeTargets.Field, AllowMultiple=false, Inherited=true )]
public class TableColumnAttribute : Attribute
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
private string name;
private bool notNull;
private int size;
private bool hasDbType = false; // true when DbType property has been set
private long dbType;
private object nullValue = null;
private bool handleEnumAsString = false;
private bool isReadOnly = false;
private bool isUpdateAfterWrite = false;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Constructor for table columns that are named after their property counterpart
/// and whose value cannot be null.
/// </summary>
public TableColumnAttribute()
: this(null, true)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Constructor for table columns that are named after their property counterpart.
/// </summary>
/// <param name="notNull">A boolean telling whether null values are allowed in the database</param>
public TableColumnAttribute(bool notNull)
: this(null, notNull)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Constructor for table columns whose value cannot be null.
/// </summary>
/// <param name="name">The name of the database column</param>
public TableColumnAttribute(string name)
: this(name, true)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Constructor for table columns.
/// </summary>
/// <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>
public TableColumnAttribute(string name, bool notNull)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.name = name;
this.notNull = notNull;
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// The name of the database column for storing the property decorated with this attribute.
/// </summary>
public string Name
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return name; }
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 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
/// is violated.
/// </summary>
public bool NotNull
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return notNull; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ notNull = value; }
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 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
/// 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.
/// </summary>
[Obsolete("Please use DatabaseType instead.")]
public DbType DbType
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return (DbType)dbType; }
set
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
hasDbType = true;
dbType = (long)value;
}
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// The database type of the field in the database. Convert the actual database type
/// enumeration to a long by casting it in the declaration.
/// </summary>
public long DatabaseType
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return dbType; }
set
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
hasDbType = true;
dbType = value;
}
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// The size or length of the field in the database. String properties will be clipped
/// to fit.
/// This feature will obsoleted once Gentle is capable of extracting type and size
/// information directly from the database. If specified, the values must match
/// those extracted from the database (when implemented).
/// </summary>
public int Size
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return size; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ size = value; }
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// This property indicates whether a DbType was specified. This construct is necessary
/// because the DbType enum has no value for undefined.
/// </summary>
public bool HasDbType
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return hasDbType; }
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Obsolete, use NullValue instead.
/// </summary>
[Obsolete("Use NullValue instead.")]
public object MagicValue
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return nullValue; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ nullValue = value; }
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 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
/// whose values are null.
/// </summary>
public object NullValue
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return nullValue; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ nullValue = value; }
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
NullValue Helper Properties for VB.NET Users#region NullValue Helper Properties for VB.NET Users
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// This property allows type-safe setting of the NullValue for VB users.
/// </summary>
public int NullValue_int
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ NullValue = value; }
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// This property allows type-safe setting of the NullValue for VB users.
/// </summary>
public NullOption NullValue_opt
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ NullValue = value; }
}
#endregion
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 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.
/// </summary>
public bool IsReadOnly
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return isReadOnly; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ isReadOnly = value; }
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 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
/// 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
/// performed. Also, fields will be updated using reflection after select, which is also
/// quite slow (depending on the number of columns).
/// </summary>
public bool IsUpdateAfterWrite
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return isUpdateAfterWrite; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ isUpdateAfterWrite = value; }
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// If member which has this attribute attached is enum then this property
/// indicates wheter framework saves it as string or as integer.
/// Default is false, ie enums are saved as integers
/// </summary>
public bool HandleEnumAsString
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return handleEnumAsString; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ handleEnumAsString = value; }
}
}
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public enum NullOption
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// <type>.MinValue will be stored as NULL, and NULL will be read as <type>.MinValue.
/// </summary>
MinValue,
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// <type>.MaxValue will be stored as NULL, and NULL will be read as <type>.MaxValue.
/// </summary>
MaxValue,
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 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).
Zero,
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Guid.Empty will be stored as NULL, and NULL will be read as Guid.Empty. This value can only be
/// used with Guid fields.
/// </summary>
EmptyGuid
}
4.数据表person对应的实体类person.cs
------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// person 的摘要说明
/// </summary>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
[TableName("person")]
public class person
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public person()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//[TableColumn("personID", NotNull = true), PrimaryKey]
protected int _personID;
//[TableColumn("personName", NotNull = true)]
protected string _personName = String.Empty;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[TableColumn("personID", NotNull = true), PrimaryKey]
public int PersonID
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _personID; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _personID = value; }
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[TableColumn("personName", NotNull = true)]
public string PersonName
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _personName; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _personName = value; }
}
}
5.获得person.cs类型实体 对应的表名及字段名
------------------------------------------
protected void Button1_Click(object sender, EventArgs e)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
person aPerson = new person();
aPerson.PersonID = 22;
aPerson.PersonName = "zhangsan";
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Type aType = aPerson.GetType();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//类实体公开属性对应的列名属性
PropertyInfo[] aPropertyInfos = aType.GetProperties();
string strPublicProperty = " ";
for (int i = 0; i < aPropertyInfos.Length; i++)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
PropertyInfo aPropertyInfo = (PropertyInfo)aPropertyInfos[i];
strPublicProperty += i.ToString() + " : 实体属性名 " + aPropertyInfo.Name;
strPublicProperty += " 对应值 " + aPropertyInfo.GetValue(aPerson, null).ToString() + " ";
object[] attrs = aPropertyInfo.GetCustomAttributes(typeof(TableColumnAttribute), true);
for (int m = 0; m < attrs.Length; m++)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strPublicProperty += " 对应列名 " + ((TableColumnAttribute)attrs[m]).Name;
}
}
this.TextBox1.Text = strPublicProperty;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//FieldInfo[] aFieldInfos = aType.GetFields();
//string strPublicField = " ";
//for (int j = 0; j < aFieldInfos.Length; j++)
//{
// FieldInfo aFieldInfo = (FieldInfo)aFieldInfos[j];
// strPublicField += j.ToString() + " : " + aFieldInfo.Name + " ";
//}
//this.TextBox2.Text = strPublicField;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//类实体对应的表名属性
string strTablePKName = " ";
object[] attrsAA = aType.GetCustomAttributes(typeof(TableNameAttribute), true);
for (int n = 0; n < attrsAA.Length; n++)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strTablePKName += " " + ((TableNameAttribute)attrsAA[n]).Name;
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//类实体对应的主键属性
MemberInfo[] aMemberInfos = aType.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
string strMemberInfo = " ";
for (int k = 0; k < aMemberInfos.Length; k++)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MemberInfo aM = aMemberInfos[k];
object[] attrs = aM.GetCustomAttributes(typeof(PrimaryKeyAttribute), true);
for (int m = 0; m < attrs.Length; m++)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strTablePKName += " 主键 " + aM.Name;
}
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.TextBox2.Text = strTablePKName;
}