您的位置:首页 > 编程语言 > C#

在C#中将表与类,属性与字段邦定,并取值.

2009-08-05 16:21 316 查看
--
..
private string cc;
[Property("aaabbbccc")]
public string aa
{
get { return cc; }
set { cc = value; }
}
..
private Hashtable m_UHashtable;
public Hashtable UHashtable
{
get { return m_UHashtable; }
set { m_UHashtable = value; }
}
//---------调用的类---------------------

aaETT _aa=new aaETT();
_aa.From = Theo.GetTableName(ruserdept.GetType());
_aa.UHashtable = ruserdept.InitializeToField(ruserdept.GetType()); //给Hashtable赋值
//从Hashtable获取表的字段名
String fieldName = _aa.UHashtable["aa"]
//-------------------------------
/// <summary>
/// 获取表名
/// </summary>
/// <param name="t">Type,对象类型</param>
/// <returns>String,表名</returns>
public static String GetTableName(Type t)
{
ActiveRecordAttribute MyAttribute = (ActiveRecordAttribute)Attribute.GetCustomAttribute(t, typeof(ActiveRecordAttribute));

if (null == MyAttribute)
{
return String.Empty;
}
else
{
return MyAttribute.Table;
}
}
//获取字段名 并以键值对放入Hashtable中
public Hashtable InitializeToField(Type t)
{
Hashtable ht = new Hashtable();
foreach (MemberInfo m in t.GetMembers())
{
foreach (Attribute attr in m.GetCustomAttributes(false))
{// Check for the AnimalType attribute.
if (attr.GetType() == typeof(PropertyAttribute))
{
ht[m.Name] = ((PropertyAttribute)attr).Column; ;
}
}
}
return ht;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: