C#特性使用
1.定义属性类
using System;
public class PropertyAttribute:Attribute
{
public string _tableName;
public int _date;
public PropertyAttribute(string tableName)
{
this._tableName = tableName;
}
public PropertyAttribute(int date)
{
this._date = date;
}
}
2.在类中应用属性特性
using System;
[Property("Post_Model")]
public class TestModel
{
private int _postId;
private string _postName;
[Property(100)]
public int PostId
{
set { _postId = value; }
get { return _postId; }
}
public string PostName
{
set { _postName = value; }
get { return _postName; }
}
}
3.获取特性值
Type type = typeof(TestModel);
//PropertyAttribute property = (PropertyAttribute)(type.GetCustomAttributes(false)[0]); //获取类外的特性
//Response.Write(property._tableName);
PropertyAttribute property = (PropertyAttribute)(type.GetProperties()[0].GetCustomAttributes(false)[0]);//获取类里面的特性
Response.Write(property._date);


浙公网安备 33010602011771号