///
<typeparam name="T">
对象类型
</typeparam>
///
<param name="model">
对象
</param>
public
static
void
ForeachClassProperties<T>
(T model)
Type t
=
model.GetType();
PropertyInfo[] PropertyList
=
t.GetProperties();
foreach
(PropertyInfo item
in
PropertyList)
string
name =
item.Name;
object
value = item.GetValue(model,
null
);
下面我们来简单测试下:
新建Model如下:
public class AddressInfo
public int Id { get; set; }
public string userName { get; set; }
public string userTel { get; set; }
public string Addressdetail { get; set; }
public int isMoren { get; set; }
public AddressInfo()
Id = 1;
userName = "陈卧龙";
userTel = "1813707015*";
Addressdetail = "江苏省苏州市工业园区国际科技园";
isMoren = 1;
调用如下:
public partial class index : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
//Response.Redirect("/Home/Login");
AddressInfo model = new AddressInfo();
ForeachClass.ForeachClassProperties<AddressInfo>(model);
测试结果如下:
经过测试,我们可以得到对象的各个属性及对应的值、
其实这块内容输入C# 反射系列,小弟也是误打误撞,撞入了C# 反射,索性每天学一点
@陈卧龙的博客