C# PropertyInfo类案例详解
作者:-小龙人 时间:2021-12-11 16:47:45
对一个对象进行属性分析,并得到相应的属性值,并判断属性的默认值以及空值
public class People
{
public string name { get; set; }
public int age { get; set; }
public DateTime birthday { get; set; }
public bool isActive { get; set; }
public List<Address> address{get;set;}
}
public class Address
{
public string country { get; set; }
public string province { get; set; }
public string city { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Address> address = new List<Address>()
{
new Address(){
country="china",
province="anHui",
city="bengBu",
},
new Address(){
country="china",
city="shangHai",
},
};
People people = new People()
{
name="wangqilong",
age=23,
birthday=Convert.ToDateTime("2018-09-15"),
isActive=true,
address=address
};
string str = method(people);
}
public static string method(Object obj)
{
string str = "";
Type postType = obj.GetType();
PropertyInfo[] postTypeInfos = postType.GetProperties(); //返回为当前 Type 的所有公共属性,PropertyInfo[] PropertyInfo 的所有公共属性的 Type 对象数组
foreach (PropertyInfo p in postTypeInfos)
{
if (p.PropertyType.FullName == typeof(DateTime).FullName)
{
DateTime pValue = (DateTime)p.GetValue(obj, null);
if (pValue != null && pValue != DateTime.MinValue) //dateTime类型申明时默认值为最小值
{
str += p.Name + ":" + pValue + ";";
}
}
else if (p.PropertyType.FullName == typeof(Int32).FullName)
{
int pValue = (int)p.GetValue(obj, null);
if (pValue != 0) //int类型申明时默认值为最小值0
{
str += p.Name + ":" + pValue + ";";
}
}
else if (p.PropertyType.FullName == typeof(Boolean).FullName)
{
Object pValue = p.GetValue(obj, null);
str += p.Name + ":" + pValue + ";";
}
else if (p.PropertyType.FullName == typeof(String).FullName)
{
Object pValue = p.GetValue(obj, null);
str += p.Name + ":" + pValue + ";";
}
//如果传入的对象包含集合,集合中是另个对象
else if (p.PropertyType.FullName == typeof(List<Address>).FullName)
{
List<Address> list = (List<Address>)p.GetValue(obj, null);
if (list != null)
{
foreach (Address address in list)
{
str += p.Name + ":" + address.country+","+address.province+","+address.city + ";";
}
}
}
}
return str;
}
}
结果:”name:wangqilong;age:23;birthday:2018/9/15 0:00:00;isActive:True;address:china,anHui,bengBu;address:china,,shangHai;”
关于PropertyInfo类信息: https://docs.microsoft.com/zh-cn/dotnet/api/system.reflection.propertyinfo?view=netframework-1.1
来源:https://blog.csdn.net/qq_36330228/article/details/82715331
标签:C#,PropertyInfo
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java中的final关键字使用方式
2023-01-16 15:04:25
对Jpa中Entity关系映射中mappedBy的全面理解
2023-07-25 03:48:39
Android 微信摇一摇功能实现详细介绍
2023-06-21 21:00:09
解析Spring事件发布与监听机制
2022-09-01 09:52:19
![](https://img.aspxhome.com/file/2023/6/88586_0s.jpg)
Android WebView实现网页滚动截图
2022-12-12 12:13:03
![](https://img.aspxhome.com/file/2023/8/88098_0s.jpg)
c#反射调用方法示例
2022-07-14 15:28:40
关于EntityWrapper的in用法
2023-11-29 09:02:11
![](https://img.aspxhome.com/file/2023/3/60353_0s.png)
Android中Snackbar的使用方法及小技巧
2023-10-30 03:13:31
![](https://img.aspxhome.com/file/2023/8/103888_0s.gif)
Java的线程与进程以及线程的四种创建方式
2022-12-01 14:11:07
![](https://img.aspxhome.com/file/2023/5/79905_0s.png)
详解C# ConcurrentBag的实现原理
2022-11-17 02:26:05
![](https://img.aspxhome.com/file/2023/2/76422_0s.jpg)
Java递归遍历树形结构的实现代码
2021-11-15 19:51:59
![](https://img.aspxhome.com/file/2023/1/84261_0s.jpg)
winform导出dataviewgrid数据为excel的方法
2023-09-25 15:01:38
Java实现远程控制技术完整源代码分享
2022-04-10 20:31:53
![](https://img.aspxhome.com/file/2023/3/68003_0s.jpg)
图解红黑树及Java进行红黑二叉树遍历的方法
2023-04-20 22:09:14
![](https://img.aspxhome.com/file/2023/1/83021_0s.png)
Java swing仿酷狗音乐播放器
2023-10-24 03:04:34
![](https://img.aspxhome.com/file/2023/8/91668_0s.jpg)
SpringMVC上传图片与访问
2023-12-06 17:54:31
关于spring boot中几种注入方法的一些个人看法
2022-09-07 10:15:14
Ubuntu 14.04下创建Genymotion安卓虚拟机的步骤详解
2023-11-04 19:17:54
![](https://img.aspxhome.com/file/2023/2/91002_0s.png)
C#实现读取txt文件生成Word文档
2022-08-06 19:03:22
![](https://img.aspxhome.com/file/2023/5/98215_0s.jpg)
关于jdk环境变量的配置方式解读
2023-04-22 14:53:05
![](https://img.aspxhome.com/file/2023/6/61306_0s.png)