通过反射获取所有属性
2015-06-26 15:55
549 查看
public string getProperties<T>(T t)
{
string tStr = string.Empty; if (t == null) { return tStr; } System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); if (properties.Length <= 0) { return
tStr; } foreach (System.Reflection.PropertyInfo item in properties)
{
string name = item.Name; object value = item.GetValue(t, null); if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
{ tStr += string.Format("{0}:{1},", name, value); }
else { getProperties(value); }
} return tStr;
}
{
string tStr = string.Empty; if (t == null) { return tStr; } System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); if (properties.Length <= 0) { return
tStr; } foreach (System.Reflection.PropertyInfo item in properties)
{
string name = item.Name; object value = item.GetValue(t, null); if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
{ tStr += string.Format("{0}:{1},", name, value); }
else { getProperties(value); }
} return tStr;
}
相关文章推荐
- MyEclipse6.5安装SVN插件的三种方法
- 查看哪个进程占用80端口(转)
- VS2012 ddk驱动编译与虚拟机联机调试设置
- [leetcode] 100.Same Tree
- struts2零配置参考示例
- daatable动态创建
- Javascript模块化编程(require.js)
- iOS 8.3 JB ready
- jquer ajax
- 合并排序
- 佛山南方风机股份启用易度文控管理
- python获取当前脚本下的所有方法
- DW中常用的标签
- 数据结构--顺序栈和链式栈
- CentOS6.6上编译安装httpd2.4
- 链接的地方安卓出现边框,苹果出现阴影解决
- JS Charts生成网页的柱状图,圆饼图以及曲线图
- 用对角线将凸n边行分成三角形,问有多少种分法
- 如何取消IE已限制此网页运行脚本或ActiveX控件
- HTTP协议详解(转)