.NET 判断对象所有属性是否为空
2017-09-10 01:46
393 查看
如题,此实例考虑对象属性较多的情况(暂不考虑此对象设计是否合理),当想要验证众多对象是否为空时,If Else不在考虑之列,期望用最简单的代码实现,如下:
参考:https://codereview.stackexchange.com/questions/70341/check-if-any-of-class-properties-is-not-null-empty-was-assigned
代码:
代码逻辑简单,控制台应用程序实现,关键代码 pi.GetValue(op, null) ,已运行通过,达到期望,如有错,请指正。
参考:https://codereview.stackexchange.com/questions/70341/check-if-any-of-class-properties-is-not-null-empty-was-assigned
代码:
ObjectProperties op = new ObjectProperties(); op.name = "test"; op.age = 15; StringBuilder sb = new StringBuilder(); PropertyInfo[] properties = op.GetType().GetProperties(); foreach (PropertyInfo pi in properties) { if (pi.GetValue(op,null) != "" && pi.GetValue(op, null) != null) { sb.Append( string.Format("Name: {0} | Value: {1}", pi.Name, pi.GetValue(op, null) ) ); } } Console.WriteLine(sb.ToString()); Console.ReadLine(); }
代码逻辑简单,控制台应用程序实现,关键代码 pi.GetValue(op, null) ,已运行通过,达到期望,如有错,请指正。
相关文章推荐
- java反射机制判断对象所有属性是否全部为空
- java反射机制判断对象所有属性是否全部为空
- js 判断对象中所有属性是否为空
- java反射机制判断对象所有属性是否全部为空
- java 判断对象的所有属性是否为空解决方案
- C#利用反射来判断对象是否包含某个属性的实现方法
- js如何判断一个对象{}是否为空对象,没有任何属性
- 判断JS对象是否拥有某属性的方法举例
- 判断对象属性的值是否空,如为空,删除该属性
- js通过map和set判断数组中的对象的某个属性是否有重复
- 通过length属性判断jquery对象是否存在
- 判断JS对象是否拥有某属性
- js如何判断一个对象{}是否为空对象,没有任何属性
- Java 判断一个对象中属性是否为空或空字符串
- 判断JS对象是否拥有某种属性的两种方式
- 判断js对象是否拥有某一个属性的js代码
- js如何判断一个对象{}是否为空对象,没有任何属性
- 判断JS对象是否拥有某属性
- JavaScript判断对象是否含有某个属性
- C# 根据部分属性来判断俩个对象是否相同