using System;
using System.Reflection;
namespace CS_Test
{
public class MyStudent
{
private string sName;
public string SName
{
get { return sName; }
set { sName = value; }
}
private int sAge;
public int SAge
{
get { return sAge; }
set { sAge = value; }
}
}
class Test_Attribute
{
[STAThread]
static void Main(string[] args)
{
MyStudent stu1 = new MyStudent();
stu1.SName = "刘德华";
stu1.SAge = 40;
// 获得所有属性
PropertyInfo[] pros = stu1.GetType().GetProperties();
// 显示所有属性值
foreach (PropertyInfo pro in pros)
Console.WriteLine(pro.Name+":"+pro.GetValue(stu1,null));
//更改stu1对象的SAge值
stu1.GetType().GetProperty("SAge").SetValue(stu1, 30, null);
// 显示所有属性值
foreach (PropertyInfo pro in pros)
Console.WriteLine(pro.Name+":"+pro.GetValue(stu1, null));
}
}
}
//描述数据库字段的 Attribute
public class DataFieldAttribute : Attribute
{
public DataFieldAttribute(string fieldName,string fieldType)
{
this._fieldName = fieldName;
this._fieldType = fieldType;
}
// 数据库字段名
private string _fieldName;
public string FieldName
{
get { return _fieldName; }
set { _fieldName = value; }
}
// 数据库字段类型
private string _fieldType;
public string FieldType
{
get { return _fieldType; }
set { _fieldType = value; }
}
}
public class MyStudent
{
private string _name;
//使用“特性”描述对应的数据库字段名、类型
[DataFieldAttribute("SName", "varchar")]
public string Name
{
get { return _name; }
set { _name = value; }
}
private int _age;
[DataFieldAttribute("SAge", "int")]
public int Age
{
get { return _age; }
set { _age = value; }
}
}
static void Main(string[] args)
{
MyStudent stu1 = new MyStudent();
stu1.Name = "刘德华";
stu1.Age = 40;
//通过反射的方法来动态获取此映射规则
PropertyInfo[] infos = stu1.GetType().GetProperties();
object[] objs_fieldAttr = null;
foreach (PropertyInfo info in infos)
{
// GetCustomAttributes:
// 返回实体对象中每个属性对应的“特性”信息(数据库字段名、类型)
objs_fieldAttr = info.GetCustomAttributes(
typeof(DataFieldAttribute), false);
if (objs_fieldAttr != null)
{
Console.Write("实体类的属性名:" + info.Name);
Console.Write(" -> 数据库字段名:");
Console.WriteLine(((DataFieldAttribute)objs_fieldAttr[0]).FieldName);
}
}
}
using System;
using System.Reflection;
namespace CS_Test
{
public class MyStudent
{
private string _name;
//使用“特性”描述对应的数据库字段名、类型
[DataFieldAttribute("SName", "varchar")]
public string Name
{
get { return _name; }
set { _name = value; }
}
private int _age;
[DataFieldAttribute("SAge", "int")]
public int Age
{
get { return _age; }
set { _age = value; }
}
}
//描述数据库字段的 Attribute
public class DataFieldAttribute : Attribute
{
public DataFieldAttribute(string fieldName,string fieldType)
{
this._fieldName = fieldName;
this._fieldType = fieldType;
}
// 数据库字段名
private string _fieldName;
public string FieldName
{
get { return _fieldName; }
set { _fieldName = value; }
}
// 数据库字段类型
private string _fieldType;
public string FieldType
{
get { return _fieldType; }
set { _fieldType = value; }
}
}
class Test_Attribute
{
[STAThread]
static void Main(string[] args)
{
MyStudent stu1 = new MyStudent();
stu1.Name = "刘德华";
stu1.Age = 40;
// 获得所有属性
PropertyInfo[] pros = stu1.GetType().GetProperties();
// 显示所有属性值
foreach (PropertyInfo pro in pros)
Console.WriteLine(pro.Name+":"+pro.GetValue(stu1,null));
//更改stu1对象的SAge值
stu1.GetType().GetProperty("Age").SetValue(stu1, 30, null);
// 显示所有属性值
foreach (PropertyInfo pro in pros)
Console.WriteLine(pro.Name+":"+pro.GetValue(stu1, null));
//通过反射的方法来动态获取此映射规则
PropertyInfo[] infos = stu1.GetType().GetProperties();
object[] objs_fieldAttr = null;
foreach (PropertyInfo info in infos)
{
// GetCustomAttributes:
// 返回实体中每个属性对应的“特性”信息(数据库字段名、类型)
objs_fieldAttr = info.GetCustomAttributes(
typeof(DataFieldAttribute), false);
if (objs_fieldAttr != null)
{
Console.Write("实体类的属性名:" + info.Name);
Console.Write(" -> 数据库字段名:");
Console.WriteLine(((DataFieldAttribute)objs_fieldAttr[0]).FieldName);
}
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有