c#中判断对象为空的几种方式

如题所述

通常我都是用反射解决的,应为这样比较万能

public static string AnyPropertyIsNull<T>(T t) where T : class

{

PropertyInfo[] rs =  t.GetType().GetProperties();

foreach( PropertyInfo prop in rs )

{

PropertyInfo Info = typeof(T).GetProperty(prop.Name);

object value =Info.GetValue(t);

if( value == null )

{

return string.Format("Property: {0}, null value!", prop.Name);

}

}

return null;

}


温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-05-28
if(你的对象名 == null)
或者有的类型是可以调用方法体的,比如String,if(String.IsNullOoEmpty(对象))本回答被网友采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网