C# PropertyInfo类案例详解
作者:-小龙人 发布时间:2021-12-11 16:47:45
标签:C#,PropertyInfo
对一个对象进行属性分析,并得到相应的属性值,并判断属性的默认值以及空值
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


猜你喜欢
- 定义枚举类型时本质上就是在定义一个类,只不过很多细节由编译器帮您补齐了,所以某些程度上,enum关键字的 作用就像是class或interf
- 带返回值的方法练习需求: 设计一个方法可以获取两个数的较大值,数据来自于参数思路:1. 定义一个方法,用于获取两个数中的较大数public
- import java.io.BufferedInputStream;import java.io.BufferedOutputStream
- 常用:System:根空间,包含一些基本的类库 System.Collections:主要是和集合类相关的类库 System.Collect
- ava:采用大端字节序存储数据【低地址存放数据的高位,高地址存放数据的低位,数据高位存放在数组的前面】windows(intel平台):采用
- StringBuilder在高性能场景下的正确用法关于StringBuilder,一般同学只简单记住了,字符串拼接要用StringBuild
- 一、背景Lambda表达式是Java SE 8中一个重要的新特性。lambda表达式允许你通过表达式来代替功能接口。 lambda表达式就和
- ava最明显的一个优势就是它的内存管理机制。你只需简单创建对象,java的垃圾回收机制负责分配和释放内存。然而情况并不像想像的那么简单,因为
- 什么是零拷贝?零拷贝(英语: Zero-copy)技术是指计算机执行操作时,CPU不需要先将数据从某处内存复制到另一个特定区域。这种技术通常
- 今天在编译Java程序时遇到如下问题:No enclosing instance of type PrintListFromTailToHe
- 下载地址:https://www.jb51.net/database/588158.html?_=1522396455592运行程序,关闭工
- springboot 中各种配置项纪录1. @Value最早获取配置文件中的配置的时候,使用的就是这个注解,SpEL表达式语言。// 使用起
- 开发中有时候需要自己封装分页排序时,List如何对某一属性排序呢,分享一个小实例,大家共勉,希望能对大家有用,请多多指教。1.Student
- 前言我曾经在一篇介绍 Compose Navigation 的文章 中提到了 Navigation 的状态保存实际是由 rememberSa
- excel对于下拉框较多选项的,需要使用隐藏工作簿来解决,使用函数取值来做选项选项较少(一般少于5个):private static Dat
- 本文为大家分享了Tablayout简单的使用方法,供大家参考,具体内容如下一、TabLayout普通用法在项目中使用viewpager的时候
- 题目描述:给定一 m*n 的矩阵,请按照逆时针螺旋顺序,返回矩阵中所有元素。示例:思路:这是一道典型的模拟问题:我们可以分析一下,遍历前进轨
- Future接口是Java标准API的一部分,在java.util.concurrent包中。Future接口是Java线程Future模式
- Android5.0之后提供了JobService和JobScheduler,用于在稍后的某个时间点或者当满足某个特定的条件时执行一个任务
- Android上使调用OpenCV 2.4.10 实现二维码区域定位(Z-xing 码),该文章主要用于笔者自己学习中的总结,暂贴出代码部分