在C#中将表与类,属性与字段邦定,并取值.
--
Code
//-------实体类------aaETT----------------
[ActiveRecord("VI_tbUser")]
public class asdfETT : Entity
{
..
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;
}
//-------实体类------aaETT----------------
[ActiveRecord("VI_tbUser")]
public class asdfETT : Entity
{
..
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;
}