How to check all properties of an object whether null or empty?

后端 未结 9 1360
星月不相逢
星月不相逢 2020-11-28 06:41

I have an object lets call it ObjectA

and that object has 10 properties and those are all strings.

 var myObject = new {Property1=\"\",P         


        
9条回答
  •  心在旅途
    2020-11-28 06:57

    The following code returns if any property is not null.

      return myObject.GetType()
                     .GetProperties() //get all properties on object
                     .Select(pi => pi.GetValue(myObject)) //get value for the propery
                     .Any(value => value != null); // Check if one of the values is not null, if so it returns true.
    

提交回复
热议问题