{
[System.AttributeUsage(System.AttributeTargets.Property)]//只能对属性 (Property) 应用属性 (Attribute)。
class DataFieldAttribute : System.Attribute
{
private string _FieldName;
private string _FieldType;
public DataFieldAttribute(string fieldname, string fieldtype)
{
this._FieldName = fieldname;
this._FieldType = fieldtype;
}
public string FieldName
{
get { return this._FieldName; }
set { this._FieldName = value; }
}
public string FieldType
{
get { return this._FieldType; }
set { this._FieldType = value; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CustomerAttribute
{
public class PeoopleExample
{
string _Pl_ID;
[DataFieldAttribute("Pl_ID", "Int")]
public string Pl_ID
{
get { return _Pl_ID; }
set { _Pl_ID = value; }
}
int _PL_Age;
[DataFieldAttribute("PL_Age", "Int")]
public int PL_Age
{
get { return _PL_Age; }
set { _PL_Age = value; }
}
string _Pl_Sex;
[DataFieldAttribute("Pl_Sex", "nvarchar")]
public string Pl_Sex
{
get { return _Pl_Sex; }
set { _Pl_Sex = value; }
}
string _Pl_LoginName;
[DataFieldAttribute("Pl_LoginName", "nvarchar")]
public string Pl_LoginName
{
get { return _Pl_LoginName; }
set { _Pl_LoginName = value; }
}
string _Pl_TrueName;
[DataFieldAttribute("Pl_TrueName", "nvarchar")]
public string Pl_TrueName
{
get { return _Pl_TrueName; }
set { _Pl_TrueName = value; }
}
string _PL_Pwd;
[DataFieldAttribute("PL_Pwd", "nvarchar")]
public string PL_Pwd
{
get { return _PL_Pwd; }
set { _PL_Pwd = value; }
}
}
}
static void Main(string[] args)
{
PeoopleExample mp = new PeoopleExample();
PropertyInfo[] infos = mp.GetType().GetProperties();
string Str_TestAtrrubute = "";
object[] objDataFieldAttribute =null;
foreach (PropertyInfo info in infos)
{
objDataFieldAttribute = info.GetCustomAttributes(typeof(DataFieldAttribute), false);
if (objDataFieldAttribute != null)
{
Console.WriteLine("FieldName: " + ((DataFieldAttribute)objDataFieldAttribute[0]).FieldName + " ; FieldType:" + ((DataFieldAttribute)objDataFieldAttribute[0]).FieldType);
}
}
Console.WriteLine(Str_TestAtrrubute);
Console.ReadKey();
}