在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;
}
..
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;
}
相关文章推荐
- ASP.NET(C#)中将TextBox控件的Readonly属性设置为True时后台直接取值取不到的解决方案
- C#中修饰符、字段、属性
- C# 属性(Property)和字段(Field)的区别
- 在ARCGIS中将面范围内的点字段赋予面的字段属性
- Hibernate中将一个字段映射到POJO多个属性
- C#中的类:字段、属性、方法
- C#中字段和属性的区别
- C#属性、字段和成员变量的关系区别
- C#属性、字段和成员变量的关系区别
- C#中对属性和字段的理解
- 常量、字段、方法、属性和索引器(C#)
- 深入理解c#的字段与属性
- C#中属性和字段的区别
- C# 语法练习(9): 类[一] - 访问限制、方法、字段、属性
- C# 中字段和属性的使用时机
- 【c#】定义类成员(字段、方法、属性)
- C# 关键字“this”在静态属性、静态方法或静态字段初始值设定项中无效
- 为什么要用属性 C# .NET中的字段与属性_2
- c# 字段和属性
- C# 类型和成员基础以及常量、字段、属性