标签:exchange string assign any style class log sign coder
如题,此实例考虑对象属性较多的情况(暂不考虑此对象设计是否合理),当想要验证众多对象是否为空时,If Else不在考虑之列,期望用最简单的代码实现,如下:
参考: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) ,已运行通过,达到期望,如有错,请指正。
标签:exchange string assign any style class log sign coder
原文地址:http://www.cnblogs.com/tymonyang/p/7500012.html